How can a particular thread be notified in Spring Boot

354 Views Asked by At

I have a REST service defined in Spring Boot, which exposes a synchronous REST API, to be invoked by a user from a Web based UI. The service is expected to provide a real time response.

The service talks to an external endpoint in an asynchronous fashion. That is: - A single one - way outbound message for the request - A single one - way inbound message for the response

How can I combine the two messages to provide an impression of a synchronous behavior? I am thinking of a few approaches like:

  • The Rest Service posts a request to the endpoint. Once the endpoint responds, the response is added to a ConcurrentHashMap. The Rest Service queries the HashMap every few milliseconds and once it finds the right response it exits with a valid HTTP reason code. It is akin to polling and I am thinking if we can avoid that.

  • The Rest Service posts a request to the endpoint. Once the endpoint responds, the waiting thread in the Rest Service is notified. However the waiting thread should conclude only if the right response is received (i.e. matching correlation Ids etc.) Is this possible?

I realize that this is similar to a JMS Queue Request Response scenario, where each JMS queue request opens up a listener on the response queue with a message selector criteria. However in this case I have to implement this using HTTP.

Any thoughts on this are welcome. I am fully convinced I am missing something very basic, but am not sure what.

Thanks a lot!

0

There are 0 best solutions below