Stop execution for long running request - java

I have this scenario: Service A calls Service B by a HTTP POST request.
Service A ---> Service B
Service B at times, takes more than 3 minutes to return the result but the timeout configured in HTTP request in A is 60s.
I want to know how to do this by Java with HTTP Client.
What I want to know is how can I stop/cancel the execution in Service B if it takes more than 60 seconds to complete or the timeout expires to avoid inconsistencies in the database.
Thank you!

Related

gRPC-java server delay in processing request and sending response under load

I am running a performance test on my unary gRPC server using GHZ, under load about 100K request the response time is high(about 1s to 1.5s) for initial few thousand requests.
After some debugging what I am observing is that there is a delay of about 500ms between the end of gRPC server interceptor and invocation of the gRPC service method, and about 500ms delay between the end of service method and invocation of SimpleForwardingServerCallListener.onComplete.
What could be causing this delay, I have configured 8 threads for netty server and 8 threads for gRPC cancellation context executor.
Had posted the same question on github java-grpc, below is the thread. https://github.com/grpc/grpc-java/issues/7372
TL;DR
A large number of concurrent users(num users >> num gRPC worker threads ) would mean the system is oversubscribed and unable to process the requests, thus creating a backlog of requests to be processed. This causes delay in processing the request even though the request is accepted by the gRPC event loop thread. For example. Total time for processing a single request= 375ms Time between end of gRPC server interceptor(ServerInterceptor.interceptCall) and invocation of gRPC service = 162ms Time to process the request by gRPC service = 65ms Time between end of gRPC service and invocation of SimpleForwardingServerCallListener.onComplete = 148ms
382/61=6.2, so the system is 6.2x oversubscribed. 100/6.2=16 which would be closer to the maximum concurrent RPCs it can handle.

How to trigger first update with rabbitMQ using rxJava 2.0?

Is there an elegant way to subscribe to updates only after I trigger first update of my data using rabbitMQ?
Or.. Is there a way to know when a new consumer is added and trigger sending the data?
For example:
Service A is getting updates from Service B (using rabbitMQ, Service B pushes IPs that I need to send the data to).
Service A is also getting requests from Service C and sends each request to all the IPs from Service B.
My problem is when Service A is up, there might be 1-5 minutes until Service B pushes an update. Meanwhile, Service C can send 100 requests, and I'll have no ips to send these requests ...
If the queue could have known that a new consumer is added - we could trigger sending all the ips..
I hope it explains my problem.
Any help would be very appreciated.

How to get the response if returned after the time out occurs

I am consuming a REST web service from Java code using Apache commons HTTP client API. If no response returns within the socket timeout value configured in the connection manager parameters, socket time out exception occurs. In such cases as the thread returns the exception to the caller class, even if the REST service returns response few secs later, will be lost.
Is it possible to create a new thread which will still listen to the service even after the timeout and just logs the response, while the main thread returns the exception to the caller class?
Is there any better way to achieve this?
Thanks.
The pattern you are most likely looking for involves asynchronous requests. For every action you post you create a unique "job" id and with that a specific URL for the job status. After starting the job, you can then query on that specific job instance's status. For example:
POST to /actions
Returns 202 Accepted & include a Location header to /actions/results/1234
Immediately GET /actions/results/1234 to ascertain it's status.
If it returns a 2xx your job is done.
If it returns 404, wait 10 seconds (or whatever) and try again.
Once you are happy with the result, issue a DELETE to /actions/results/1234 to clean up after yourself.
Of course you don't have to return 404 if the job is not done, there are other strategies for checking on the status - the key thing is that it's a subsequent call.

Long polling in spring mvc(async)

My operation takes 30 mins to process which is invoked by a rest call request. i want to give the client an immediate response telling operation in progress,and processing should happen in another thread, what is the best way to crack this out,Is deferred result the only way.
30 minutes is a long time. I'd suggest you using websockets to push progress updates and operation status.
Since you are providing rest services, another approach could be to immediately return 'Accepted' (202) or 'Created' (201) to the client and provide a link to another service that would provide updates about the progress status of the processing. This way the client is free to decide whether to poll the server for updates, or just provide the user an 'update status' button.
Use a message queue (ActiveMQ, Redis).
Send request from client.
Controller gets request, post process/message in message queue.
Send response back to client saying it's processing.
Another thread to look for changes/new process in message queue.
Execute the process - Update the status in message queue each step is completed. - (started/running/completed/failed).
You can show the status of process everytime with the id of process in queue.

long polling using JSON not working

I am trying to build chat application with long polling mechanism on Google app engine server.
HTTPRequest has default time out of 30 seconds, so I am sending polling request to server every 28 seconds if there is no update from server (so that I wont miss any message from other clients).
First request gets registered, but second request sent after 28 seconds is not reaching server.
function loadPage(query){
$.get({ url: query, success: function(events){
updated = 1;
//events data processing
createServerChannel();
});
}
function createServerChannel(){
var query='/ChatController?&user='+userName+'&sessionName='+sessionName+'&register=true';
loadPage(query);
updated = 0;
setInterval(function() { poll(query); }, 28000);
};
function poll(query){
if(updated==0){
loadPage(query);
}
}
I am using thread.wait() for request to wait on server. Is there any way to consume first pending request when next request from same client is available.
Please help.
I think web sockets might be a better approach as this keeps a continuous connection open to the server and waits for the server to push data to the client.
http://www.html5rocks.com/en/tutorials/websockets/basics/

Categories

Resources