RESTful Webservice with Jersey in Java and observe

2019-05-25 01:02发布

问题:

I'm trying to implement an n-tier architecture application for a school project. The communication between the clients and the server is done with a RESTful Webservice. I Used Jersey to implement this in Java. The only question I have is, how to register the clients at the server to receive notification of changes (like normally done with the observer pattern or listen/publish pattern). Do i need to implement a service on the client side aswell and then register the URL to be called by the server and then call these URLs when changes occur?

Thanks for your help!

回答1:

Typically, in http based client-server scheme, server always responds to a request. There is no way server can send anything (notification) without client request it. But there are use cases for server pushing the data to client without client requesting and hence Server-Push came into picture.

Jersey provides Server Push in the form of Server-Sent Events (SSE). The documentation says,

Server-Sent Events (SSE) is a mechanism that allows server to asynchronously push the data from the server to the client once the client-server connection is established by the client

Please note that the client needs to establish connection with the server first. You might call that first client request as Registration. Once the connection is established, the server can send/push data to client. The linked mention also has a working example as well.