javaee / jersey

This is no longer the active Jersey repository. Please see the README.md
http://jersey.github.io
Other
2.86k stars 2.35k forks source link

Using Sse with Jersey in OSGi #2515

Closed glassfishrobot closed 10 years ago

glassfishrobot commented 10 years ago

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;
    }
}

Environment

Windows 7, Eclipse Kepler

Affected Versions

[2.0]

glassfishrobot commented 10 years ago

Reported by lifedj

glassfishrobot commented 10 years ago

michalgajdos said: 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).

glassfishrobot commented 10 years ago

Was assigned to michalgajdos

glassfishrobot commented 7 years ago

This issue was imported from java.net JIRA JERSEY-2243

glassfishrobot commented 10 years ago

Marked as works as designed on Monday, November 25th 2013, 6:44:30 am