User: schaefera
Date: 01/05/20 13:33:01
Modified: src/docs howtotimer.xml
Log:
Changed and fixed the layout to make the code read easier.
Revision Changes Path
1.3 +237 -206 manual/src/docs/howtotimer.xml
Index: howtotimer.xml
===================================================================
RCS file: /cvsroot/jboss/manual/src/docs/howtotimer.xml,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- howtotimer.xml 2001/03/21 08:30:20 1.2
+++ howtotimer.xml 2001/05/20 20:33:01 1.3
@@ -1,208 +1,239 @@
<?xml version = "1.0" encoding = "UTF-8"?>
<section>
- <title>How To us the Timer MBean</title>
- <para>Author:<author>
- <firstname>Andreas</firstname>
- <surname>Shaefer</surname>
- </author>
- <email>[EMAIL PROTECTED]</email>
- </para>
- <section>
- <title>Introduction</title>
-
- <para> 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.
- Therefore you can check for mails, check if some change on target (auto
deployer) or notify the client for a
- date.</para>
-
- </section>
- <section>
- <title>Preparation</title>
-
- <itemizedlist>
- <listitem>
- <para> 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:
- <programlisting><![CDATA[
- <MLET CODE = "javax.management.timer.Timer"
- NAME="DefaultDomain:service=timer"
- ARCHIVE="jmxri.jar" CODEBASE="../../lib"> </MLET>
- ]]></programlisting>
-
-
- </para>
-
- </listitem>
-
- <listitem>
-
- <para> If you are not using JBoss then to the
following:
-
- <orderedlist>
- <listitem>
- <para> Create a
MBeanServer
-
- <programlisting> MBeanServer lServer =
- MBeanServerFactory.createMBeanServer();
- </programlisting>
- </para>
- </listitem>
- <listitem>
- <para> Load and
register the Timer MBean
-
- <programlisting> ObjectInstance lTimer =
- lServer.createMBean(
- "javax.management.timer.Timer", new
- ObjectName( "DefaultDomain",
- "service", "timer" ) );
- </programlisting>
-
- </para>
- </listitem>
-
- <listitem>
- <para>Initialize and start the
timer service
-
- <programlisting> lServer.invoke(
- lTimer.getObjectName(), "init", new
- Object[] {}, new String[] {} );
- lServer.invoke(
- lTimer.getObjectName(), "start", new
- Object[] {}, new String[] {} );
- </programlisting>
-
- </para>
- </listitem>
- </orderedlist>
-
- </para>
- </listitem>
-
-
- <listitem>
-
- <para>Next step is to get the MBeanServer within your
object to work with the timer.</para>
-
- <para>
- 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.
- 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: </para>
- <programlisting> MBeanServer
lServer =
- MBeanServerFactory.createMBeanServer();</programlisting>
- <para> 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. </para>
-
- </listitem>
-
- <listitem>
-
- <para> 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): </para>
- <programlisting>
- 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();
- </programlisting>
- </listitem>
-
-
- <listitem>
-
- <para> Let's go to work with the timer. Because
the timer sends a Notification Event to the listeners we have to
- register first: </para>
- <programlisting>
- lServer.addNotificationListener(
- lTimer.getObjectName(), new Listener(), // No
- filter null, // No object handback necessary null
- );</programlisting>
-
- </listitem>
-
-
- <listitem>
-
- <para>
- The Listener (in this case) is an inner class implementing the
NotificationListener interface: </para>
- <programlisting>
- 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 ); } } </programlisting>
-
- </listitem>
-
-
- <listitem>
-
- <para> 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.</para>
-
-
- <programlisting> 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() } ); </programlisting>
- </listitem>
-
-
- <listitem>
-
- <para> A timer notification after an Hour from
now repeating every minute for ten times.</para>
-
-
- <programlisting> 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() } ); </programlisting>
- </listitem>
-
-
- <listitem>
-
- <para> If you want to get ride of the second
timer then do:</para>
-
- <programlisting>
- 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() } ); </programlisting>
-
- </listitem>
-
- </itemizedlist>
-
- <para>
- Now the rest is quite simple. Have a look at the javax.management.Timer
class description and use the
- MBeanServer.invoke() method style.</para>
-
-
- <para>Attention: 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.</para>
-
- <para>If anything is wrong or not correct please contact me at
[EMAIL PROTECTED] Also if
- you want to know more in detail or have a request for further infos.</para>
-
- </section>
-</section>
\ No newline at end of file
+ <title>How To us the Timer MBean</title>
+ <para>Author:
+ <author>
+ <firstname>Andreas</firstname>
+ <surname>Schaefer</surname>
+ </author>
+ <email>[EMAIL PROTECTED]</email>
+ </para>
+ <section>
+ <title>Introduction</title>
+ <para>
+ 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.
+ Therefore you can check for mails, check if some change on target (auto
deployer) or notify the
+ client for a date.
+ </para>
+ </section>
+ <section>
+ <title>Preparation</title>
+ <itemizedlist>
+ <listitem>
+ <para>
+ First you have to add the timer service into the jboss.jcml
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 mbean tag looks like this:
+ <programlisting><![CDATA[
+ <mbean code="javax.management.timer.Timer" name="DefaultDomain:service=timer"/>
+ ]]></programlisting>
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ If you are not using JBoss then to the following:
+ <orderedlist>
+ <listitem>
+ <para>
+ Create a MBeanServer
+ <programlisting>
+ MBeanServer lServer = MBeanServerFactory.createMBeanServer();
+ </programlisting>
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ Load and register the Timer MBean
+ <programlisting>
+ ObjectInstance lTimer = lServer.createMBean(
+ "javax.management.timer.Timer",
+ new ObjectName( "DefaultDomain", "service", "timer" )
+ );
+ </programlisting>
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ Initialize and start the timer service
+ <programlisting>
+ lServer.invoke(
+ lTimer.getObjectName(),
+ "init",
+ new Object[] {},
+ new String[] {}
+ );
+ lServer.invoke(
+ lTimer.getObjectName(),
+ "start",
+ new Object[] {},
+ new String[] {}
+ );
+ </programlisting>
+ </para>
+ </listitem>
+ </orderedlist>
+ </para>
+ </listitem>
+ <listitem>
+ <para>Next step is to get the MBeanServer within your object to work
with the timer.</para>
+ <para>
+ 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.
+ 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:
+ </para>
+ <programlisting>
+ MBeanServer lServer =
+ MBeanServerFactory.createMBeanServer();
+ </programlisting>
+ <para>
+ 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.
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ 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):
+ </para>
+ <programlisting>
+ 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();
+ </programlisting>
+ </listitem>
+ <listitem>
+ <para>
+ Let's go to work with the timer. Because the timer sends a
Notification Event to the
+ listeners we have to register first:
+ </para>
+ <programlisting>
+ lServer.addNotificationListener(
+ lTimer.getObjectName(),
+ new Listener(),
+ // No filter
+ null,
+ // No object handback necessary
+ null
+ );
+ </programlisting>
+ </listitem>
+ <listitem>
+ <para>
+ The Listener (in this case) is an inner class implementing the
NotificationListener
+ interface:
+ </para>
+ <programlisting>
+ 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 );
+ }
+ }
+ </programlisting>
+ </listitem>
+ <listitem>
+ <para>
+ 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.
+ </para>
+ <programlisting>
+ Date lNext = new Date( new Date().getTime() + Timer.ONE_HOUR );
+ 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
+ lNext,
+ },
+ new String[] {
+ "".getClass().getName(),
+ "".getClass().getName(),
+ "java.lang.Object",
+ lNext.getClass().getName()
+ }
+ );
+ </programlisting>
+ </listitem>
+ <listitem>
+ <para>
+ A timer notification after an Hour from now repeating every minute
for ten times.
+ </para>
+ <programlisting>
+ Date lNext = new Date( new Date().getTime() + Timer.ONE_HOUR );
+ 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
+ lNext,
+ Timer.ONE_MINUTE,
+ 10
+ },
+ new String[] {
+ "".getClass().getName(),
+ "".getClass().getName(),
+ "java.lang.Object",
+ lNext.getClass().getName(),
+ Long.TYPE.getName(),
+ Long.TYPE.getName()
+ }
+ );
+ </programlisting>
+ </listitem>
+ <listitem>
+ <para>If you want to get ride of the second timer then do:</para>
+ <programlisting>
+ 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()
+ }
+ );
+ </programlisting>
+ </listitem>
+ </itemizedlist>
+ <para>
+ Now the rest is quite simple. Have a look at the javax.management.Timer
class description and
+ use the MBeanServer.invoke() method style.
+ </para>
+ <para>
+ Attention: 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.
+ </para>
+ <para>
+ If anything is wrong or not correct please contact me at
[EMAIL PROTECTED] Also if
+ you want to know more in detail or have a request for further infos.
+ </para>
+ </section>
+</section>
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development