Cristian Rinaldi
Cristian Rinaldi

Reputation: 439

Jersey, osgi and Servlet context 3.x

I am developing an application with Jersey 2.x and OSGI. To register the end point of jersey (ServletContainer), I use the HTTPService of osgi with an implementation of Jetty 8.x I want to use SSE (Server Sent Event), but unfortunately when I use the service that implements it, tells me that this does not run in a Servlet Context 2.x The strange is that the stack I use is servlet 3.x. If I make a simple servlet with SSE support goes well regardless of JAX-RS. I think that Jersey starts thinking that runs on a servlet container 2.x, but not how to change that. I do not have any web.xml because it is a native application OSGI.

This is the simple initialization with OSGI:

httpService.registerServlet ("/ kratos / rest" (HttpServlet) ServletContext, null, null);

where servletContext is an instance of ServletContainer.

Then the ServletContainer instance, we add JAX services dynamically reloading the application.

Any suggestions?

Thanks ....

EDIT ...

These are some of the bundles:

karaf@root> la  | grep Se
[   3] [Active     ] [            ] [    8] OPS4J Pax Logging - Service (1.7.1)
[   5] [Active     ] [            ] [   10] Apache Felix Configuration Admin Service (1.6.0)
[  10] [Active     ] [            ] [   20] Apache Aries Proxy Service (1.0.1)
[  49] [Active     ] [Created     ] [   30] Apache Karaf :: Management :: MBeans :: Services (2.3.3)
[  54] [Active     ] [            ] [   30] Apache ServiceMix :: Specs :: Activation API 1.4 (1.9.0)
[  55] [Active     ] [            ] [   30] Servlet 3.0 (1.0)
[  60] [Active     ] [            ] [   30] Jetty :: Aggregate :: All Server (8.1.9.v20130131)
[  67] [Active     ] [            ] [   30] Apache ServiceMix :: Bundles :: asm (3.3.0.2)
[  69] [Active     ] [            ] [   30] OPS4J Pax Web - Service SPI (3.0.2)
[ 105] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: javax.inject (1.0.0.1)
[ 112] [Active     ] [            ] [   80] ServiceLocator Default Implementation (2.2.0.b10)
[ 121] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: aopalliance (1.0.0.5)

This is a Exception:

013-11-03 20:50:27,708 | WARN  | qtp18651544-59   | ServletHandler                   | pse.jetty.servlet.ServletHandler  492 | 60 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.9.v20130131 | 
javax.servlet.ServletException: java.lang.UnsupportedOperationException: Asynchronous processing not supported on Servlet 2.x container.                                                                                         
Caused by: java.lang.UnsupportedOperationException: Asynchronous processing not supported on Servlet 2.x container.                                                                                                              
        at org.glassfish.jersey.internal.Errors.process(Errors.java:267)[149:org.glassfish.jersey.core.jersey-common:2.3.1]
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)[149:org.glassfish.jersey.core.jersey-common:2.3.1]
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:235)[150:org.glassfish.jersey.core.jersey-server:2.3.1]
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:983)[150:org.glassfish.jersey.core.jersey-server:2.3.1]
        at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:359)[147:org.glassfish.jersey.containers.jersey-container-servlet-core:2.3.1]
        ... 29 more
2013-11-03 20:50:27,710 | WARN  | qtp18651544-59   | ServletHandler                   | pse.jetty.servlet.ServletHandler  517 | 60 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.9.v20130131 | /kratos/rest/sse
java.lang.UnsupportedOperationException: Asynchronous processing not supported on Servlet 2.x container.
        at org.glassfish.jersey.servlet.WebComponent$1.suspend(WebComponent.java:120)[147:org.glassfish.jersey.containers.jersey-container-servlet-core:2.3.1]
        at org.glassfish.jersey.servlet.internal.ResponseWriter.suspend(ResponseWriter.java:109)[147:org.glassfish.jersey.containers.jersey-container-servlet-core:2.3.1]
        at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:621)[150:org.glassfish.jersey.core.jersey-server:2.3.1]
        at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:357)[150:org.glassfish.jersey.core.jersey-server:2.3.1]
        at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:347)[150:org.glassfish.jersey.core.jersey-server:2.3.1]
        at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:258)[150:org.glassfish.jersey.core.jersey-server:2.3.1]
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)[149:org.glassfish.jersey.core.jersey-common:2.3.1]
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)[149:org.glassfish.jersey.core.jersey-common:2.3.1]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)[149:org.glassfish.jersey.core.jersey-common:2.3.1]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)[149:org.glassfish.jersey.core.jersey-common:2.3.1]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:267)[149:org.glassfish.jersey.core.jersey-common:2.3.1]
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)[149:org.glassfish.jersey.core.jersey-common:2.3.1]
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:235)[150:org.glassfish.jersey.core.jersey-server:2.3.1]
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:983)[150:org.glassfish.jersey.core.jersey-server:2.3.1]
        at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:359)[147:org.glassfish.jersey.containers.jersey-container-servlet-core:2.3.1]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)[147:org.glassfish.jersey.containers.jersey-container-servlet-core:2.3.1]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)[147:org.glassfish.jersey.containers.jersey-container-servlet-core:2.3.1]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)[147:org.glassfish.jersey.containers.jersey-container-servlet-core:2.3.1]
        at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:669)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:457)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:69)[71:org.ops4j.pax.web.pax-web-jetty:3.0.2]
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1075)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:210)[71:org.ops4j.pax.web.pax-web-jetty:3.0.2]
        at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:384)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1009)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:77)[71:org.ops4j.pax.web.pax-web-jetty:3.0.2]
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.server.Server.handle(Server.java:368)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:488)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:932)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:994)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:640)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at java.lang.Thread.run(Thread.java:724)[:1.7.0_40]

