问题
I need to stop and then start the https connector programmatically. In tomcat 6 the following scrap of code works fine:
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");
}
But in tomcat 7 (7.0.23) the connector does not stopped! So I invoked the destroy() method (right after invoking the stop() method) using this line of code: mbeanServer.invoke(objectName, "destroy", null, null);
In this case the connector indeed stopped. But when I tried to start it, the connector did not start and I got this exception:
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)
Any ideas of how I can stop and then start the connector in tomcat 7? Preferable in a graceful manner. Thanks in advance, Guy
回答1:
It depends on how you are defining "stop". By default, Tomcat binds to the port during init() and unbinds during destroy(). In the default case stop() just stops processing new connections but it may not stop those connections being made (depending on - amongst other things - the underlying OS).
You may get better results if you set bindOnInit="false" for the connector in your server.xml. With this setting the port will be bound when start() is called an unbound when stop() is called.
来源:https://stackoverflow.com/questions/9912585/stop-start-connector-within-my-code