1

We've had some issues with AsyncServlets, and so we looked into SSE and WebSockets.

All the tomcat examples of SSE look like:

@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //content type must be set to text/event-stream
        response.setContentType("text/event-stream"); 
        //cache must be set to no-cache
        response.setHeader("Cache-Control", "no-cache");     
        //encoding is set to UTF-8
        response.setCharacterEncoding("UTF-8");

        PrintWriter writer = response.getWriter();

        for(int i=0; i<10; i++) {
            System.out.println(i);
            writer.write("data: "+ i +"\n\n");
            writer.flush();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        writer.close(); 
    }
}

However, I want to keep the connection for long running periods, hopefully without having to keep the thread handling the request alive. Here's an example of receiving information from a PubSubHub and wanting to push it to browsers.

@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //content type must be set to text/event-stream
        response.setContentType("text/event-stream"); 
        //cache must be set to no-cache
        response.setHeader("Cache-Control", "no-cache");     
        //encoding is set to UTF-8
        response.setCharacterEncoding("UTF-8");

        final PrintWriter writer = response.getWriter();

        PublishSubsribeHub.getInstance().subscribe("TestData", new Callback(String msg){
           public void run(){
               writer.write("data:"+msg);
               writer.flush(); // this throws NullPointerException since undeline HttpServletResponse is closed
           });
}

Like mentioned in the comment above, at the point data is ready and received through PublishSubsribeHub, the connection is already closed.

The way to "fix" this is to use response.startAsync() which keeps the connection open.

However, our problems were exactly with this mechanism "asyncContext" / "asyncServlet". So wondering (before I switch to Tomcat's WebSocket impl) if there is any other way to support async push messages using Tomcat8.5 or Tomcat9, maybe using HTTP/2 spec.

Thank you

1 Answer 1

0

If it is feasible in the context of your project, consider using jersey library in order to enable SSE functionality.

Jersey is a popular, easy-to-use and well documented solution. Additionally, the documentation contains clear examples of how to use the libraries. You can find such an example below.

Broadcaster example

import org.glassfish.jersey.media.sse.SseBroadcaster;

@Singleton
@Path("broadcast")
public static class BroadcasterResource {
    private SseBroadcaster broadcaster = new SseBroadcaster();

    @POST
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.TEXT_PLAIN)
    public String broadcastMessage(String message) {
        OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
        OutboundEvent event = eventBuilder.name("message")
            .mediaType(MediaType.TEXT_PLAIN_TYPE)
            .data(String.class, message)
            .build();

        broadcaster.broadcast(event);

        return "Message '" + message + "' has been broadcast.";
    }

    @GET
    @Produces(SseFeature.SERVER_SENT_EVENTS)
    public EventOutput listenToBroadcast() {
        final EventOutput eventOutput = new EventOutput();
        this.broadcaster.add(eventOutput);
        return eventOutput;
    }
}

This specific example uses a broadcaster which:

  • Allows a client to subscribe through the 'listenToBroadcast' method.
  • Broadcast a message to every subscribed client with the 'broadcastMessage' method.

Running the example

In order to run the example, follow these steps:

  • Create a new package for SSE classes
  • In this package create a class based on the above Broadcaster example
  • Add another class capable of auto-discovering (and registering) every class which exists in the same package:

-

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("api")
public class ApiConfig extends Application { 

}

The above solution was tested with Tomcat 7, 8, 8.5. The jersey version used: 2.x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.