简体   繁体   中英

Java in web services return response after threshold time reaches and Continue execution after that

In Java in a web service, I have a requirement I want to return the response to the user after configured threshold time reaches and wants to continue processing after that.

Let's say I have a service it does step1, step 2, and the configured threshold is 1 second. Let's say step1 is completed at 1 second I want to return an acknowledgment response to the user and continue processing with step2 and wants to store response in DB or something like that.

Please let me know if anyone has any solutions or thoughts on this problem

There are multiple ways to achieve this

HTTP Layer

On HTTP layer, if the response comes back before the threshold, then I'd be tempted to send back a 200 Success .

However, if it takes more time than the threshold, you could use 202 Accepted

Looking at the RFC , its use case looks like this

6.3.3. 202 Accepted

The 202 (Accepted) status code indicates that 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 in HTTP for re-sending a status code from an asynchronous operation.

The 202 response is intentionally noncommittal. 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 representation sent with this response ought to describe the request's current status and point to (or embed) a status monitor that can provide the user with an estimate of when the request will be fulfilled.

Now, of course, instead of having a mix of 200 and 202 , you could just return 202 everytime

Application Layer

In your application layer, you'll typically want to make use of asynchronous processing for this purpose.

There are multiple ways to leverage this way of working, you can:

  • Post a message on a queue/topic and let a message broker take care of dispatching it to another part of the app, or another app and let this part do the processing
  • Save the request inside of a database, and have another service poll the database for new requests, similar to queueing explained above, without JMS
  • If you're using Java EE, your EJB container allows you to work with @Asynchronous which will call a method asynchronously and return (so you'll be able to return 202 )
  • If you're using Spring, it has an @Async annotation for the same purpose as hereabove

There are definitely other methods you could use to achieve this use case, but I think the ones I presented are the most common ones

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM