REST with JAX-RS - Asynchronous call - java

I saw the below post from "cmd" which was posted couple of years back. And "Wojtek Owczarczyk" was answered this one. I am good with all the answer, except last line.
My Confusion is, If we return immediately with ACCEPTED status. Then, we will lost the track of the request.
So i am planning to implement below steps. Please correct me if i am wrong.
1) As soon as the request hits service api - I will create one Job Id and persist my request detail and send back the client with ACCEPTED status code along with Job id.
2) Then, i will create the new thread for that request to continue with the requested operation.
3) After successful completion of Operation, I will send back the client with all status of the request.
4) Finally, in callbackCompletion register i will remove the job id from my persistence list.
To implement the above logic, i need client to send his listener information along with request (basically URI). This is to update the request status to client back, after processing the request.
REST with JAX-RS - Handling long running operations

This is not how REST is meant to work in my opinion. I would do the following approach instead:
Client makes a request for a long operation
Create a job id and run the job asynchronously
Return the accepted status together with the a URI to request the status for the job. For example: http://.../resources/jobs/1234
The client is now responsible e.g. to poll the URI to get the current status of the job execution.

Related

How to handle this response case

I have an endpoint to which another team connects. This endpoint returns a response which is irrelevant to the team. It only cares if their input has reached us or not. But we have to do a lot of processing after that. Currently we just take in the request and respond How to handle this scenario. Below is the sample code.
#POST
public Response perform(Request request){
//do something here that takes some time.
return Response.status(Httpstatus.OK).build();
}
What is the best way to make sure that the response goes back to the caller even though the processing keeps going on. I thought about async but wanted to check if there was a better way.
Return a
202 Accepted
The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place. There is no facility for re-sending a status code from an asynchronous operation such as this.
The 202 response is intentionally non-committal. Its purpose is to allow a server to accept a request for some other process (perhaps a batch-oriented process that is only run once per day) without requiring that the user agent's connection to the server persist until the process is completed. The entity returned with this response SHOULD include an indication of the request's current status and either a pointer to a status monitor or some estimate of when the user can expect the request to be fulfilled.
including a Link where the result will be available.
#POST
public Response perform(Request request){
//TODO attach the request to an asynch background job
//pseudo code!
return Response.status(ACCEPTED)
.location(/* URI where the result will be accessible*/)
.build();
}

REST API Single Request - Multiple responses

I am writing a REST API in JAX-RS 2.0, JDK 8 for the below requirement
POST API /server/fileUpload/ (Multipart Form data) where I need to send a Big .AI (Adobe Illustrator) File in this.
The Server, takes the file and return Status 202 (Accepted), Acknowledging that file transfer happened Successfully. (From endpoint to Server)
Now at the Server, I am using Java + Imagemagik to convert .AI File (20-25 MB File) to small JPG Thumbnail, place on a Apache HTTP Server and share the location (like http://happyplace/thumbnail0987.jpg)
Now the Second Response should come from Server with Status 200 OK and Thumbnail URL
is it feasible with one REST API? (Async/similar)
or should I split it to 2 API calls, Please suggest
No. In http, one request gets one response. The client must send a second request to get a second response.
You can use WebSockets for that.
If you are calling from script the call will be async you can handle the Thumbnail URL when you get a response. When you are calling from java program i suggest to run it on a different thread, If the execution is not sequential i.e ( Remaining lines can be executed without getting URL). If url is needed for the remaining section of code you can make one call and wait for the response then execute remaining code.
You need to make different APIs for both scenarios. One for showing file upload status and another for all file conversion and manipulation.
On the client side second request must be callback of first request.
The best way to handle these kind of scenario is to use Java Reactive (Project Reactor, WebFlux).
You can return two response using custom middlewares in asp.net (however not recommended).
Return response from one middleware and subsequently you can invoke next middleware and return second response from second middleware

Java servlet, respond after recieving each request before queuing

I am using Java servlets using Apache tomcat.
I've configured a threadpool and am dealing with each request.
My page is taking in many GET requests at the same time, I'm wondering if I can respond to the server after each get request before any of the logic happens?
So server gives me a request -> I respond with either 'good send another' or 'bad send another' before I start my queueing.
Any help would be much appreciated!
EDIT
Sorry that was terribly written :(
What I'm asking for is a way to send a Header to the client (in this case it's a server which sends me lots of requests). The response would just be 200 or error based on the information I get sent.
What my program is doing:
My servlet gets sent lots of GET requests from one client. (over 100,000) Which I am using tomcat to queue and put into a threadpool. It is then assigned to a worker thread which processes it and puts it into a database.
I've been told to do is send a request back to that server saying 'ok received it'. I think I can use a header response but I don't have the URL of that client (and the client can change for different campaigns). So was wondering what the best way would be to send that response.
After doing some more research I think what I'm looking for is ServletOutputStream.
response.setContentType("text/html");
ServletOutputStream output = response.getOutputStream();
output.flush();
output.close();
Using servlet output stream where do I set the <head><body> tag? and insert the header response afterwards.
The simple answer is "sure".
If these are get requests from a web page for a web page, include a refresh timer and send back some token that can be used to identify the difference between a first-time-request and an I-requested-earlier-are-you-done request. In this case the refresh timer can be set via a meta refresh tag.
If the get requests are part of a REST API then you can define "got it and I'm working" into the protocol. For instance, return a 202 to indicate "got it but not done" and return 200 to indicate "done". As with the html page, consider sending some token back with the 202 that identifies the pending request.

How to match responses to requests in Netty client?

I'm implementing a http client app with Netty. The task is to send the same request to several endpoints and collect the answers for further processing. Many such requests can be sent concurrently to the same endpoints. The problem is to match responses received from one endpoint to requests.
One possible solution is to create a new handler (and pipeline) for each request as describe here https://stackoverflow.com/a/7905761/4926576. In this case request can be mapped to handler and handler can store the response. But this means creating new connection for each request which will degrade the performance.
I also don't want to change the protocol and include request ids in requests/responses only for the purpose of matching.
If both the client and server are respecting HTTP pipelining semantics, then the server must respond to requests in order. Therefore, for each connection, you can maintain a queue of requests. Each new request goes on the back of the queue and each response pops it's matching request from the front of the queue.
In the event of connection failure the queue also provides you with the list of requests that were sent but for which you have not received a response. You can then take appropriate error correcting action for each request.

Sending GET & POST requests in Java or other without responses

Is it possible to make GET & POST requests in Java or another language such that you don't care about what is returned?
As in just sending the requests but not wanting to receive any responses?
Whether you care about the response or not, it will be sent. The HTTP protocol specifications say that it must be.
If you don't care about the response, your client could just close the connection immediately after sending the request. But the chances are that you do want to know that the request was processed (i.e. the response status) even if you don't want to look at the contents of the response message.
So maybe you could send the request and request body, and read the response status and then close the connection without reading the response body. However, this has a downside. It means that you can't reuse the HTTP connection to make further requests. The next request to the same server has to open a new connection.
You could use anynchronous HTTP requests if you don't care about the responses (that way your worker thread will not have to wait for the response to come back). See http://www.javaworld.com/javaworld/jw-03-2008/jw-03-asynchhttp.html for some details on Asynchronous/Synchronous HTTP queries in Java. Then you can control if the anychronous thread does or does not handle the response (or any failure flagged on the communication) - as long as there were no TCP level failures on the request the connection will still be opened.
You can't control whether or not the server returns a response. Your code is free to ignore any response it receives.
It's pretty hard to not get responses because they're part of the HTTP protocol. but you can certainly ignore the responses.

Categories

Resources