How can i return response before job is'nt done in spring - spring

I would make a simple restful api in spring boot.
This api trigger a hard job that takes about 20 min.
Request is performed by another spring boot project.
And request's wanted is only to trigger, not return data.
I can't wait end of this job. So I have to separated api call and 20 minute job.
I think that async is not way...
How can i do this?
Only way is to use a message broker?

Related

Best approach to run a Spring Batch job

I have a requirement where I have to call a spring batch job from a rest endpoint. We are using an API which has all the boilerplate code for running a job and it also adds an endpoint to our service and upon calling that endpoint it will run the requested job. I have attached a snip of that endpoint.
Please suggest a best approach to run the job from another rest endpoint within the same service should I call /Jobs endpoint or should I implement the logic of running the required job? TIA.
There is no best approaches, it depends on the use case. I would not make REST endpoints call each others, I think it would be simpler to inject the JobLauncher in your controller and call your job accordingly. You can find more details and a code example in the reference documentation here: Running Jobs from within a Web Container.

How to make Spring Boot REST controller asynchronous?

My application is simple 3-tier Spring Boot rest web-service with usual synchronous endpoints.
But since the period of getting response from downstream system where my service sends requests is quite long (kind of 60 seconds), I need to add support of asynchronous REST calls to my service to save upstream systems from a response awaiting. In other words, if a response to a downstream system is going to take more than 60 seconds (timeout), then the upstream system break the connection with my service and keeps its doing...
But when the response come, my service using "reply-to" header from the upstream system will send the response to the upstream system.
All the things above are kind of call back or webhook.
But I didn't find any examples of implementation.
How to implement this mechanism?
How can I find more information?
Does Spring Boot have something to implement it out-of-box?
Thank you for attention!
You can use the #Async annotation from Spring. You will also need to enable in your application this by setting #EnableAsync.
An important note is that your method with the #Async annotation should be on a different class from where it is being called. This will let the Spring proxy intercept the call and effectively do it asynchronous.
Please find here the official tutorial.

Is there a good practice to have cron jobs in a spring webflux app?

For a while now, our team started to work on a reactive new micro-service. The service is mainly using spring-webFlux and other cool reactive futures.
At this point there should be created a cron-job triggered every 10 secs(that has to verify some transactions state), but I'm afraid that this will affect the load of the app and also is not compatible with the reactive concurrency model since the #Scheduled job when will be triggered it's going to consume one thread until it ends.
At this point I'm more in favor of an aws-lambda that it's going to call an endpoint of our app.
Do you have any better suggestions or advices ?
You can't schedule an AWS Lambda to run more often than once a minute. If you need an event to occur every 10 seconds in Spring I think using #Scheduled is your best option.

Spring #Retryable with configurable delay based on client

I am developing a Spring boot application. From my application we are calling an external RestService and want to retry if RestCall fails for any reason.
I know that #Retryable within spring should take care of this. I am trying to implement something like below with parameters:
#Retryable(maxAttempts=3,value=RunTimeException.class,backoff = #Backoff(delay = 900000))
Now my problem is, there are multiple clients who will call this service. When client1 is calling this service, i want to retry when a failure occurs after 15min. When client2 is calling this service, i want to retry when a failure occurs after 1sec.
So i want to know if there is any way we can configure the delay in retryable based on client1 or client2.
Any suggestions are helpful.
You can use a ThreadLocal<Long> #Bean to store the delay and then use delayExpression="#threadLocalBean.get().
However, suspending a thread for 15 minutes is a bit severe.
You would be better using a scheduler for the retries in that case.

How to send data to multiple servers in spring boot micro service?

I have requirement something like this:
once the request is received by my service, i need to send it 2-3 third party servers at a time and get the response from all server and return the response.
How can I achieve that?.
My thought : I can create separate threads for different servers and send the request to all servers parallely, but here the issue is, how I will come to know the threads are finished and consolidate the response from all servers and return to caller.
Is there any other way to do in spring boot(micro service)?.
You can leverage asyn feature supported by Spring Framework. Let's see the folllowing example which issue multiple calls in Async style from Spring's official guides:Async Method
Another possible solution for internal communications between Microserives is to use the message queue.
You didn't specify what type of services are you consuming. If they are HTTP, you may want to use some Enterprise Integrations abstractions (most popular are Spring Integration and Apache Camel).
If you don't want to introduce message bus solution into your microservice, you may want to take a look at AsyncRestTemplate

Resources