4 Replies Latest reply: Feb 24, 2012 6:40 AM by gimbal2 RSS

    Starting and stopping tomcat as a webservice

    777465
      Dear Friends,

      I am trying to write a webservice which will start a tomcat and stop it .. the 2 functions with the class are given below:

      public class Tomcat_Windows_Loader_Impl {
           Bootstrap bootstrap;
           public int startTomcat()
           {
                bootstrap=new Bootstrap();
                bootstrap.setCatalinaHome("c:/TOSCA/apache-tomcat-7.0.25-windows-x86/apache-tomcat-7.0.25/");
                bootstrap.setCatalinaBase("c:/TOSCA/apache-tomcat-7.0.25-windows-x86/apache-tomcat-7.0.25/");
                
      try {
                     bootstrap.start();
                     System.out.println("Started Again!!!!");
                     return 0;
                } catch (Exception e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                     
                }

                /*try {
                     Thread.sleep(600000);
                     System.out.println("Sleeping!");
                } catch (InterruptedException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }*/
                return -1;
           }
           

                public int stopTomcat()
           {
                     System.out.println("bootstrap3");
                     bootstrap=new Bootstrap();
           //     bootstrap.setCatalinaHome("c:/TOSCA/apache-tomcat-7.0.25-windows-x86/apache-tomcat-7.0.25/");
                     bootstrap.setCatalinaHome("c:/TOSCA/apache-tomcat-7.0.25-windows-x86/apache-tomcat-7.0.25/");
                     bootstrap.setCatalinaBase("c:/TOSCA/apache-tomcat-7.0.25-windows-x86/apache-tomcat-7.0.25/");
                     System.out.println("Catalina home now is " + bootstrap.getCatalinaHome());
                     System.out.println("Catalina base now is changed to " + bootstrap.getCatalinaBase());
           try {
                
                if (bootstrap == null) {
                     System.out.println("Bootstrap is null");
                }
                else{
                     System.out.println("Bootstrap is not null");
                System.out.println("Going to stop");
           //     bootstrap.start(); //javax.management.InstanceAlreadyExistsException: Catalina:type=ServerClassLoader,name=common

                bootstrap.stop();
                return 0;
                }
           } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
           }
           return -1;
           }

      I am getting a null pointer exception when i call           bootstrap.stop();

      Java.lang.NullPointerException
           at org.apache.catalina.startup.Bootstrap.stop(Bootstrap.java:314)
           at com.tosca.iaas.Tomcat_Windows_Loader_Impl.stopTomcat(Tomcat_Windows_Loader_Impl.java:54)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
           at java.lang.reflect.Method.invoke(Unknown Source)
           at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:178)
           at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:117)
           at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
           at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:110)
           at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:181)
           at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:172)
           at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:146)
           at org.wso2.carbon.core.transports.CarbonServlet.doPost(CarbonServlet.java:199)
           at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
           at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
           at org.eclipse.equinox.http.servlet.internal.ServletRegistration.handleRequest(ServletRegistration.java:90)
           at org.eclipse.equinox.http.servlet.internal.ProxyServlet.processAlias(ProxyServlet.java:111)
           at org.eclipse.equinox.http.servlet.internal.ProxyServlet.service(ProxyServlet.java:67)
           at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
           at org.wso2.carbon.bridge.BridgeServlet.service(BridgeServlet.java:164)
           at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
           at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
           at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
           at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
           at org.wso2.carbon.server.CarbonStuckThreadDetectionValve.invoke(CarbonStuckThreadDetectionValve.java:154)
           at org.wso2.carbon.server.TomcatServer$1.invoke(TomcatServer.java:254)
           at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
           at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
           at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
           at org.apache.coyote.http11.Http11NioProcessor.process(Http11NioProcessor.java:396)
           at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:356)
           at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1534)
           at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
           at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
           at java.lang.Thread.run(Unknown Source)


      I want to be able to get hold of a reference that will help me to stop the tomcat.

      When i wrote it as a java application, it was working fine.

      public static void main(String[] args)
      {
           
           Bootstrap bootstrap=new Bootstrap();
           bootstrap.setCatalinaHome("c:/TOSCA/apache-tomcat-7.0.25-windows-x86/apache-tomcat-7.0.25/");
           
           try {
                bootstrap.start();
                System.out.println("Started!");
                System.out.println("Catalina base now is " + bootstrap.getCatalinaBase());
           } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
           }

           try {
                Thread.sleep(60000);
                System.out.println("Sleeping!");
           } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
           }

           try {
                System.out.println("Going to stop");
                bootstrap.stop();
           } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
           }
      }
      }

      Can someone comment on the things that i overlook here?

      Thanks ,
      Anand
        • 1. Re: Starting and stopping tomcat as a webservice
          EJP
          I am trying to write a webservice which will start a tomcat and stop it .. the 2 functions with the class are given below:
          I hope this means you are trying to start another Tomcat, other than whatever the webservice is embedded in. Otherwise what you are are trying to do is ipso facto impossible.
                         bootstrap=new Bootstrap();
          ...
                    if (bootstrap == null) {
          bootstrap cannot possibly be null here.
          I am getting a null pointer exception when i call           bootstrap.stop();

          Java.lang.NullPointerException
               at org.apache.catalina.startup.Bootstrap.stop(Bootstrap.java:314)
          So something that the Bootstrap class requires not to be null is null. Check its Javadoc. Not a question for this forum, or these forums.
          • 2. Re: Starting and stopping tomcat as a webservice
            r035198x
            Don't do this. Start and stop Tomcat using OS specific commands that take care of all environment specific paths and variables.
            All the webservice implementation should be doing is call the OS specific start/stop script.
            • 3. Re: Starting and stopping tomcat as a webservice
              777465
              Thanks a lot of folks.

              I would just want another opinion on this.

              What i am doing is deploying a web service on WSO2 which will be going to start/stop different containers.. I looked at this

              http://stackoverflow.com/questions/2190300/how-to-start-and-stop-an-tomcat-container-with-java

              and the option to start/stop using cargo API seems interesting as well..

              As you guys advised, I think i would also try to use the start/stop scripts that is OS specific and executing them as 'commands' rather than trying to start Bootstrap class loader.

              i was curious and just wanted your opinion on this .. which way is better ?

              I will try both the options and let know how it turned out! thanks for your suggestions..


              Cheers,
              -Anand
              • 4. Re: Starting and stopping tomcat as a webservice
                gimbal2
                user8784076 wrote:
                i was curious and just wanted your opinion on this .. which way is better ?
                Well if you can do it reliably using only a Java API, I'd certainly prefer that.