0

I have a Websocket servlet and a Rest servlet. I want to inform the websocket servlet about changes in order to write these "events" via websocket to a server.

I could only find the forward() and include() approach. But they seem to me that they only can forward onGet, onPost, etc.

do I miss something?

4

1 回答 1

1

Indeed, forward() and include() are meant to be used when processing a request. So they might not be the best option given what you want to achieve.

What you could do is create a third component, let's call it EventManager for the time being, and have the Rest servlet signal changes to the EventManager. The websocket, on the other hand, could be notified by the EventManager that new data are available and then get that new data in order to write it back to the client.

In this approach it is essential that both the Rest servlet and the websocket servlet share the same instance of the EventManager. You could achieve that by marking the EventManager as a singleton EJB by adding the @Singleton annotation, and inject it to both the Rest servlet and the websocket servlet.

于 2013-07-23T07:46:02.243 回答