user1317880
user1317880

Reputation: 11

How to manage Tomcat via Java

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.

Upvotes: 1

Views: 4476

Answers (6)

Sagar Zaveri
Sagar Zaveri

Reputation: 41

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
}

}

Upvotes: 0

MarianP
MarianP

Reputation: 2759

You can run Tomcat embedded in your app.

Upvotes: 2

hhe
hhe

Reputation: 313

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();

Upvotes: 1

dash1e
dash1e

Reputation: 7807

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

Upvotes: 0

Vishal
Vishal

Reputation: 11

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

Upvotes: 1

Anna
Anna

Reputation: 855

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/

Upvotes: 1

Related Questions