Controlling JCDs with JMX

Has anyone figured out how to stop or restart JCDs programmatically using JMX?

I know that I can enable and disable the EAR using the isadmin command line utility. And, I know that I can stop and restart the JCD using the Enterprise Monitor. I'd like to be able to start and restart the JCD from another JCD.

Regards.

[338 byte] By [AKsuiteda] at [2007-11-27 0:36:03]
# 1

Yes, we have done this. We send out a jms message from the service we want to invoke some action on to another jcd that will locate the correct managed bean and then shut it down. It takes a bit of work though. We search through the list of managed beans with the correct deployment profile name and the service name. From that, we get the J2EEApplication name (because it usually gets created with alphanumeric characters due to our project path exceeding 30 characters) which provides us the unique identifier. Once you have the J2EEApplication name, you can get a reference to the managed bean server for that domain and then locate that specific bean. You can then invoke the stop or start operation on the managed bean (done through Reflection).

Alternatively, I have read that you can shut down a service from within itself by invoking a Alert.fatal command passing a 'HL7' parameter. I believe it bubbles up a runtime error which shuts down the service. We prefer the option I described above as we can then invoke the service from other components (i.e. scheduling shutdowns of given services).

chrisallen21a at 2007-7-11 22:45:13 > top of Java-index,Java Enterprise System,Java Composite Application Platform Suite -- General Discussion...
# 2
Chrisallen21,That is great news. Would it be possible to get a copy of the jcd that locates the bean and then does the shut down?Regards,bill dot morrison 2 at nike dot com
AKsuiteda at 2007-7-11 22:45:13 > top of Java-index,Java Enterprise System,Java Composite Application Platform Suite -- General Discussion...
# 3
i would be interested too if you could send it without trouble.eingfoan AT YAHOO DOT de regards chris
eingfoana at 2007-7-11 22:45:13 > top of Java-index,Java Enterprise System,Java Composite Application Platform Suite -- General Discussion...
# 4
I would also be interestedjohnferron at alliantenergy dot comjohn
johnferrona at 2007-7-11 22:45:13 > top of Java-index,Java Enterprise System,Java Composite Application Platform Suite -- General Discussion...
# 5

Here are a couple of the methods that do most of the work. This assumes that you already have a reference to the managed bean server to iterate through the beans that it controls.

/** Retrieves the runtime name of a service

* @param collabName Name of service to retrieve runtime name for

* @return String Runtime deployment name

* @throws Exception

*/

public String getBeanRuntimeName(final String collabName)

throws Exception {

final String METHOD_NAME = "getBeanRuntimeName";

final String J2EE_APPLICATION = "J2EEApplication";

System.out.println(new StringBuffer(METHOD_NAME).append(" >>").toString());

String runtimeName = null;

String correctBeanName = new StringBuffer(collabName).append(".jar").toString();

final ObjectName objectName = new ObjectName(new StringBuffer("com.sun.appserv:j2eeType=StatelessSessionBean,name=").append(collabName)

.append(",EJBModule=").append(correctBeanName)

.append(",*,J2EEServer=server,category=runtime").toString());

final Set beanNames = this.mbeanServer.queryNames(objectName,null);

if (beanNames.isEmpty()){

System.out.println(new StringBuffer(METHOD_NAME).append(" Error: Did not locate any beans matching query").toString());

} else {

for (Iterator iterator = beanNames.iterator(); iterator.hasNext();) {

ObjectName objRetrieved = (ObjectName) iterator.next();

runtimeName = (String)this.mbeanServer.getAttribute(objRetrieved,J2EE_APPLICATION);

}

}

System.out.println(new StringBuffer(METHOD_NAME).append(" Retrieved J2EEAppliation name:").append(runtimeName).toString());

System.out.println(new StringBuffer(METHOD_NAME).append(" <<").toString());

return runtimeName;

}

/** Retrieves a Managed Bean

* @param domain Name of domain

* @param runtimeName Name of runtime project for bean

* @param deploymentProfile Name of deployment profile

* @param serviceName Name of service

* @throws MalformedObjectNameException, MalformedObjectNameException, Exception

*/

