在我的代码中停止\启动连接器

时间:2012-03-28 17:22:38

标签: tomcat connector

我需要停止然后以编程方式启动https连接器。 在tomcat 6中,以下代码段可以正常工作:

        final ObjectName objectNameQuery = new ObjectName("*:type=Connector,port=443,*"); 
        MBeanServer      mbeanServer = null;
        ObjectName       objectName  = null;

        for (final MBeanServer server : (List<MBeanServer>) MBeanServerFactory.findMBeanServer(null))
        {
            if (server.queryNames(objectNameQuery, null).size() > 0)
            {
                mbeanServer     = server;
                objectName      = (ObjectName) server.queryNames(objectNameQuery, null).toArray()[0];

                break;
            }
        }

        // now we restart the connector that we just found. We sleep a little, but I am
        // not actually sure 1) how long we should sleep for or 2) if sleeping is
        // necessary at all.
        if (mbeanServer != null)
        {
            mbeanServer.invoke(objectName, "stop", null, null);                

            Thread.sleep(waitForStopInSec * 1000);

            mbeanServer.invoke(objectName, "start", null, null);

            log.warn("https Connector was restarted");
        }

但是在tomcat 7(7.0.23)中,连接器没有停止!所以我使用这行代码调用了destroy()方法(在调用stop()方法之后): mbeanServer.invoke(objectName,“destroy”,null,null);

在这种情况下,连接器确实已停止。但是当我尝试启动它时,连接器没有启动,我得到了这个例外:

28/03/12 18:32:01 ERROR T:CommServerScheduler_Worker-1 TrustStoreRefreshJob.refreshHTTPSConnectors - failed to restart connector 
javax.management.InstanceNotFoundException:    Catalina:type=Connector,port=443,address="/192.168.201.24"
     at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1094)
     at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:833)
     at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:761)
     at tool.security.TrustStoreRefreshJob.refreshHTTPSConnectors(TrustStoreRefreshJob.java:79)
     at tool.security.TrustStoreRefreshJob.executeJob(TrustStoreRefreshJob.java:32)
     at com.nextnine.common.scheduler.AbstractJobLogic.execute(AbstractJobLogic.java:47)
     at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
     at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)

关于如何在tomcat 7中停止然后启动连接器的任何想法?优雅地以优雅的方式。 提前致谢, 盖

1 个答案:

答案 0 :(得分:2)

这取决于你如何定义“停止”。默认情况下,Tomcat在init()期间绑定到端口并在destroy()期间解除绑定。在默认情况下,stop()只是停止处理新连接,但它可能无法停止正在进行的连接(取决于 - 除其他外 - 底层操作系统)。

如果为server.xml中的连接器设置bindOnInit =“false”,则可能会获得更好的结果。使用此设置,当调用stop()时调用start()为未绑定时,将绑定端口。