[OpenJMS 0.7.6.1] How to Shutdown Server Programatically?

"Edwin Lee" <[email protected]> Wed, 13 Sep 2006 14:46:08 +0800
Newsgroups gmane.comp.java.openjms.user
Message-ID <[email protected]>
Hi guys,

i have an application which uses OpenJMS 0.7.6.1, and i want to be
able to start up and shutdown the OpenJMS server from within my
application programatically.

i start up the server as follows:

// Configuration object (configuration) is created and populated here
JmsServer jmsServer = new JmsServer(configuration);
jmsServer.init();

The starting up is great. The server is up and running. i can send
messages, receive messages, etc.

Now for the shutting down of the server. i can't seem to find any API
to shutdown the server.

i tried to trace what shutdown.bat (or shutdown.sh) does, and arrived at this:

AbstractAdminConnection onlineConnection = new OnlineConnection(u, p);
onlineConnection.stopServer();

which *does* shutdown the server, but it also shuts down the rest of
my application, due to an eventual call to System.exit(0). So i can't
really use this, as i need the rest of my application to continue
running.

So i tried to hack together something which required reflection to
gain access to some of the private fields:

Class jmsServerClass = jmsServer.getClass();

Field servicesField = jmsServerClass.getDeclaredField("_services");
servicesField.setAccessible(true);
ServiceManager services = (ServiceManager) servicesField.get(jmsServer);

services.stopAll();

Field interfacesField = jmsServerClass.getDeclaredField("_interfaces");
interfacesField.setAccessible(true);
JmsServerIfc[] interfaces = (JmsServerIfc[]) interfacesField.get(jmsServer);
if (interfaces != null)
{
	for (int i = 0; i < interfaces.length; i++)
	{
		if (interfaces[i] instanceof IpcJmsServer)
		{
			((IpcJmsServer) interfaces[i]).removeAllConnections();
			
			Class ipcJmsServerClass = interfaces[i].getClass();
			Field serverField = ipcJmsServerClass.getDeclaredField("_server");
			serverField.setAccessible(true);
			MultiplexConnectionServerIfc server =
(MultiplexConnectionServerIfc) serverField.get(interfaces[i]);
			server.shutdownAll();
		}
	}
}

Field jndiInterfacesField = jmsServerClass.getDeclaredField("_jndiInterfaces");
jndiInterfacesField.setAccessible(true);
JndiServerIfc[] jndiInterfaces = (JndiServerIfc[])
jndiInterfacesField.get(jmsServer);
if (jndiInterfaces != null)
{
	for (int i = 0; i < jndiInterfaces.length; i++)
	{
		if (jndiInterfaces[i] instanceof IpcJndiServer)
		{
			Class ipcJndiServerClass = jndiInterfaces[i].getClass();
			Field serverField = ipcJndiServerClass.getDeclaredField("_server");
			serverField.setAccessible(true);
			Server server = (Server) serverField.get(jndiInterfaces[i]);
			server.stop();
		}
	}
}

jmsServer = null;

but i ran into a ClassCastException at the point server.shutdownAll().

The stack trace of the CCE points to line 185 of
org.exolab.core.mipc.MultiplexConnectionServer, which is
((MultiplexConnectionIfc) connections[i]).finish();

i figure i must have done something wrongly here as well.

Any advice on how to shutdown the server programatically?

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642