Uploaded image for project: 'Weld'
  1. Weld
  2. WELD-2223

Registering servlet listener on tomcat embedded fails when container bootstrapped outside

    XMLWordPrintable

    Details

      Description

      Presume I have this test:

          @Test
          public void shouldStartTomcat() throws Exception {
              try(WeldContainer weldContainer = new Weld().disableDiscovery()
                      .beanClasses(DefaultServlet.class).initialize()) {
                  String baseDir = "target/webapp-runner";
                  Tomcat tomcat = new Tomcat();
                  int port = 8080;
                  tomcat.setPort(port);
                  File base = new File(baseDir);
                  if (!base.exists())
                  {
                      base.mkdirs();
                  }
                  tomcat.setBaseDir(baseDir);
                  Context ctx = tomcat.addContext("/",base.getAbsolutePath());
                  StandardContext standardContext = (StandardContext)ctx;
                  standardContext.addApplicationListener(Listener.class.getName());
       
                  Wrapper wrapper = Tomcat.addServlet(ctx,"RequestServlet",DefaultServlet.class.getName());
                  wrapper.addMapping("/*");
                  tomcat.start();
                  try(InputStream stream = new URL("http://localhost:8080/").openStream()) {
                      String data = IOUtils.toString(stream).trim();
                      assertThat(data).isEqualTo(MessageProvider.DATA);
                  }
       
                  try(InputStream stream = new URL("http://localhost:8080/").openStream()) {
                      String data = IOUtils.toString(stream).trim();
                      assertThat(data).isEqualTo(MessageProvider.DATA);
                  }
              }
          }
      

      This test fails, with output like:

      SEVERE: Failed to initialize end point associated with ProtocolHandler ["http-nio-8080"]
      java.net.BindException: Address already in use
      	at sun.nio.ch.Net.bind0(Native Method)
      	at sun.nio.ch.Net.bind(Net.java:433)
      	at sun.nio.ch.Net.bind(Net.java:425)
      	at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
      	at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
      	at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:340)
      	at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:742)
      	at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:458)
      	at org.apache.coyote.http11.AbstractHttp11JsseProtocol.init(AbstractHttp11JsseProtocol.java:120)
      	at org.apache.catalina.connector.Connector.initInternal(Connector.java:960)
      	at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
      	at org.apache.catalina.core.StandardService.initInternal(StandardService.java:568)
      	at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
      	at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:851)
      	at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
      	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
      	at org.apache.catalina.startup.Tomcat.start(Tomcat.java:344)
      	at ws.ament.hammock.web.tomcat.TomcatWebServer.start(TomcatWebServer.java:73)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at org.jboss.weld.bean.proxy.AbstractBeanInstance.invoke(AbstractBeanInstance.java:38)
      	at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:100)
      	at ws.ament.hammock.web.spi.WebServer$874618097$Proxy$_$$_WeldClientProxy.start(Unknown Source)
      	at ws.ament.hammock.web.spi.StartWebServer.init(StartWebServer.java:53)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:88)
      	at org.jboss.weld.injection.MethodInvocationStrategy$SpecialParamPlusBeanManagerStrategy.invoke(MethodInvocationStrategy.java:144)
      	at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:313)
      	at org.jboss.weld.event.ExtensionObserverMethodImpl.sendEvent(ExtensionObserverMethodImpl.java:125)
      	at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:291)
      	at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:269)
      	at org.jboss.weld.event.ObserverNotifier.notifySyncObservers(ObserverNotifier.java:302)
      	at org.jboss.weld.event.ObserverNotifier.notify(ObserverNotifier.java:291)
      	at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:160)
      	at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:154)
      	at org.jboss.weld.bootstrap.events.AbstractContainerEvent.fire(AbstractContainerEvent.java:53)
      	at org.jboss.weld.bootstrap.events.AbstractDeploymentContainerEvent.fire(AbstractDeploymentContainerEvent.java:35)
      	at org.jboss.weld.bootstrap.events.AfterDeploymentValidationImpl.fire(AfterDeploymentValidationImpl.java:28)
      	at org.jboss.weld.bootstrap.WeldStartup.validateBeans(WeldStartup.java:449)
      	at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:90)
      	at org.jboss.weld.environment.servlet.WeldServletLifecycle.initialize(WeldServletLifecycle.java:238)
      	at org.jboss.weld.environment.servlet.Listener.contextInitialized(Listener.java:121)
      	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4812)
      	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)
      	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
      	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
      	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      	at java.lang.Thread.run(Thread.java:745)
      

      IN this case, the port is not already in use. It seems that adding listener is starting tomcat in a loop. Removing listener fixes it.

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Assignee:
                mkouba Martin Kouba
                Reporter:
                meetoblivion John Ament
              • Votes:
                1 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: