4

リソースが動的にロードされる OSGI を使用して JAX-RS アプリケーションを開発しています。このためには、プログラムで ServletContainer を登録する必要があります。その後、メソッド ServletContainer.reload (ResourceConfig) を呼び出すことができます。PAX-WEB 3.0.2 と Jersey 2.4 を使用して、サーブレット 3.x をサポートする OSGI 環境で実行しています。問題は、Server Sent Event を実装するリソースにアクセスしたいときに、Jersey が Servlet 2.x で実行されるため、非同期をサポートしないというエラーを発生させることです。

ここで例外:

2013-11-05 00:22:37,675 | WARN  | qtp27902282-62   | 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.
        at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)[224:org.glassfish.jersey.containers.jersey-container-servlet-core:2.3.1]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)[224:org.glassfish.jersey.containers.jersey-container-servlet-core:2.3.1]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)[224:org.glassfish.jersey.containers.jersey-container-servlet-core:2.3.1]
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)[224: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$CachedChain.doFilter(ServletHandler.java:1448)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.ops4j.pax.web.service.internal.WelcomeFilesFilter.doFilter(WelcomeFilesFilter.java:185)[70:org.ops4j.pax.web.pax-web-runtime:3.0.2]
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1419)[60:org.eclipse.jetty.aggregate.jetty-all-server:8.1.9.v20130131]
        at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:455)[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]
Caused by: java.lang.UnsupportedOperationException: Asynchronous processing not supported on Servlet 2.x container.
        at org.glassfish.jersey.servlet.WebComponent$1.suspend(WebComponent.java:120)[224:org.glassfish.jersey.containers.jersey-container-servlet-core:2.3.1]
        at org.glassfish.jersey.servlet.internal.ResponseWriter.suspend(ResponseWriter.java:109)[224:org.glassfish.jersey.containers.jersey-container-servlet-core:2.3.1]
        at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:621)[219:org.glassfish.jersey.core.jersey-server:2.3.1]
        at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:357)[219:org.glassfish.jersey.core.jersey-server:2.3.1]
        at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:347)[219:org.glassfish.jersey.core.jersey-server:2.3.1]
        at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:258)[219:org.glassfish.jersey.core.jersey-server:2.3.1]
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)[220:org.glassfish.jersey.core.jersey-common:2.3.1]
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)[220:org.glassfish.jersey.core.jersey-common:2.3.1]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)[220:org.glassfish.jersey.core.jersey-common:2.3.1]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)[220:org.glassfish.jersey.core.jersey-common:2.3.1]
        at org.glassfish.jersey.internal.Errors.process(Errors.java:267)[220:org.glassfish.jersey.core.jersey-common:2.3.1]
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)[220:org.glassfish.jersey.core.jersey-common:2.3.1]
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:235)[219:org.glassfish.jersey.core.jersey-server:2.3.1]
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:983)[219:org.glassfish.jersey.core.jersey-server:2.3.1]
        at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:359)[224:org.glassfish.jersey.containers.jersey-container-servlet-core:2.3.1]

ServletContainer をさまざまな方法で登録しようとしましたが、何も機能せず、Jersey の登録と設定に問題があると思います。Server Sent Event ベースの非同期サーブレットを記録し、すべてがうまくいったからです。

コード例:

        ServletContainer container = new ServletContainer();
        ...
        ...
        webContainerRef = bc.getServiceReference(WebContainer.class);
        started = webContainerRef != null;
        if (started) {
            final WebContainer webContainer = (WebContainer) bc
                    .getService(webContainerRef);
            if (webContainer != null) {
                // create a default context to share between registrations
                final HttpContext httpContext = webContainer
                        .createDefaultHttpContext();
                // set a session timeout of 10 minutes
                webContainer.setSessionTimeout(10, httpContext);
                // register the hello world servlet for filtering with url
                // pattern
                final Dictionary<String, Object> initParamsServlet = new Hashtable<String, Object>();
                initParamsServlet.put("javax.ws.rs.Application", "com.workingflows.osgi.jaxrs.samples.conf.MyJaxApp");
                webContainer.registerServlet(
                        container, // registered
                        new String[]{"/rest/*"}, // url patterns
                        initParamsServlet, // init params
                        1,
                        true,
                        httpContext // http context
                );

                webContainer.registerServlet(
                        (HttpServlet) new SseServlet(), // registered
                        new String[]{"/servlet/sse"}, // url patterns
                        initParamsServlet, // init params
                        1,
                        true,
                        httpContext // http context
                );
            }
      }
     ...
     ...

ここに私のコードリポジトリがあります。OsgiJersey プロジェクトを参照してください。

4

2 に答える 2

3

クラスパスにモジュールがありますかjersey-container-servlet(これは、サーブレット 3.x のサポートに使用されるはずのモジュールです)。

この問題が発生している理由は、Jersey が(モジュールから) 適切なAsyncContextDelegateProviderを見つけることができず、このように失敗するモジュール (サーブレット 2.x サポート)jersey-container-servletからの実装を使用しているためです。jersey-container-servlet-core

于 2013-11-06T10:26:26.967 に答える
1

エラーは、ジャージ モジュールが、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:29:21.817 に答える