Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Works as designed
    • Affects Version/s: 2.0
    • Fix Version/s: None
    • Component/s: media, osgi
    • Labels:
      None
    • Environment:

      Windows 7, Eclipse Kepler

      Description

      I'm trying to implement a simple Java Sse server using Jersey on OSGi.
      I've followed the example described in the Jersey official guide:

      https://jersey.java.net/documentation/latest/sse.html#d0e8183

      but when I try to call the server, it says: "MessageBodyWriter not found for media type=text/event-stream, type=class org.glassfish.jersey.media.sse.OutboundEvent, genericType=class org.glassfish.jersey.media.sse.OutboundEvent."

      I've found some other similar issues in which all say to register SSE feature, but in OSGi there is not a main in which I can register it, and, moreover, the SseFeature doesn't provide any service!
      How can i register the sse feature? Or: what can I do to fix the problem?

      PS. To work in OSGi, Jersey need the connector:
      https://github.com/hstaudacher/osgi-jax-rs-connector

      Here I will paste the code you should need to help me!

      MANIFEST.MF

      Manifest-Version: 1.0
      Bundle-ManifestVersion: 2
      Bundle-Name: RestServerSSE
      Bundle-SymbolicName: it.prova.sse
      Bundle-Version: 1.0.0.qualifier
      Bundle-RequiredExecutionEnvironment: JavaSE-1.7
      Import-Package: javax.ws.rs;version="2.0.0",
       javax.ws.rs.core;version="2.0.0",
       javax.ws.rs.ext;version="2.0.0",
       org.glassfish.jersey.media.sse;version="2.0.0",
       org.glassfish.jersey.message;version="2.0.0",
       org.glassfish.jersey.server;version="2.0.0",
       org.osgi.framework;version="1.7.0"
      Service-Component: OSGI-INF/component.xml
      

      component.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="it.prova.sse">
         <implementation class="it.prova.sse.Server"/>
         <service>
            <provide interface="it.prova.sse.Server"/>
         </service>
      </scr:component>
      

      Server.java

      package it.polito.server.rest.sse;
      import java.io.IOException;
      import javax.ws.rs.GET;
      import javax.ws.rs.Path;
      import javax.ws.rs.Produces;
      import org.glassfish.jersey.media.sse.EventOutput;
      import org.glassfish.jersey.media.sse.OutboundEvent;
      import org.glassfish.jersey.media.sse.SseFeature;
      import org.glassfish.jersey.server.ResourceConfig;
      
      @Path("/sse")
      public class Server
      {
      public Server()
      {
          final ResourceConfig resourceConfig = new ResourceConfig(Server.class,SseFeature.class);
      }
      	
        @GET
          @Produces(SseFeature.SERVER_SENT_EVENTS)
            public EventOutput getServerSentEvents() {
      		
              final EventOutput eventOutput = new EventOutput();
              new Thread(new Runnable() {
                @Override
                  public void run() {
                     try {
                     for (int i = 0; i < 10; i++) {
                     // ... code that waits 1 second
                     final OutboundEvent.Builder eventBuilder
                     = new OutboundEvent.Builder();
                     eventBuilder.name("message-to-client");
                        eventBuilder.data(String.class,
                            "Hello world " + i + "!");
                            final OutboundEvent event = eventBuilder.build();
                            eventOutput.write(event);
                        }
                     } catch (IOException e) {
                        throw new RuntimeException(
                            "Error when writing the event.", e);
                     } finally {
                        try {
                            eventOutput.close();
                        } catch (IOException ioClose) {
                            throw new RuntimeException(
                                "Error when closing the event output.", ioClose);
                        }
                    }
                 }
              }).start();
              return eventOutput;
          }
      }
      

        Activity

        Hide
        Michal Gajdos added a comment -

        This is not a bug. You're configuring wrong ResourceConfig. You should configure the one that is used during deployment time and not the one that you create in a resource (please consult documentation to osgi connector how to do that).

        Show
        Michal Gajdos added a comment - This is not a bug. You're configuring wrong ResourceConfig . You should configure the one that is used during deployment time and not the one that you create in a resource (please consult documentation to osgi connector how to do that).

          People

          • Assignee:
            Michal Gajdos
            Reporter:
            lifedj
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: