How to make a OkHttp call within a Android.WorkManager? - okhttp

I have several OkHttp requests that I need to send and even if there is no internet for some time, in the end they have to be delivered. It seems that WorkManager is a good API that can handle this.
But how to integrate OkHttp requests and WorkManager?
And (second) be able to get notifications?

I have recently used same as you want:
You can do that like:
#NonNull
#Override
public Result doWork() {
// DO OKHTTP REQUEST HERE........
return Result.success();
}
Note : Do not use any UI thread here like progress bar.

Related

Android Management - How to send bulk request?

We have been using Android Management APIs to enroll devices in an enterprise and provide restricted environment to our users. We have a requirement where we need to bulk update all the devices by making a patch request using this API: https://developers.google.com/android/management/reference/rest/v1/enterprises.devices/patch
What I am looking for is that instead of sending requests one by one in an arbitrary fashion, is there any way I can send bulk requests and receive a response when all the devices are updated?
Note: I am already aware of how to do it one by one.
Help appreciated, thanks.
Use the AndroidManagement.batch() API to queue several requests for execution and reduce the connections your client makes. The now deprecated Google EMM API documentation has more information about this.
Here's a pseudo example on how to create a batch request, queue a device patch call and execute the request thereafter.
AndroidManagement amapi;
var batchRequest = amapi.batch();
amapi.enterprises().devices().patch(deviceName, content).queue(batchRequest, new JsonBatchCallback<>() {
#Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
}
#Override
public void onSuccess(com.google.api.services.androidmanagement.v1.model.Device device, HttpHeaders responseHeaders) throws IOException {
updateDevice(device);
}
});
// queue another device patch request...
// execute all queued requests
batchRequest.execute();
Please note that if you want to do a bulk update in order to save on the API usage limit, this will not help you. All requests in your batch will be counted separately when it comes to limits. There is no way around this.
Can you give more context about what you are trying to achieve? Perhaps what you want to do can be achieved with a policy. For example, to install an app on multiple devices, you could apply the same policy to those devices, and then add these lines to the policy:
"applications": [
{
"packageName": "com.google.samples.apps.iosched",
"installType": "FORCE_INSTALLED"
}
]
Regarding batch requests, here’s a sample code on how you can do batch requests in Android Management API.
def list_devices(request_id, response, exception):
"""Do something with the devices list response."""
if exception is not None:
# Do something with the exception.
pass
else:
# Do something with the response.
pass
get_device1 = androidmanagement.enterprises().devices().get(name=deviceName)
get_device2 = androidmanagement.enterprises().devices().get(name=deviceName)
batch = androidmanagement.new_batch_http_request();
batch.add(get_device1, list_devices)
batch.add(get_device2, list_devices)
batch.execute()
You may also check this link for more details about new_batch_http_request.
"You can use the batch() call on AndroidEnterprise and execute bulk requests by calling BatchRequest.execute().
Please note, you are limited to 1000 calls in a single batch request, so if you need to make more calls than that, use multiple batch requests.
Here's the format you can refer to: EMM API batch format.
Along with an example: EMM API batch example."

Background processing on C# web api controller

I'm new on .NET technology and come into some problem. Currenlty i'm trying to build a REST API that handle long processing before sending the result to client.
What i'm trying to achieve is, i would like to do a background processing after receiving request from client. But, i would also like to send a response to client.
In short, it would be something like this.
Client Request -> Handled by controller ( doing some processing ) -> send response directly, ignoring the background that still running.
On Java, i can do this using Runnable Thread. How can i achieve this on C# Web API ?
Thank you.
In short, don't do this.
The job of an API is not to perform heavy duty, long running tasks.
You could simply let the API receive the request to perform something, then delegate that to another service. The API can then send a 200 response to show it received the request and maybe a URL to another resource which allows a user to track the progress.
The API needs to be available and responsive at all times. It needs to serve a number of users and if a number of them all request something that uses a lot of resources and takes a lot of time, chances are the API will simply go down and not serve anyone.
This is why you do not do such things in an API. Let other services do the heavy lifting.
Your api can call another async method and return 200/OK response without waiting for the request to complete.
You can learn more about async programing in c#.
static async Task Main(string[] args)
{
Console.WriteLine("coffee is ready");
var toastTask = MakeToastWithButterAndJamAsync(2);
async Task<Toast> MakeToastWithButterAndJamAsync(int number)
{
//Do something here.
}
}
This can be achieve this using loosed coupled architecture, by introducing service bus or blob storage, once you receive request in web api you can save it to blob/service bus and return acknowlegement response from web api. From service bus/blob storage use webjob/function/ durable function app to process the message using event.

