-2

We have a code (webservice) that we expect it to run one time but it runs two times. We were unable to find out why it runs two times. Here is a simplified Hello World example:

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;
    }

}

And here is part of the logs. You can see that there are two Hello ! in line 13 and 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 and Tomcat 8.

Why it runs two times?

Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
ilhan
  • 8,700
  • 35
  • 117
  • 201
  • what do you mean by `run` how did you test your webservice did you use the netbeans testing feature to run it , did you run it via drag and drop in jsp or test dialog or deployed in tomcat – shareef Dec 14 '15 at 20:15
  • @shareef, there is a green run button at the top of Netbeans. I don't know other methods. – ilhan Dec 14 '15 at 20:24
  • 1
    okay take a close look at the logs you have attached i noticed the first hello were printed after logs in tomcat like logs and then the second hello printed after glassfish logs like why is that , you should test the webservice like this tutorial `https://netbeans.org/kb/docs/websvc/jax-ws.html` please provide if you did follow any tutorial to develop your service – shareef Dec 14 '15 at 20:27
  • I tried the code you have provide , and i think the constructor which making that duplicate , what is the type of your project `java project` or `web application` ? – shareef Dec 14 '15 at 21:15

1 Answers1

1

In an Java EE Server Environment, how often a class is instantiated depends on multiple factors (framework, type of bean, config of bean, server config, user load ...). It's not like a client, were you ran a application once and things happen once. The essence is, in the constructor you should only do things that are imported to initialize for the object itself.

What you are looking for are scopes. The define a context for application, session, request ... But if you need context for a webservice is another case.

What happens in you case, the class is instantiated from an listener [http-nio-8084-exec-5] and in the beginning it is instantiated from class loader. That is done because in your web.xml there is a element <load-on-startup> with a value >= 0.

Read more about Java EE lifecycle:

Community
  • 1
  • 1
notes-jj
  • 1,437
  • 1
  • 20
  • 33