Jersey,osgi和Servlet上下文3.x.

时间:2013-11-03 22:32:21

标签: java servlets jersey osgi

我正在使用Jersey 2.x和OSGI开发一个应用程序。 要注册jersey的终点(ServletContainer),我使用osgi的HTTPService和Jetty 8.x的实现 我想使用SSE(服务器发送事件),但不幸的是,当我使用实现它的服务时,告诉我 这不会在Servlet上下文2.x中运行 奇怪的是我使用的堆栈是servlet 3.x. 如果我使用SSE支持创建一个简单的servlet,无论JAX-RS如何都可以。 我认为Jersey开始认为在servlet容器2.x上运行,但不是如何改变它。 我没有任何web.xml,因为它是本机应用程序OSGI。

这是OSGI的简单初始化:

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

其中servletContext是ServletContainer的一个实例。

然后是ServletContainer实例,我们添加动态重新加载应用程序的JAX服务。

有什么建议吗?

谢谢....

编辑......

这些是一些捆绑包:

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)

这是一个例外:

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]

示例:

注意:我使用Peaberry代替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:

单件 公共类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);
}

}

ConfigHttpService.init()方法是从代表内部模块的其他类调用的。在此方法中,您将看到JAX ServletContext的配置。

2 个答案:

答案 0 :(得分:2)

错误是,泽西模块的启动与注册ServletContainer类的应用程序模块处于同一级别。

[ 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)

我在30级打开了一叠球衣,80级的应用程序开始运行!!!!重要的模块是jersey-core-server,它增加了对servlet 3.x的支持。

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

请参阅my repository

中的示例

答案 1 :(得分:1)

我认为我们可能有更好的解决方案,而不是直接使用HttpService,你可以使用WhiteBoard扩展器(只需要为此安装http-whiteboard功能)。这样您就可以将Servlet注册为服务,而白板扩展器会将其注册,另请参阅pax-web samples之一。或者,当您正在开发Servlet 3.0 Servlet时,只需通过注释Servlet来注册它。您需要做的就是将一个web-contextPath添加到Bundle的Manifest中。另请参阅我的blog

如果这仍然没有帮助,我可能需要将一个工作样本发送到ops4j邮件列表,或者向pax-web添加一个问题,以便我可以深入了解它。 BTW。 CXF确实使用白板方法并且像魅力一样工作;)