Context
I am receiving a sync request from an external client to my spring boot rest endpoint(/request)
My rest service makes a sync call to Microservice A
After A is finished executing it publishes an event & then returns a response to my rest client
The event published by A is being consumed by another microservice B which performs some transaction and sends a response to the restClient endpoint /success indicating a success response
Problem
How do I make my /request endpoint wait until /success has been hit before returning a response to the client.
Use Semaphore for your desired endpoint and acquire() when needed.
https://www.geeksforgeeks.org/semaphore-in-java/
Related
I dont mean supporting async responses (like described here https://cloud.spring.io/spring-cloud-contract/reference/html/project-features.html#contract-dsl-async).
I mean situation when after my request I get response synchronously. And after that I want to get some request back from stub. Smth like this:
request from my service to stub:
POST /subscription {subscription params...}
synchronous response from stub:
202 Accepted
after a while request from stub to my service:
POST /subscription-answer {subscription with more params...}
Consider the endpoint as "abc" and method as "POST".
I need JSON request and response using Publish/Subscribe in JMS for asynchronous call.
I've been working on a Spring (backend) and ExtJS (UI) application. I am using Spring Data REST to reduce code, with just an interface am able to do CRUD on my web endpoints. The hard nut is getting my event handlers to send JSON response back to my client after an event such as before and after crud. My handlers are registered and working since events are being logged on the console. Now I need to send such a response if create bank is successful
{success:true, 'msg':'Bank created successfully '
Help?
If you take a look at fundamentals, your code would depend on the http response status codes and headers after creation/update/delete requests. Note the headers for resources created. Optionally you could configure to return the content ( RepositoryRestConfiguration.returnBodyOnCreate & RepositoryRestConfiguration.returnBodyOnUpdate) using if you prefer.
I have a lightweight Java application exposing a web service through Jersey/Grizzly. It's fairly simple as it just sends back JSON content:
#GET
public Response status() {
CacheControl cc = new CacheControl();
cc.setMaxAge(CLIENT_EXPIRY);
cc.setPrivate(true);
ResponseBuilder builder = Response.ok(someJsonString, MediaType.APPLICATION_JSON);
builder.cacheControl(cc);
return builder.build();
}
I would like to perform server-side operations when and only when a client requests this response, but without delaying the response itself.
These other methods will not influence the response, but will use up server resources, and take a significant time. That would make the end user experience less enjoyable if I simply pasted the call in the middle of response building.
What would be a good way to monitor the endpoint activity and trigger a server-side treatment without delaying the response?
Start a new thread that will do the work, and then return the response to the client.
Consider using a thread pull, not to exhaust your server's resources.
This way the client will not wait for response and the task will be executed.
HTTP controller (Spring) gets a requests and making a blocking call. Once the blocking call is over the controls get back to the controller.
At this point I don't want to send a response back to the client but rather wait for another event(e.g., completion of some processing) to happen. As soon as that event happens I need a way to collect the data from the event and then return the HTTP response with this data.
HTTPController doSomething( HTTPRequest )
{
makeBlockingCall();
waitForEventToHappen();
collectDataFromEvent();
return HTTPResponse();
}
You wouldn't do that from within one request, because that would probably time out eventually and until then wouldn't provide feedback to the customer that something is happening.
Instead, you would immediately show a page that asynchronously (probably per AJAX) polls the server to see whether the result is already available.
So the first request will return an id that will be used in the second (AJAX) request to lookup the result.