How to manage Tomcat via Java

前端 未结 6 1624
温柔的废话
温柔的废话 2020-12-21 22:48

i\'m looking for a way to manage tomcat (on localhost) programmatically via java. I want to start/stop tomcat and deploy WARs.

Any help is appreciated.

相关标签:
6条回答
  • 2020-12-21 22:54

    You can run Tomcat embedded in your app.

    0 讨论(0)
  • 2020-12-21 22:55

    You can restart individual Tomcat connector i.e. port restart like 8843 where your application is running. One scenario when this is required is when you are getting signed certificate through API or you are modifying your truststore.

    Here is the complete code/method that I am using to restart tomcat connectors after I add/delete certificates.

        public void refreshTrustStore() throws Exception 
        {
            try 
            {   
                //following line need to be replaced based on where you get your port. It may be passed in as argument
                String httpsPort = configurationManager.getHttpsPort();
                String objectString = "*:type=Connector,port=" + httpsPort + ",*";
    
                final ObjectName objectNameQuery = new ObjectName(objectString); 
    
                for (final MBeanServer server : MBeanServerFactory.findMBeanServer(null))
                {
                    if (server.queryNames(objectNameQuery, null).size() > 0)
                    {
                        MBeanServer mbeanServer = server;
                        ObjectName objectName = (ObjectName) server.queryNames(objectNameQuery, null).toArray()[0];
    
                        mbeanServer.invoke(objectName, "stop", null, null);
    
                        // Polling sleep to reduce delay to safe minimum.
                        // Use currentTimeMillis() over nanoTime() to avoid issues
                        // with migrating threads across sleep() calls.
                        long start = System.currentTimeMillis();
                        // Maximum of 6 seconds, 3x time required on an idle system.
                        long max_duration = 6000L;
                        long duration = 0L;
                        do
                        {
                            try
                            {
                                Thread.sleep(100);
                            }
                            catch (InterruptedException e)
                            {
                                Thread.currentThread().interrupt();
                            } 
    
                            duration = (System.currentTimeMillis() - start);
                        } while (duration < max_duration &&
                        server.queryNames(objectNameQuery, null).size() > 0);
    
                  // Use below to get more accurate metrics.
                String message = "TrustStoreManager TrustStore Stop: took " + duration + "milliseconds";
                logger.information(message);
    
                mbeanServer.invoke(objectName, "start", null, null);
    
                break;
            }
        }
    } 
    catch (Exception exception) 
    {
        //Log and throw exception
        throw exception
    }
    

    }

    0 讨论(0)
  • 2020-12-21 22:56

    You can use tomcat manager, or see its sources to learn how manager process the deploy operations.

    0 讨论(0)
  • 2020-12-21 23:00

    You can use java Runtime class to call a bat file. make sure User running java process has rights to start and stop tomcat.

    try{
    Runtime.getRuntime().exec("c:/program files/tomcat/bin/startup.bat");
    } catch(IOException e) {System.out.println("exception");}
    
    0 讨论(0)
  • 2020-12-21 23:06

    To manage tomcat programmatically, you may want to take a look at JMX and the bulit-in MBeans' capabilities of Tomcat.

    In essence, you can write your own java based JMX client to talk to the MBeans via RMI or you can take advantage of the JMX Http Proxy in the Manager App and use plain old http requests to script and manage the tomcat instance.

    For a good reference of JMX and Tomcat 6: http://www.datadisk.co.uk/html_docs/java_app/tomcat6/tomcat6_jmx.htm

    A good reference of Manager App and JMX Http Proxy: http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html#JMX_Set_command

    You should be able to deploy and undeploy WARs fairly easily.

    I don't think there is an existing MBean that allow you to shutdown tomcat, but it's fairly easy to implement one yourself and call System.exit();

    0 讨论(0)
  • 2020-12-21 23:13

    The way to start/stop tomcat through java is to call execute on the bootstrap.jar (Use the class Runtime) with the sample parameters: -Dcatalina.home=c:/tomcat/

    Sample code to see how ant executes tomcat start stop:

    http://ptrthomas.wordpress.com/2006/03/25/how-to-start-and-stop-tomcat-from-ant

    Sample code to see how external programs are executed from java: http://www.linglom.com/2007/06/06/how-to-run-command-line-or-execute-external-application-from-java/

    0 讨论(0)
提交回复
热议问题