admin管理员组

文章数量:1353228

I have an application that uses pax web 8 and jetty 9. (Migrating from pax web 7 to 8) I've set up my servlets with the new whiteboard properties. For example:

        <service-properties>
            <entry key="osgi.http.whiteboard.servlet.pattern" value="/logout/*"/>
            <entry key="osgi.http.whiteboard.servlet.name" value="localLogout"/>
            <entry key="osgi.http.whiteboard.context.select" value="(osgi.http.whiteboard.context.name=customContext)"/>
            <entry key=".eclipse.jetty.servlet.SessionIdPathParameterName" value="none"/>
            <entry key=".eclipse.jetty.servlet.SessionPath" value="/"/>
            <entry key="osgi.http.whiteboard.servlet.asyncSupported" value="true"/>
        </service-properties>
    </service> 

Now a few of these are Resource Mappings as well:

             interface=".ops4j.pax.web.service.whiteboard.ResourceMapping">
            <service-properties>
                <entry key="osgi.http.whiteboard.servlet.name" value="${pkg.name}-resource-mapping"/>
                <entry key="osgi.http.whiteboard.resource.pattern" value="${pkg['context-path']}/*"/>
                <entry key="osgi.http.whiteboard.context.select" value="(osgi.http.whiteboard.context.name=customContext)"/>
            </service-properties>
    </service>

So I went ahead and created a service factory for my ServletContextHelper creation so that the bundleContext would be linked to the bundle registering the servlet ( so it can find the resources in that bundle )

public class Activator implements BundleActivator {

  private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);

  private ServiceRegistration<ServletContextHelper> registration;

  @Override
  public void start(BundleContext bundleContext) throws Exception {
    Dictionary<String, Object> properties = new Hashtable<>();
    properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "customContext");
    properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/");
    //        properties.put(HttpWhiteboardConstants.HTTP_SERVICE_CONTEXT_PROPERTY, "");
    properties.put(Constants.SERVICE_RANKING, Integer.MAX_VALUE);
    registration =
        bundleContext.registerService(
            ServletContextHelper.class,
            new DefaultServletContextHelperServiceFactory(),
            properties);
  }

  @Override
  public void stop(BundleContext bundleContext) throws Exception {
    if (registration != null) {
      registration.unregister();
      registration = null;
    }
  }

  private static class DefaultServletContextHelperServiceFactory
      implements ServiceFactory<ServletContextHelper> {

    @Override
    public ServletContextHelper getService(
        Bundle bundle, ServiceRegistration<ServletContextHelper> registration) {
      return new DefaultServletContextHelper(bundle);
    }

    @Override
    public void ungetService(
        Bundle bundle,
        ServiceRegistration<ServletContextHelper> registration,
        ServletContextHelper service) {}
  }
}

This works and my resources are served from the expected endpoint.

The problem appears to be related to my resource management when my context is restarted (which appears to happen when bundles registering servlets are stopped or restarted.)

Here's the stacktrace I receive

rnal.framework.BundleContextImpl  968 | 396 - .ops4j.pax.web.pax-web-extender-whiteboard - 8.0.24 | FrameworkEvent ERROR
java.lang.RuntimeException: Servlet with reference "{javax.servlet.Servlet}={osgi.http.whiteboard.servlet.name=servletName, service.id=1551, osgi.service.blueprintpname=servletName, service.bundleid=860, service.scope=bundle, osgi.http.whiteboard.servlet.pattern=/servletroute/*, osgi.http.whiteboard.context.select=(osgi.http.whiteboard.context.name=customContext)}" was never registered by bundle-name [860]
    at .ops4j.pax.web.service.internal.HttpServiceEnabled.lambda$doUnregisterServlet$2(HttpServiceEnabled.java:707) ~[?:?]
    at .ops4j.pax.web.service.spi.model.ServerModel.lambda$run$2(ServerModel.java:541) ~[?:?]
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768) ~[?:?]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[?:?]
    at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[?:?]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
    at java.lang.Thread.run(Thread.java:840) ~[?:?]
    Suppressed: java.lang.Throwable

Presumably I'm not managing something correctly but I'm not really sure what. Is there something I'm supposed to do on the bundle stop for all of these servlets (that's missed when it's registered via blueprint?) Maybe something within the Service Factory for my context? I'm not really sure and I'm having trouble debugging through as it's not a consistent issue. So what is it I should be doing differently for my context and servlet registration to prevent this issue from happening?

本文标签: osgiServlet Management Pax web 8 jetty 9Stack Overflow