1

Jersey 2.x と OSGI を使用してアプリケーションを開発しています。jersey(ServletContainer)のエンドポイントを登録するために、Jetty 8.xの実装でosgiのHTTPServiceを使っていますSSE(Server Sent Event)を使いたいのですが、残念ながらそれを実装しているサービスを使うと、これはサーブレット コンテキスト 2.x では実行されません 。奇妙なことに、私が使用しているスタックはサーブレット 3.x です。SSE をサポートする単純なサーブレットを作成すると、JAX-RS に関係なくうまくいきます。Jersey はサーブレット コンテナー 2.x で動作することを考え始めていると思いますが、それを変更する方法は考えていません。ネイティブ アプリケーションの OSGI であるため、web.xml はありません。

これは、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]

例:

注: DI には Peaberry を使用します。

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:

シングルトン 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);
}

}

メソッド ConfigHttpService.init() は、内部モジュールを表す他のクラスから呼び出されます。このメソッドでは、JAX ServletContext の構成が表示されます。

4

2 に答える 2

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 でアプリケーションを開始すると、すべてが機能します!!!! 重要なモジュールは、サーブレット 3.x のサポートを追加する jersey-core-server です。

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

私のリポジトリで例を見る

于 2013-11-06T17:35:38.680 に答える