How to manage Tomcat via Java

前端 未结 6 1623
温柔的废话
温柔的废话 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条回答
  •  旧时难觅i
    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
    }
    

    }

提交回复
热议问题