当前位置: 首页 > 知识库问答 >
问题:

Jersey、osgi和Servlet上下文3。十、

仲学真
2023-03-14

我正在用Jersey 2开发一个应用程序。x和OSGI。为了注册jersey(ServletContainer)的endpoint,我使用osgi的HTTPService和Jetty 8的实现。x我想使用SSE(服务器发送事件),但不幸的是,当我使用实现它的服务时,它告诉我这不在Servlet上下文2中运行。奇怪的是,我使用的堆栈是servlet 3。x、 如果我使用SSE支持制作一个简单的servlet,那么不管JAX-RS如何,它都会运行良好。我认为Jersey开始考虑在servlet容器2上运行。x、 但这不是如何改变的。我没有任何网站。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。

激活剂。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());

                ...
             }
        });
   }
}

配置HttpService.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个答案

公西培
2023-03-14

我认为我们可能有一个更好的解决方案,而不是直接使用HttpService,你可以使用白板扩展程序(只需要为此安装超文本传输协议白板功能)。通过这种方式,您将Servlet注册为服务,白板扩展器将拾取它,另请参阅pax-web示例之一。或者当您正在开发Servlet 3.0 Servlet时,只需通过注释Servlet来注册它。您所需要做的就是向捆绑包的清单中添加一个web-contextPath。请参阅我的博客。

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

邢博学
2023-03-14

错误在于jersey模块与注册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级开始堆叠jersey,在80级开始应用,一切正常!!!!重要的模块是jersey core server,它增加了对servlet 3的支持。十、

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

参见我的仓库中的示例

 类似资料:
  • Web 服务器可以支持多个逻辑主机共享一个服务器 IP 地址。有时,这种能力被称为“虚拟主机”。这种情况下,每一个逻辑主机必须有它自己的 servlet 上下文或一组 servlet 上下文。servlet 上下文不会在虚拟主机之间共享。 ServletContext 接口的 getVirtualServerName 方法允许访问ServletContext 部署在的逻辑主机的配置名字。该方法必须

  • 我知道我需要在servlet上下文中注册用注释的类,以使webapp可访问。通常,我会按照以下方式来做: 我添加到根应用程序上下文中的所有其他配置类。下面是我的dispatcher初始值设定项通常的样子: 但是当我开始使用WebSockets时,事情变得更有趣了。要使websockets工作,必须将WebSoketConfig.class放到servlet上下文中。下面是我的WebSocketCo

  • 我正在阅读spring framework文档,现在我正在讨论应用程序范围概念。关于这个概念的文档是这样说的: 这有点类似于Spring单例bean,但有两个重要的区别:它是每个ServletContext的单例,而不是每个Spring ApplicationContext的单例 对我来说,不清楚的是我们如何想象Spring应用程序上下文和Servlet上下文之间的关系。 那么,Spring应用程

  • 嗨,可以通过传递列表作为参数从服务器下载文件吗 用RestyGWT和Jersey 1.7? 在服务器端,我有以下web服务 当我在浏览器中键入localhost:8080/Url/download时,它可以工作,但我如何使用Resty Gwt或通过窗口下载文件呢。open()? 我想使用POST not GET,这样我可以传递一个可序列化对象的列表,例如:列表文件。 我尝试在客户端使用RestyG

  • 我正在使用aws无服务器java容器将Jersey服务包装到aws Lambda中。我决定使用函数别名进行“测试”和“生产”阶段,最终指向lambda函数的不同版本。 我需要在中选择一些属性。基于该别名的属性文件,基本上是因为我需要与“test”或“prod”DB对话,或者使用不同的endpoint来调用外部web服务。 为此,我需要调用Context对象的方法getInvokedFunction

  • 我将Spring Boot与Jetty一起使用,当我从servletContext获取路径时。getRealPath(“/”)结果如下: 但应该像那样或至少类似于: 还有一个有趣的事实是,如果我有webapp文件夹,jetty会在其中使用这个文件夹,比如servlet context和servlet context。getRealPath(“/”)将返回正常路径。 为什么Jetty对servlet