tomcat7jersey-2.0broadcastserver-sent-eventseventsource

Jersey SSE not working with Tomcat 7


Frustrated, i am posting this question. I am sure I have surfed enough to know that the proper answer is not discussed so far. But if the case is the contrary, please accept my Apologies and share with me the apt forum link.

I am testing the concepts of SSE broadcast using Jersey SSE API.

I am not using Maven or anything. Because the pet project i am working on, was started way before i became familiar with Maven, hence i manually add Jar files to the Web-Inf lib folder

I have the following relevant jars-

2/28/2016  06:58 PM            85,147 javax.json-1.0.4.jar
12/28/2016  06:58 PM            85,353 javax.servlet-api-3.0.1.jar
12/28/2016  06:58 PM           115,534 javax.ws.rs-api-2.0.1.jar
12/28/2016  06:58 PM           100,146 jaxb-api-2.2.7.jar
12/28/2016  06:58 PM           117,090 jersey-client-2.2.jar
12/28/2016  06:58 PM           600,514 jersey-common-2.2.jar
12/28/2016  06:58 PM            52,819 jersey-container-servlet-core-2.2.jar
12/28/2016  06:58 PM            18,107 jersey-container-servlet.jar
12/28/2016  06:58 PM           971,310 jersey-guava-2.24.1.jar
12/28/2016  06:58 PM            30,495 jersey-media-sse.jar
12/28/2016  06:58 PM           801,094 jersey-server-2.2.jar

My web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id="WebApp_ID" version="3.0">
   <display-name>SDTP-SS-UK-ERP</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      </filter-class>
       <async-supported>true</async-supported>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/struts/*</url-pattern>
   </filter-mapping>

    <!-- Jersey Support Configuration for RestFul WebServices -->
    <servlet>
         <servlet-name>jersey-serlvet</servlet-name> 
        <!-- <servlet-name>jersey-servlet</servlet-name>  -->
        <servlet-class>
            org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
         <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.rest.services,com.sse.services</param-value>

         </init-param>
        <!--  
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        -->
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
         <servlet-name>jersey-serlvet</servlet-name> 
        <!--   <servlet-name>jersey-servlet</servlet-name> -->
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

My Test SSE registration file. I use this to register for the SSE:

     @Singleton
@Path("/sse")
public class SseTest extends ResourceConfig
{


    public SseTest() {
        super(SseTest.class, SseFeature.class);
    }

    public SseBroadcaster BROADCASTER = new SseBroadcaster();

     @GET
     @Path("/getSSE")
     @Produces(SseFeature.SERVER_SENT_EVENTS)
     public EventOutput getMessageQueue() 
     {
         EventOutput eventOutput = new EventOutput();

         this.BROADCASTER.add(eventOutput);
         System.out.println(eventOutput.toString());
         return eventOutput;
     }

//After this is registered, i trigger broadcast via another service-

 @GET
 @Path("/test2/{id}")
 @Produces(MediaType.TEXT_PLAIN)
 //@Consumes(MediaType.TEXT_PLAIN)
 public String addMessage(@PathParam("id") final String msg) throws IOException
 {

    String misg= msg+"\\n\\n";
    OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
    OutboundEvent event = eventBuilder.mediaType(MediaType.TEXT_PLAIN_TYPE).data(String.class, misg).build();
    System.out.println(event.getData());
    BROADCASTER.broadcast(event);
    return "Message '" + msg + "' has been broadcast.";
}

Now i will share what happens in Chrome, or firefox:

When i am calling the service getSSE, the browser is registering event-stream and even there is a tab eventStream in the network details with a blank table- id, type, data.

When i call the service /test2/10, it shows 200 OK for that and the response is displayed in the browser.

In the getSSE tab, the first getSSE shows as complete and another getSSE comes up as pending. But the eventsource data is still zero.

After calling test2 twice here is the result:

getSSE 200 eventsource sse.html:-Infinity 149 B 35.41 s
getSSE 200 eventsource Other 149 B 11.0 min
getSSE 200 eventsource Other 144 B Pending

Can someone please point out why i am not getting data? I am not getting any runtime errors in Server End Java.

The server is Tomcat 7.0.69 Java is JDK 1.7

Please help me make this work.. I am losing sleep out of curiosity where i am going wrong


Solution

  • Issue solved

    Problem with Library versions