How to design a spring boot application that can process 1k requests a second

I have a rest controller which accepts post requests and returns the statuses of whether the operations are successfull or not. It works fine for 100 requests per second as I have multiple operations underlying it which at the end send the response.
There could be hundreds of users trying to send the requests to the controller, thereby its all done using a completable future and http Async invoker. The problem happens when there are a 1000 requests per second and then the controller threads are exhausted as there are already multiple thread processing multiple requests and all are waiting for there future to be complete and then sending the response.
How can I make my rest controller be able to handle 1000 requests per Second without breaking.
there are already multiple thread processing multiple requests and all are waiting for there future to be complete and then sending the response.
You can actually make you controllers asynchronous by making them return a CompletableFuture. Just chain the calls on the CompletableFuture returned by your service to convert them into a an appropriate response instead of using get() or join():
#RequestMapping
public CompletableFuture<ResponseEntity<…>> processRequest() {
return myService.getStatusFuture()
.thenApply(status -> convertToResponseEntity(status));
}
Of course for this to work properly you should have a truly asynchronous service. If you are using #Async or submitting tasks with CompletableFuture.supplyAsync(), this will just move the problem from the HTTP threadpool to another threadpool.
It depends on the servlet server you are using. In the application.properties file, you can use the server.* properties to set the parameters you need.
In this link you can find these properties under the EMBEDDED SERVER CONFIGURATION section. If you are using the default tomcat embedded server, check out the server.tomcat.* properties. Especially the server.tomcat.accept-count, server.tomcat.max-connections and server.tomcat.max-threads properties.

Prevent client repeatedly sending requests

I have a spring controller and request mapped method.
#RequestMapping(value = { "/doSomething.do" })
public ModelAndView emailToNonBelievers(){
.....
// do something which takes lot of time
// send response..
return modelAndView;
}
This method takes very long time, about an hour.(It is for Admin, not users. And Admin doesn't need to wait an hour. Just fire and forget, that's ok. And this is not a batch job)
But I found that client send requests repeatedly with 3 minutes interval(observed 6 times and I stopeed Spring service).
I guess because client didn't get any response from server.
If my guess is right, server should response sort of "Your request is accepted, just shut up and wait!!" .
But how to send response(200 ok) before jobs finished in Spring?
Or am I missing something?
In this situation it would be recommended to use asynchronous task processing. Spring comes with out-of-box support for it via #Async annotation.Consult my answer for detailed setup for similar query and here for docs.

how to make a httpwebrequest on application launch in wp7

how to make a httpwebrequest on application launch in windows phone 7 and 8 synchronous so that based on the response from the server can change the start page .
private void Application_Launching(object sender, LaunchingEventArgs e)
{
// http request and respose b
// based on response select the start page
}
Well as only async requests are performed. For WP7, you can perform async request and wait for response, based on response you can navigate to Pages as per you logic. To do that, in Startup Page:
In class's constructor call method like NavigateToPages();
Now in that method you can call the http reqeust you want and when you get response navigate to page like,
void NavigateToPage()
{
WebClient client = new WebClient();
client.DownloadStringCompleted += (object sender, DownloadStringCompletedEventArgs e) =>
{
var result = e.Result;
//Navigate to page
}
client.DownloadStringAsync(new Uri("<your web request"));
}
This will wait till you get response. Meanwhile to show user that you're requesting web service you can add ProgressIndicator and before starting web request start it and just put "Fetching Response" as text and in the response make it empty "". Also it is recommended you should call request in try-catch-finally block. So that if internet is down or problem getting reponse app should not crash.
I done this for WP7, I think should work for WP8 as well. Try it out if you want.
Well you cannot make a synchronous call.
you can do is the next best thing. Make the method async and use the PCL version of http client.
this would allow you logic to flow in a synchronous like way (still async though).
As #Hermit Dave suggest, you cannot make synchronous http request in WP, and making it async will break your current application logic. The application execution will probably exit Application_Launching method before the call return response.
Maybe you can create a default page as start page displaying some sort of loading animation and some hint to give user idea of what the application is currently processing. In that page make async http request. And in the callback, redirect to proper page based on the response.

Resources