public ObjectName findManagedBean(final String domain, final String runtimeName, final String deploymentProfile, final String serviceName) throws MalformedObjectNameException, Exception {

final String METHOD_NAME = "findManagedBean";

System.out.println(new StringBuffer(METHOD_NAME).append(" >>").toString());

// create pattern to match the key

final String patternString = new StringBuffer( "^(" ).append( runtimeName ).append( ").*(" ).append( deploymentProfile ).append( "\\|" ).append( serviceName ).append( ")$" ).toString();

System.out.println(new StringBuffer(METHOD_NAME).append(" Searching for pattern:").append(patternString).toString());

final Pattern p = Pattern.compile( patternString);

final String objectNamePattern = new StringBuffer(domain).append(":*").toString();

System.out.println(new StringBuffer(METHOD_NAME).append(" Using object name pattern:").append(objectNamePattern).toString());

final ObjectName searchObjectName = new ObjectName( objectNamePattern);

final Set beanNames = this.mbeanServer.queryNames( searchObjectName, null );

ObjectName objectName = null;

String fullBeanPathName = null;

if (beanNames.isEmpty()) {

System.out.println(new StringBuffer(METHOD_NAME).append("Error: Did not locate any beans matching query").toString());

} else {

for (Iterator iterator = beanNames.iterator(); iterator.hasNext();) {

objectName = (ObjectName) iterator.next();

fullBeanPathName = objectName.getKeyProperty( "Name" );

if (fullBeanPathName != null) {

Matcher m = p.matcher( fullBeanPathName );

if (m.find()) {

System.out.println(new StringBuffer(METHOD_NAME).append(" Found bean:").append(objectName.getKeyPropertyListString()).toString());

break;

}

}

}

}

System.out.println(new StringBuffer(METHOD_NAME).append(" <<").toString());

return objectName;

}

The code to invoke the desired operation is as follows (we pass an integer to indicate the operation since the eDesigner doesn't support 1.5 in which case, we would use enums as they were meant to be used). The ServiceControl object is a class we created to control our services:

/*

Executes the requested operation on the bean

* @param objectName Bean to operate on

* @throws Exception

*/

protected void execute( final ObjectName objectName )

throws Exception

{

final String METHOD_NAME = "execute";

final int operationToInvoke = Integer.parseInt( this.inputMessage.retrieveUserProperty( "operation" ) );

final String logicalHostName = InetAddress.getLocalHost().getHostName();

final ServiceControl serviceControl = ServiceControlFactory.getInstance( collabContext, logicalHostName );

final MBeanServer mbeanServer = this.serviceControl.getManagedBeanServer();

boolean isValidOperation = false;

final String status = mbeanServer.getAttribute( objectName, "Status" ).toString().toUpperCase();

switch (operationToInvoke) {

case ServiceControl.STOP_MBEAN :

{

isValidOperation = true;

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

break;

}

case ServiceControl.START_MBEAN :

{

isValidOperation = true;

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

break;

}

case ServiceControl.RESTART_MBEAN :

{

isValidOperation = true;

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

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

break;

}

}

if (!isValidOperation) {

System.out.println( "Invalid operation" );

}

chrisallen21a at 2007-7-11 22:45:13 > top of Java-index,Java Enterprise System,Java Composite Application Platform Suite -- General Discussion...
# 6
Thank you very much.
AKsuiteda at 2007-7-11 22:45:13 > top of Java-index,Java Enterprise System,Java Composite Application Platform Suite -- General Discussion...
# 7
Very helpfull. Thank You
RajeshDuddupudia at 2007-7-11 22:45:13 > top of Java-index,Java Enterprise System,Java Composite Application Platform Suite -- General Discussion...
# 8
hi chrisallen21,thank you for giving away the code. with which versions of CAPS is this code tested / compiled?regards chris
eingfoana at 2007-7-11 22:45:13 > top of Java-index,Java Enterprise System,Java Composite Application Platform Suite -- General Discussion...
# 9
Java CAPS 5.1.1
chrisallen21a at 2007-7-11 22:45:13 > top of Java-index,Java Enterprise System,Java Composite Application Platform Suite -- General Discussion...