My JMX implementation is significantly different (less robust) that the jBoss
implementation, but here is a sample how an MBean (containing a reference to the
server) can use the timer...
I hope that there is enough detail here to be helpful...
- jeremiah
/*
*
* part of my application is a service that checks for new
* mail every {interval} seconds - I want that interval to
* be configurable at run-time, so I made it an MBean
*
*/
public int getInterval() {
return interval;
}
public void setInterval( int interval ) {
this.interval = interval;
/*
* remove the old notification timer
*/
removeNotifications();
/*
* add the new notification timer
*/
addNotification();
}
/*
*
* to get the whole thing started, I am using the postRegister
* method which means I have to implement the MBeanRegistration
* interface -> Bonus: I get the MBean server reference in the
* preRegister method...
*
*/
public void postRegister( Boolean registrationDone ) {
addNotificationListener();
addNotification();
}
/*
*
* I ignore the message and user data as I am just interested
* in getting a wake-up call every {interval} seconds
*
*/
public void handleNotification( Notification notification, Object object ) {
System.out.println( "SendOutgoing.handleNotification called." );
checkForOutgoingMail();
}
/*
*
* adding a notification timer that runs {interval} seconds
* of type 'org.opengroupware.server.mail.SendOutgoing'
* Note: I am adding the timer from the bean that wants to
* use the timer.
*
*/
protected void addNotification() {
try {
mbeanServer.invoke(
// new ObjectName("Service:name=timer"),
timerObjectName,
"addNotification",
new Object[] {
"org.opengroupware.server.mail.SendOutgoing",
null,
null,
new Date(),
new Long(interval * 1000)
},
new String[] {
"java.lang.String",
"java.lang.String",
"java.lang.Object",
"java.util.Date",
"long"
}
);
} catch( Exception e ) {
// MalformedObjectNameException, InstanceNotFoundException
System.out.println( "Fatal error setting up timer: " + e );
System.exit(1);
}
}
/*
*
* adding the notification listener
*
*/
protected void addNotificationListener() {
try {
mbeanServer.addNotificationListener(
// new ObjectName("Service:name=timer"),
// new ObjectName("Service:name=sendOutgoing"),
timerObjectName,
sendOutgoingObjectName,
null,
null
);
} catch( Exception e ) {
// MalformedObjectNameException, InstanceNotFoundException
System.out.println( "Fatal error adding listener: " + e );
System.exit(1);
}
}
------Original Message------
From: "Schaefer, Andreas" <[EMAIL PROTECTED]>
To: "'[EMAIL PROTECTED]'" <[EMAIL PROTECTED]>
Sent: November 29, 2000 5:40:58 PM GMT
Subject: [jBoss-Dev] JMX Timer example
Hi Geeks
How fast the time runs when you are busy!!!
I promised to bring an example how to use the TIMER
agent service to be notified once or periodically
at a given time (and period).
These are the steps:
- add a <MLET> to the jboss.conf telling to load
"javax.management.timer.Timer" in jmxri.jar.
- get the MBeanServer either through JNDI if you are
in the same JVM or through JMX Connector when outside
- get the Timer MBean through MBeanServer/JMX Connector
- add a notification with addNotification()
- add yourself as listener to the Timer MBean
I should come up with a code example by the end of the
week.
Have fun - Mad Andy / Better Pizza
______________________________________________
FREE Personalized Email at Mail.com
Sign up at http://www.mail.com/?sr=signup