User: andreas
Date: 00/12/07 21:33:20
Added: documentation JMX.Timer.HowTo.html
Log:
Add the new JMX Timer HowTo and also created a
table for the HowTos to add a Version number and a
Last Updated Date.
To all HowTo writers please add these informations.
Revision Changes Path
1.1 newsite/documentation/JMX.Timer.HowTo.html
Index: JMX.Timer.HowTo.html
===================================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>How To use the Timer MBean</TITLE>
<LINK REL="stylesheet" type="text/css" href="HowTo.css" >
</HEAD>
<BODY>
<DIV class="page">
<H1>How To us the Timer MBean</H1>
<H2>Introduction</H2>
<DIV class="para" ALIGN="left">
As part of the JMX specification each JMX compliant server must provide a timer
service to let the users beeing notified at a certain time, in a certain
interval and/or number of occurrences.
</DIV>
<DIV class="para" ALIGN="left">
Therefore you can check for mails, check if some change on target (auto
deployer) or notify the client for a date.
</DIV>
<H2>Preparation</H2>
<DIV class="para" ALIGN="left">
First you have to add the timer service into the jboss.conf therefore that the
timer service is loaded, registered at the JMX server and the initialized and
started (done by jBoss's Main.java class). This MLET tag looks like this:
<PRE class="code" ALIGN="left">
<MLET CODE = "javax.management.timer.Timer" NAME="DefaultDomain:service=timer"
ARCHIVE="jmxri.jar" CODEBASE="../../lib">
</MLET>
</PRE>
</DIV>
<DIV class="para" ALIGN="left">
If you are not using jBoss then to the following:
<OL>
<LI>
Create a MBeanServer
<PRE class="code" ALIGN="left">
MBeanServer lServer = MBeanServerFactory.createMBeanServer();
</PRE>
</LI>
<LI>
Load and register the Timer MBean
<PRE class="code" ALIGN="left">
ObjectInstance lTimer = lServer.createMBean(
"javax.management.timer.Timer",
new ObjectName( "DefaultDomain", "service", "timer" )
);
</PRE>
</LI>
<LI>
Initialize and start the timer service
<PRE class="code" ALIGN="left">
lServer.invoke(
lTimer.getObjectName(),
"init",
new Object[] {},
new String[] {}
);
lServer.invoke(
lTimer.getObjectName(),
"start",
new Object[] {},
new String[] {}
);
</PRE>
</LI>
</OL>
</DIV>
<DIV class="para" ALIGN="left">
Next step is to get the MBeanServer within your object to work with the timer.
<UL>
<LI>
This is quite simple if your are in a MBean registered to the same JMX server
because then you get it when you overwrite preRegister() method.
</LI>
<LI>
When you are in the same JVM as the JMX server (in jBoss is any instance
running within jBoss like EJBs or other classes). Then you can obtain the
MBeanServer throug:
<PRE class="code" ALIGN="left">
MBeanServer lServer = MBeanServerFactory.createMBeanServer();
</PRE>
</LI>
<LI>
For the rest it is a little bit more complicated. In a Java client you can use
jBoss RMI connector which will be released as separat package till mid December
2000. Then you connect to a MBeanServer through the JMXConnector interface
which is more or less the same.
</LI>
</DIV>
<DIV class="para" ALIGN="left">
We are nearly there: now we need the reference to the timer service to work on
it (lServer can be either of type MBeanServer or JMXConnector):
<PRE class="code" ALIGN="left">
Set lBeans = lServer.queryMBeans(
new ObjectName( "DefaultDomain", "service", "timer" ),
null
);
if( !lBeans.isEmpty() ) {
// Should be the first and only element
ObjectInstance lTimer = (ObjectInstance) lBeans.iterator().next();
</PRE>
</DIV>
<DIV class="para" ALIGN="left">
Let's go to work with the timer. Because the timer sends a Notification Event
to the listeners we have to register first:
<PRE class="code" ALIGN="left">
lServer.addNotificationListener(
lTimer.getObjectName(),
new Listener(),
// No filter
null,
// No object handback necessary
null
);
</PRE>
The Listener (in this case) is an inner class implementing the
NotificationListener interface:
<PRE class="code" ALIGN="left">
public class Listener
implements NotificationListener
{
public handleNotification(
Notification pNotification,
Object pHandback
) {
// Here to whatever you want or call a method
// in the outer class
System.out.println( "You got a Notification: " + pNotification );
}
}
</PRE>
</DIV>
<DIV class="para" ALIGN="left">
Finally we are ready to rock and roll. We set a timer event for a particular
time and at this time the Listener.handleNotification() get called.
<PRE class="code" ALIGN="left">
Integer lOneMinuteTimer = lServer.invoke(
lTimer.getObjectName(),
"addNotification",
new Object[] {
"IDoNotKnowWhatTypeIs",
"I call you with this timer once",
// No user object
null,
// I one minute from now
new Date( new Date().getTime() + Timer.ONE_MINUTE ),
},
new String[] {
String.getClass().getName(),
String.getClass().getName(),
Object.getClass().getName(),
Date.getClass.getName()
}
);
</PRE>
</DIV>
<DIV class="para" ALIGN="left">
A timer notification after an Hour from now repeating
every minute for ten times.
<PRE class="code" ALIGN="left">
Integer lOneHourTimer = lServer.invoke(
lTimer.getObjectName(),
"addNotification",
new Object[] {
"IDoNotKnowWhatTypeIs",
"I call you with this timer once",
// No user object
null,
// I one minute from now
new Date( new Date().getTime() + Timer.ONE_HOUR ),
Timer.ONE_MINUTE,
10
},
new String[] {
String.getClass().getName(),
String.getClass().getName(),
Object.getClass().getName(),
Date.getClass.getName(),
Long.TYPE.getName(),
Long.TYPE.getName()
}
);
</PRE>
</DIV>
<DIV class="para" ALIGN="left">
If you want to get ride of the second timer then do:
<PRE class="code" ALIGN="left">
lServer.invoke(
lTimer.getObjectName(),
"removeNotification",
new Object[] {
// You could also use the type: "IDoNotKnowWhatTypeIs"
lOneHourTimer
},
new String[] {
// If you remove by type: String.getClass().getName()
Integer.TYPE.getName()
}
);
</PRE>
</DIV>
<DIV class="para" ALIGN="left">
Now the rest is quite simple. Have a look at the javax.management.Timer class
description and use the MBeanServer.invoke() method style. <B>Attention:</B>
When you have basic data type in the method signature then you have to use its
wrapper class TYPE variable to get its class instead of using just "long" etc.
<DIV class="footer" ALIGN="left">
If anything is wrong or not correct please contact me at
<A HREF="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</A>.
Also if you want to know more in detail or have a request for further infos.
</DIV>
</DIV>
</BODY>
</HTML>