Search code examples
servletsserver-sent-events

Servlet with Asynchronous method, servlet side event


I have a servlet that implements a class. The class tells my servlet to implement an asynchronous messageReceived method.

Every now and again the messageReceived method is called by a different process than the server container. How can I send a response back to the client to indicate that this method has been called.

I was able to implement the HTML5 servlet sent events using the doGet method and send a response to the client, but how do I return a response indicating that the messageReceived method was called.

Apologies if I am not being clear, I will add some code examples when I get time.


Solution

  • The answer from user user3218114 regarding polling the server is probably the simplest way. i used the context to store the value temporarily when the asynchronous method is called. Then when the dopost method is polled by an ajax javascript call, it retrieves the value from the context.

    I also investigated Websockets in Tomcat 8 and Server side events which probably could handle this problem as well, but due to time constraints I was not able to get it working.