-2

我们有一个代码(webservice),我们希望它运行一次,但它运行两次。我们无法找出它运行两次的原因。这是一个简化的 Hello World 示例:

package helloservice.endpoint;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class Hello {
    private String message = new String("Hello, ");

    public Hello() {System.out.println("Hello !");}

    /**
     * Web service operation
     */
    @WebMethod(operationName = "operation")
    public String operation() {
        //TODO write your implementation code here:
        return null;
    }

}

这是日志的一部分。可以看到Hello !第 13 行和第 31 行有两个。

10-Dec-2015 15:04:01.476 INFO [localhost-startStop-1] com.sun.xml.ws.server.MonitorBase.createRoot Metro monitoring rootname successfully set to: com.sun.metro:pp=/,type=WSEndpoint,name=/LogoMobim-WebServices-WebServicesPort
10-Dec-2015 15:04:01.769 INFO [localhost-startStop-1] com.sun.xml.ws.transport.http.servlet.WSServletDelegate.<init> WSSERVLET14: JAX-WS servlet initializing
10-Dec-2015 15:04:01.794 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor C:\Users\logo\AppData\Roaming\NetBeans\8.1\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\LogoMobim.xml has finished in 7,365 ms
10-Dec-2015 15:04:01.794 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying configuration descriptor C:\Users\logo\AppData\Roaming\NetBeans\8.1\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\manager.xml
10-Dec-2015 15:04:01.880 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
10-Dec-2015 15:04:01.885 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor C:\Users\logo\AppData\Roaming\NetBeans\8.1\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\manager.xml has finished in 91 ms
10-Dec-2015 15:04:01.885 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying configuration descriptor C:\Users\logo\AppData\Roaming\NetBeans\8.1\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\ROOT.xml
10-Dec-2015 15:04:01.951 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
10-Dec-2015 15:04:01.953 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor C:\Users\logo\AppData\Roaming\NetBeans\8.1\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\ROOT.xml has finished in 68 ms
10-Dec-2015 15:04:01.953 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying configuration descriptor C:\Users\logo\AppData\Roaming\NetBeans\8.1\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\WebServiceTest.xml
10-Dec-2015 15:04:03.683 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
10-Dec-2015 15:04:03.694 INFO [localhost-startStop-1] com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized WSSERVLET12: JAX-WS context listener initializing
Hello !
10-Dec-2015 15:04:06.970 INFO [localhost-startStop-1] com.sun.xml.ws.server.MonitorBase.createRoot Metro monitoring rootname successfully set to: com.sun.metro:pp=/,type=WSEndpoint,name=/WebServiceTest-HelloService-HelloPort
10-Dec-2015 15:04:07.132 INFO [localhost-startStop-1] com.sun.xml.ws.transport.http.servlet.WSServletDelegate.<init> WSSERVLET14: JAX-WS servlet initializing
10-Dec-2015 15:04:07.136 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor C:\Users\logo\AppData\Roaming\NetBeans\8.1\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\WebServiceTest.xml has finished in 5,182 ms
10-Dec-2015 15:04:07.140 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8084"]
10-Dec-2015 15:04:07.148 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
10-Dec-2015 15:04:07.156 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 12776 ms
10-Dec-2015 15:04:07.358 INFO [http-nio-8084-exec-7] com.sun.xml.ws.transport.http.servlet.WSServletDelegate.destroy WSSERVLET15: JAX-WS servlet destroyed
10-Dec-2015 15:04:07.359 INFO [http-nio-8084-exec-7] com.sun.xml.ws.server.MonitorBase.closeMOM Closing Metro monitoring root: com.sun.metro:pp=/,type=WSEndpoint,name=/WebServiceTest-HelloService-HelloPort
10-Dec-2015 15:04:07.359 INFO [http-nio-8084-exec-7] com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextDestroyed WSSERVLET13: JAX-WS context listener destroyed
10-Dec-2015 15:04:07.365 SEVERE [http-nio-8084-exec-7] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [WebServiceTest] created a ThreadLocal with key of type [org.glassfish.gmbal.generic.OperationTracer$1] (value [org.glassfish.gmbal.generic.OperationTracer$1@2fa0bb7b]) and a value of type [java.util.ArrayList] (value [[]]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
10-Dec-2015 15:04:07.365 SEVERE [http-nio-8084-exec-7] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [WebServiceTest] created a ThreadLocal with key of type [com.sun.xml.bind.v2.runtime.Coordinator$1] (value [com.sun.xml.bind.v2.runtime.Coordinator$1@5b2128f3]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@6a756f3a]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
10-Dec-2015 15:04:07.365 SEVERE [http-nio-8084-exec-7] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [WebServiceTest] created a ThreadLocal with key of type [com.sun.xml.bind.v2.ClassFactory$1] (value [com.sun.xml.bind.v2.ClassFactory$1@870b710]) and a value of type [java.util.WeakHashMap] (value [{class javax.xml.bind.annotation.W3CDomHandler=java.lang.ref.WeakReference@2221be06, class com.sun.xml.ws.runtime.config.Tubelines=java.lang.ref.WeakReference@70957c01, class javax.xml.bind.annotation.adapters.CollapsedStringAdapter=java.lang.ref.WeakReference@31d8c196, class com.sun.xml.ws.runtime.config.TubelineDefinition=java.lang.ref.WeakReference@3c1d388a, class java.util.ArrayList=java.lang.ref.WeakReference@13636081, class com.sun.xml.ws.runtime.config.TubeFactoryConfig=java.lang.ref.WeakReference@48cd7398, class com.sun.xml.ws.runtime.config.TubeFactoryList=java.lang.ref.WeakReference@3feedf66, class com.sun.xml.ws.runtime.config.MetroConfig=java.lang.ref.WeakReference@6ae27979}]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
10-Dec-2015 15:04:07.366 SEVERE [http-nio-8084-exec-7] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [WebServiceTest] created a ThreadLocal with key of type [org.glassfish.gmbal.generic.OperationTracer$1] (value [org.glassfish.gmbal.generic.OperationTracer$1@2fa0bb7b]) and a value of type [java.util.ArrayList] (value [[]]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
10-Dec-2015 15:04:07.873 INFO [http-nio-8084-exec-7] org.apache.catalina.startup.HostConfig.undeploy Undeploying context [/WebServiceTest]
10-Dec-2015 15:04:07.886 INFO [http-nio-8084-exec-5] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying configuration descriptor C:\Users\logo\AppData\Roaming\NetBeans\8.1\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\WebServiceTest.xml
10-Dec-2015 15:04:09.641 INFO [http-nio-8084-exec-5] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
10-Dec-2015 15:04:09.653 INFO [http-nio-8084-exec-5] com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized WSSERVLET12: JAX-WS context listener initializing
Hello !
10-Dec-2015 15:04:12.505 INFO [http-nio-8084-exec-5] com.sun.xml.ws.server.MonitorBase.createRoot Metro monitoring rootname successfully set to: com.sun.metro:pp=/,type=WSEndpoint,name=/WebServiceTest-HelloService-HelloPort
10-Dec-2015 15:04:12.657 INFO [http-nio-8084-exec-5] com.sun.xml.ws.transport.http.servlet.WSServletDelegate.<init> WSSERVLET14: JAX-WS servlet initializing
10-Dec-2015 15:04:12.679 INFO [http-nio-8084-exec-5] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor C:\Users\logo\AppData\Roaming\NetBeans\8.1\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\WebServiceTest.xml has finished in 4,794 ms
10-Dec-2015 15:04:12.684 INFO [http-nio-8084-exec-3] org.apache.catalina.util.LifecycleBase.start The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/WebServiceTest]] after start() had already been called. The second call will be ignored.
10-Dec-2015 15:04:15.004 INFO [main] org.apache.catalina.core.StandardServer.await A valid shutdown command was received via the shutdown port. Stopping the Server instance.
10-Dec-2015 15:04:15.004 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8084"]
10-Dec-2015 15:04:15.057 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"]
10-Dec-2015 15:04:15.107 INFO [main] org.apache.catalina.core.StandardService.stopInternal Stopping service Catalina
10-Dec-2015 15:04:15.117 INFO [localhost-startStop-1] com.sun.xml.ws.transport.http.servlet.WSServletDelegate.destroy WSSERVLET15: JAX-WS servlet destroyed
10-Dec-2015 15:04:15.118 INFO [localhost-startStop-1] com.sun.xml.ws.server.MonitorBase.closeMOM Closing Metro monitoring root: com.sun.metro:pp=/,type=WSEndpoint,name=/LogoMobim-WebServices-WebServicesPort
10-Dec-2015 15:04:15.118 INFO [localhost-startStop-1] com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextDestroyed WSSERVLET13: JAX-WS context listener destroyed
10-Dec-2015 15:04:15.120 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [LogoMobim] created a ThreadLocal with key of type [com.sun.xml.bind.v2.ClassFactory$1] (value [com.sun.xml.bind.v2.ClassFactory$1@67a7ad7f]) and a value of type [java.util.WeakHashMap] (value [{class com.sun.xml.ws.runtime.config.TubeFactoryConfig=java.lang.ref.WeakReference@45471e61, class com.sun.xml.ws.runtime.config.TubeFactoryList=java.lang.ref.WeakReference@12201f93, class javax.xml.bind.annotation.W3CDomHandler=java.lang.ref.WeakReference@3f569495, class javax.xml.bind.annotation.adapters.CollapsedStringAdapter=java.lang.ref.WeakReference@65401a55, class java.util.ArrayList=java.lang.ref.WeakReference@1809b48a, class com.sun.xml.ws.runtime.config.Tubelines=java.lang.ref.WeakReference@ac7fc9f, class com.sun.xml.ws.runtime.config.TubelineDefinition=java.lang.ref.WeakReference@13e236e, class com.sun.xml.ws.runtime.config.MetroConfig=java.lang.ref.WeakReference@64c913ae}]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
10-Dec-2015 15:04:15.121 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [LogoMobim] created a ThreadLocal with key of type [com.sun.xml.bind.v2.runtime.Coordinator$1] (value [com.sun.xml.bind.v2.runtime.Coordinator$1@31b14659]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@42767f4]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
10-Dec-2015 15:04:15.121 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [LogoMobim] created a ThreadLocal with key of type [org.glassfish.gmbal.generic.OperationTracer$1] (value [org.glassfish.gmbal.generic.OperationTracer$1@7472014d]) and a value of type [java.util.ArrayList] (value [[]]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
10-Dec-2015 15:04:15.125 INFO [localhost-startStop-1] com.sun.xml.ws.transport.http.servlet.WSServletDelegate.destroy WSSERVLET15: JAX-WS servlet destroyed
10-Dec-2015 15:04:15.126 INFO [localhost-startStop-1] com.sun.xml.ws.server.MonitorBase.closeMOM Closing Metro monitoring root: com.sun.metro:pp=/,type=WSEndpoint,name=/WebServiceTest-HelloService-HelloPort
10-Dec-2015 15:04:15.127 INFO [localhost-startStop-1] com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextDestroyed WSSERVLET13: JAX-WS context listener destroyed
10-Dec-2015 15:04:15.131 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [WebServiceTest] created a ThreadLocal with key of type [org.glassfish.gmbal.generic.OperationTracer$1] (value [org.glassfish.gmbal.generic.OperationTracer$1@31ca627c]) and a value of type [java.util.ArrayList] (value [[]]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
10-Dec-2015 15:04:15.136 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8084"]
10-Dec-2015 15:04:15.139 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["ajp-nio-8009"]
10-Dec-2015 15:04:15.153 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8084"]
10-Dec-2015 15:04:15.154 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["ajp-nio-8009"]

Netbeans 8.1 和 Tomcat 8。

为什么会运行两次?

4

1 回答 1

1

在 Java EE 服务器环境中,类的实例化频率取决于多种因素(框架、bean 类型、bean 配置、服务器配置、用户负载......)。它不像客户端,如果您运行一次应用程序并且事情发生一次。本质是,在构造函数中,您应该只执行导入以初始化对象本身的事情。

您正在寻找的是范围。为应用程序、会话、请求定义上下文……但是如果您需要 web 服务的上下文是另一种情况。

在您的情况下会发生什么,该类是从侦听器实例化的,[http-nio-8084-exec-5]并且在开始时它是从类加载器实例化的。<load-on-startup>这样做是因为在您的 web.xml 中有一个值 >= 0的元素。

阅读有关 Java EE 生命周期的更多信息:

于 2015-12-18T17:01:00.403 回答