MBean Startup Class

From PlasmaWiki

Jump to: navigation, search

In one of my web based applications I need to monitor a datbase every so often to make sure that session information was expired and that I had more control over session happenings than just the default session expiration behavior built into Tomcat. So I did a little research and figured out how to build a service for JBoss that was loaded and run when deployed.

I'm not going to include all the threading code here but rather focus on just building the service. If you are interested in the good threading code go here: Thread Monitor. Also, this class does not bind a JNDI lookup.. I didn't need that functionality. Check out the references at the end of this area if you need that.

Contents

[edit] Step 1: Create an interface for your service


import org.jboss.mx.util.ObjectnameFactory;
import org.jboss.system.ServiceMBean;

public interface LoomMonitorMBean extends ServiceMBean {

  public static final javax.management.ObjectName OBJECT_NAME =
    ObjectnameFactory.create( ":service=LoomMonitor");

  void setMonitorTimeout( Integer i);
  void setSessionTimeout( Integer i);
}

It's VERY important that you name this interface ending with MBean. Otherwise [JBoss] won't know that it is one.

[edit] Step 2: Create a implementation of the interface.

Be very sure that the name of the class is the same as the interface minus the _MBean_. Also, the names of the setters are important, be sure to remember them for the next step.

import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
...
  _other imports_
...

public class LoomMonitor extends ServiceMBeanSupport implements LoomMonitorMBean {

  private Integer _monitorTimeout = 1;
  private Integer _sessionTimeout = 30;
  ...
    _threading stuff_
  ...
  public ObjectName getObjectName( MBeanServer server, ObjectName name) throws MalformedObjectNameException {
    return name == null ? OBJECT_NAME : name;
  }
  public void startService() throws Exception {
    // called when the service descriptor is deployed
  }
  public void stopService() {
    // called when the service descriptor is undeployed
  }
  public void setMonitorTimeout( Integer i) {
    _monitorTimeout = i;
  }
  public void setSessionTimeout( Integer i) {
    _sessionTimeout = i;
  }
}

[edit] Step 3: Service Descriptor

This file is the service descriptor. It does not reside in the jar file, it must be deployed separately in the same deployment directory as your jar file.

<server>
  <classpath codebase="." archives="LoomBE.jar" />
  <mbean code="org.loom.system.LoomMonitor" name=":service=LoomMonitor" >
    <attribute name="MonitorTimeout">1</attribute>
    <attribute name="SessionTimeout">10</attribute>
  </mbean>
</server>

So, there you go. Once you deploy both the jar and the service descriptor you'll be running with your service.

[edit] Update - EJB 3.0 Service

With the coming of EJB 3.0, JBoss has defined a very easy and nice way for creating services. Now instead of having to do all of the above a lot of the redundant code has been removed.

Take a look at the Service POJOs tutorial here: JBoss EJB3

[edit] JMX MBean

I've gotten to the place where I've found that using a JMX MBean is a better way to have a singleton bean running within an app server. This way you can use the full JMX management interfaces to control your instance. This seems like a better way to handle things than using a startup class.

[edit] How-To

[edit] Define an Interface

public interface TheServiceMBean {
	public void start();
	public void stop();
	public Boolean getRunning();
}

[edit] Define the Implementation

public class TheService extends StandardMBean implements TheServiceMBean {
	protected Boolean running = Boolean.FALSE;
	public void start() {
		running = Boolean.TRUE;
	}
	public void stop() {
		running = Boolean.FALSE;
	}
	public MBeanInfo getMBeanInfo() {
		MBeanAttributeInfo[] matts = {
			new MBeanAttributeInfo(
				"Running", // name
				"java.lang.Boolean", // type
				"Is this service currently active? This value changes when start() or stop() is called.", //desc
				true, // readable
				false, // writable
				false) // isIS
		};
		MBeanOperationInfo[] mops = {
			new MBeanOperationInfo("start",
				"Starts the Service", null,
				"void", MBeanOperationInfo.ACTION_INFO),
			new MBeanOperationInfo("stop",
				"Stops the Service",
				null, "void", MBeanOperationInfo.ACTION_INFO)
		};
	Class c = this.getClass();
	Constructor[] theConstructors = c.getConstructors();
	MBeanConstructorInfo[] mcons = { new MBeanConstructorInfo(
		"The default constructor", theConstructors[0]) };
	MBeanInfo myInfo = new MBeanInfo(this.getClass().getName(),
		"JRE Subscription Service.", matts, mcons, mops, null);
	return myInfo;
     }
}

[edit] Deployment Descriptors

[edit] Oracle OC4J 10.1.3
	<orion-application>
		<jmx-mbean objectname=":name=TheService"
			class="TheService">
		</jmx-mbean>
	</orion-application>

[edit] References

JBoss MBeans

JBoss Online Manual -- this site also contains information on doing a schedules service.

Personal tools
admins only