I have 2 applications which are both rest API's built in spring. Need is to use asyn processing , will make call from APP1 to APP2 using RestTemplate and APP2 will immediately sends response back to APP1 saying "Request submitted". But in the background APP2 will still be running as we are using DeferredResult. Once the processing is finished does DeferredResult will send the response back to APP1 to do further processing say "Job finished " or "Job Error" something like this?
Related
We have an asynchronous application meaning we send a request to an API that accepts the request immediately and returns 200. The server then processes the request and calls back an API with the results of the above processing. Example :
Make a call to Server A on POST /order
Server A calls POST /processor/order on Server B which creates an Order.
Server B processes the Order
Server B calls back URL POST /order/callback/{1} on Server A
Server A processes the response
I tried to write an integration test for this using Spring Boot Tests and WireMock Webhooks which looks something like this :
#Test
void shouldReturnCallbackAndProcessOrder() {
// test setup ...
wm.stubFor(post(urlPathEqualTo("/processor/order"))
.willReturn(ok())
.withPostServeAction("webhook", webhook()
.withMethod(POST)
.withUrl("http://my-target-host/order/callback/1")
.withHeader("Content-Type", "application/json")
.withBody("{ \"result\": \"SUCCESS\" }"))
);
...some assertions here...
Thread.sleep(1000);
}
I need to put this line Thread.sleep(1000); at the end of the test to wait for the webhook to call the Server A. Is there a better way to do this?
I would suggest using Awaitility, which allows you to wrap a checking operation inside a lambda and it will be repeatedly checked until it's passed or a timeout is reached e.g.
await().atMost(5, SECONDS).until(/* fetch some value */, is("expected value"));
WireMock uses this for its own async testing: https://github.com/wiremock/wiremock/blob/25b82f9f97f3cc09136097b249cfd37aef924c36/src/test/java/com/github/tomakehurst/wiremock/PostServeActionExtensionTest.java#L85
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.
I am using the ResponseBodyEmitter in Spring 4.2 RC1 for sending JSON object as part of the server send events. I am able to see the response in web browser. Is there any spring client for the same ?
I tried using AsyncRestTemplate, but I am getting all the response together. Ie if I am sending 3 objects one after another using ResponseBodyEmitter , using AsyncRestTemplate, I get all of them together. I would like to receive in client as and when the server sends it.
You could give a go to Reactor HTTP client if you're into async and reactive programming: https://github.com/reactor/reactor/blob/master/reactor-net/src/test/groovy/reactor/io/net/http/HttpSpec.groovy#L74.
Like it's mentionned on the title, I'm trying to create an Angular service that will be used like a listenner on the backend, It will check if the backend finished calculating a list process and return what is not finished yet.
For example: reexecute the verification every 5 seconds while the user is still on the application interface.
Part of the difficulty of doing this will be to have your backend report the progress of its calculation, but, assuming you have that figured out, the frontend portion seems like it could be fairly straightforward.
Your service could, e.g., with the $http service, make repeated requests to some backend controller until the backend returns a sentinel to signify it has completed calculating.
The frontend program flow would be like this:
Make request to backend
Wait for response
When response is successfully received:
Update frontend scope/vars/view/whatever with received data
If response contains completed sentinel, stop making requests
If response doesn't contain completed sentinel, repeat this process (after some delay)
I made a JSFiddle with a very simple example of what I mean. Every three seconds it makes another request to the backend (using the $timeout service).
In my project there is a requirement to create a job which will send multiple emails.. I was thinking to use Spring mail API. I checked there is a method
send(MimeMessage[] mimeMessages)
Which will send emails in batch, I have a question, if any of the mail failed while sending, will the whole job fails or rest of them will be sent? Is it possible to get a result that which one is successful and which one failed?
Have a look at https://github.com/SpringSource/spring-framework/blob/master/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java.
Messages are sent individually and the send(MimeMessage[]) method throws MailSendException which contains the messages that failed to go.