RESTful Webservice with Jersey in Java and observer pattern

安稳与你 提交于 2020-01-02 18:42:40

问题


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.



来源:https://stackoverflow.com/questions/22761909/restful-webservice-with-jersey-in-java-and-observer-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!