Timothy Rajan
Timothy Rajan

Reputation: 1956

JAX-RS - synchronization and Threading

I am developing a bunch of Restful APIs (1 POST and 1 GET) using JAX-RS. The post method insert some rows in a table while the GET method fetch the inserted data in addition to some more data from DB.

The issue i am facing is the GET method needs to get executed immediately after the POST method. That's the business requirement and the GET method queries the DB for some data. But the POST method is not yet completed and the GET method is invoked.

Is there a way to pause the GET method till the moment the POST method is complete though the GET method is invoked from the client.

Could anyone please share the code snippet to accomplish this task. Thanks

Upvotes: 2

Views: 3120

Answers (1)

Vineet Reynolds
Vineet Reynolds

Reputation: 76719

Asynchronous HTTP request processing would help here, provided you know the conditions under which the HTTP GET request processing must be paused.

Since the POST and GET requests are processed by two different threads, you'll to ensure that the thread processing the GET request is informed of completion of processing of the POST request. This could be achieved via some sempahore.

Asynchronous HTTP request processing although available in Servlet 3.0 is not available in a standardized manner in JAX-RS 1.1. It is however a part of JAX-RS 2.0 (Java EE 7), available via the @Suspended annotation. If you want to use JAX-RS 1.1 (aka Java EE 6), you may need to rely the provider capabilities; for instance, RESTEasy supports async HTTP request processing via the @Suspend annotation for Java EE 6 apps.

Upvotes: 2

Related Questions