Example:

Note: I use Peaberry for DI.

Activator.java

public class Activator implements BundleActivator {

   ....
   ....

   @Override
   public void start(final BundleContext bc) throws Exception {

        System.out.println("Starting Activator");

        final ClassLoader primaryClassLoader = getDatanucleusClassLoader(bc);

        Injector injector = Guice.createInjector(Peaberry.osgiModule(bc), new AbstractModule() {
            @Override
            protected void configure() {

                install(new CoreResourcesJaxRsModule());
                ...
                ...
                 bind(HttpService.class).toProvider(Peaberry.service(HttpService.class).single());

                ...
             }
        });
   }
}

ConfigHttpService.java:

Singleton public class ConfigHttpService {

private final ServletContainer servletContext;
private final HttpService httpService;
private final BundleContext context;
private boolean initialized = false;

private static final Logger logger = Logger.getLogger(ConfigHttpService.class.getName());

@Inject
public ConfigHttpService(
        BundleContext bc,
        HttpService httpService, 
        ServletContainer context) {
    this.servletContext = context;
    this.httpService = httpService;
    this.context = bc;
}

public synchronized void init() throws Exception {
    if (!initialized) {
        try {
            logger.info("Configurado el registro de Guice");
            //httpService.registerFilter( guiceFilter, "/.*", null, 0, null );
            try {

                logger.info("Configurado el registro de recursos estaticos");
                httpService.registerResources("/kratos/pages", "/pages", null);

                 logger.info("Servlet /kratos/events configurado");
                 Dictionary d1 = new Hashtable();
                 d1.put("javax.ws.rs.Application", "com.workingflows.kratos.core.service.KratosServiceApplication");

                 httpService.registerServlet("/kratos/rest", (HttpServlet) servletContext, d1, null);




                //httpService.registerResources("/kratos", "/js", null);
            } catch (NamespaceException ex) {
                logger.info("Error NamespaceException");
            }
        } catch (ServletException ex) {
            logger.info("Error ServletException");
        }
        initialized = true;
    }

}

@Stop
public void stop() {
    logger.log(Level.INFO, "Eliminando el servlet {0}", "/kratos/rest");
    httpService.unregister("/kratos/rest");
    httpService.unregisterFilter(guiceFilter);
}

}

The method ConfigHttpService.init() is called from other class that represents a internal module. In this method, you will see the confguration of JAX ServletContext.

Upvotes: 1

Views: 2959

Answers (2)

Cristian Rinaldi
Cristian Rinaldi

Reputation: 439

The error was that the jersey modules were initiated on the same level as the module of the application that registered the ServletContainer class.

[ 127] [  Resolved] [   30] jersey-container-servlet-core (2.4.0)
[ 161] [    Active] [   30] jersey-core-common (2.4.0)
[ 162] [    Active] [   30] jersey-core-client (2.4.0)
[ 163] [    Active] [   30] jersey-core-server (2.4.0)
[ 164] [    Active] [   30] jersey-container-servlet (2.4.0)
[ 165] [    Active] [   30] jersey-media-sse (2.4.0)

I start the stack of jersey in level 30, and the application at level 80 and everything works!!!! The important module is jersey-core-server, that adds support for servlet 3.x.

[ 168] [    Active] [   80] OsgiJersey OSGi Bundle (1.0.0.SNAPSHOT)

See example in my repository

Upvotes: 2

Achim Nierbeck
Achim Nierbeck

Reputation: 5285

I think we might have a better solution for this, instead of using the HttpService directly you either can use the WhiteBoard extender (just need to install the http-whiteboard feature for this). This way you register a Servlet as a service and the whiteboard extender will pick this up, see also one of the pax-web samples. Or as you're developing a Servlet 3.0 Servlet just register it by annotating the Servlet. All you need to do is to add a web-contextPath to the Manifest of the Bundle. See also my blog about this.

If this still doesn't help I could need a working sample sent to the ops4j mailinglist, or add a issue to pax-web so I can take a deeper look at it. BTW. CXF does use the whiteboard approach and works like a charm ;)

Upvotes: 1

Related Questions