users@jersey.java.net

[Jersey] EventOutput closing prematurely during write()

From: ahar_ <adamh137_at_gmail.com>
Date: Wed, 24 Jun 2015 00:32:17 -0700 (MST)

I have implemented an SSE Restful web interface for publishing messages
received from an internal JMS queue out to external clients. I have managed
to get a test message out to a Java client, but the Thread throws a null
pointer exception before completing the write() execution, closing the
connection and preventing further communication.

*Here is my resource class:*

@GET
@Path("/stream_data")
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput getServerSentEvents(@Context ServletContext context){
    final EventOutput eventOutput = new EventOutput();
    MService mService = (MService) context.getAttribute("instance");
    new Thread( new ObserverThread(eventOutput, mService) ).start();
    return eventOutput;
}

*And here is my thread's run method:*

public class ObserverThread implements Observer, Runnable {
   //constructor sets eventOutput & mService objects
   public void run() {
    try {
        String message = "{'symbol':'test','entryType'='0','price'='test'}";
        Thread.sleep(1000);
        OutboundEvent.Builder builder = new OutboundEvent.Builder();
        builder.mediaType(MediaType.APPLICATION_JSON_TYPE);
        builder.data(String.class, message);
        OutboundEvent event = builder.build();
        eventOutput.write(event);
        System.out.println(">>>>>>SSE CLIENT HAS BEEN REGISTERED!");
        mService.addObserver(this);
        while(!eventOutput.isClosed()){
            if(!updatesQ.isEmpty()){
                pushUpdate(updatesQ.dequeue());
            }
        }
        System.out.println("<<<<<<<SSE CLIENT HAS BEEN DEREGISTERED!&quot;);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

&lt;b>Here is my client code:*

Client client =
ClientBuilder.newBuilder().register(SseFeature.class).build();
    WebTarget target = client.target(url);
    EventInput eventInput = target.request().get(EventInput.class);
    try {
        while (!eventInput.isClosed()) {
            eventInput.setChunkType(MediaType.WILDCARD_TYPE);
            final InboundEvent inboundEvent = eventInput.read();
            if (inboundEvent != null) {
                String theString = inboundEvent.readData();
                System.out.println(theString + "\n");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
I am getting the "{'symbol':'test','entryType'='0','price'='test'}" test
message printed to the client console, but the server then prints a
NullPointerException before it can print the ">>>>SSE Client registered"
message. This closes the connection so the client exits the while loop and
stops listening for updates.

I am not sure why the nullpointer is being thrown within the write method,
it seems to be something related to flushing the buffer to await another
chunk, shown in the stack trace:

Exception in thread "Thread-20" java.lang.NullPointerException
at
org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:741)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:434)
at
org.apache.coyote.http11.InternalOutputBuffer.flush(InternalOutputBuffer.java:299)
at org.apache.coyote.http11.Http11Processor.action(Http11Processor.java:981)
at org.apache.coyote.Response.action(Response.java:183)
at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:314)
at org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:288)
at
org.apache.catalina.connector.CoyoteOutputStream.flush(CoyoteOutputStream.java:98)
at
org.glassfish.jersey.message.internal.CommittingOutputStream.flush(CommittingOutputStream.java:292)
at org.glassfish.jersey.server.ChunkedOutput$1.call(ChunkedOutput.java:241)
at org.glassfish.jersey.server.ChunkedOutput$1.call(ChunkedOutput.java:192)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:242)
at
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:345)
at
org.glassfish.jersey.server.ChunkedOutput.flushQueue(ChunkedOutput.java:192)
at org.glassfish.jersey.server.ChunkedOutput.write(ChunkedOutput.java:182)
at com.bpc.services.service.ObserverThread.run(MarketObserverThread.java:32)
at java.lang.Thread.run(Thread.java:745)




--
View this message in context: http://jersey.576304.n2.nabble.com/EventOutput-closing-prematurely-during-write-tp7583356.html
Sent from the Jersey mailing list archive at Nabble.com.