📄 ch13s58.html
字号:
called by the Scheduler service when it receives a Notification from the Timer.
The MBean must be loaded, created and started when the Scheduler calls the
MBean for the first time. If not available the Scheduler will count this call
(decrease the remaining repetitions if not endless) but the call is not performed.
</p></li></ol></div></div><div class="section"><a name="scheduler-usage"></a><div class="titlepage"><div><h3 class="title"><a name="scheduler-usage"></a>Using the Scheduler</h3></div></div><p>
The Scheduler is programmed in a way that you only have to configure and start it
to use it. The next code snippet can be used to add it to jboss-service.xml file or
create your own scheduler-service.xml file. When you use a JMX Connector then you
can create the Scheduler MBean, set the Attributes and then invoke the <tt>
init</tt> and <tt>start</tt> methods (please have a look at the
timer-usage above for more informations). The Scheduler can be setup in two ways.
<div class="orderedlist"><ol type="1"><li><p><a name="d0e10443"></a>
To setup a Scheduler in a service.xml file using a Schedulable class add
this code snippet and adjust it to your needs:
<pre class="programlisting">
<mbean code="org.jboss.util.Scheduler" name=":service=Scheduler">
<attribute name="SchedulableClass">org.jboss.util.Scheduler$SchedulableExample</attribute>
<attribute name="SchedulableArguments">Schedulabe Test,12345</attribute>
<attribute name="SchedulableArgumentTypes">java.lang.String,int</attribute>
<attribute name="InitialStartDate">0</attribute>
<attribute name="SchedulePeriod">10000</attribute>
<attribute name="InitialRepetitions">-1</attribute>
<attribute name="StartAtStartup">true</attribute>
</mbean>
</pre>
</p></li><li><p><a name="d0e10449"></a>
To setup a Scheduler in a service.xml file using a Schedulable MBean add
this code snippet and adjust it to your needs:
<pre class="programlisting">
<mbean code="org.jboss.util.SchedulableExample" name="jboss:type=example,name=schedulable">
</mbean>
<mbean code="org.jboss.util.Scheduler" name="jboss:service=Scheduler">
<attribute name="StartAtStartup">true</attribute>
<attribute name="SchedulableMBean">jboss:type=example,name=schedulable</attribute>
<attribute name="SchedulableMBeanMethod">hit( NOTIFICATION, DATE, REPETITIONS, SCHEDULER_NAME, java.lang.String )</attribute>
<attribute name="InitialStartDate">NOW</attribute>
<attribute name="SchedulePeriod">10000</attribute>
<attribute name="InitialRepetitions">10</attribute>
</mbean>
</pre>
Note that the first MBean is the target (Schedulable) MBean which is created
before the Scheduler. This is not necessary especially when you want to use
JBoss MBeans but they should be available when the first hit (see initial start
date) from the Timer is in.
</p></li></ol></div>
<div class="important" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10456"></a>Important</h3><p>
Do NOT mix these two types. The last call to either <tt>setSchedulableClass</tt>
or <tt>setSchedulableMBean</tt> will determine if a Schedulable Class or
MBean is used.
</p></div>
<div class="variablelist"><dl><dt><a name="d0e10467"></a><span class="term">SchedulableClass</span></dt><dd><p><a name="d0e10470"></a>
Full qualified path to the class implementing the Schedulable interface. Whenever
a Time Notification is sent to the Scheduler the Schedulable's <tt>perform</tt>
method will be called. Your Schedulable instance can then peform any necessary steps
you have to perform at a certain time.
</p></dd><dt><a name="d0e10476"></a><span class="term">InitArguments</span></dt><dd><p><a name="d0e10479"></a>
Contains the text representation of the attributes to create the Schedulable
instance. If the needs arise I will also add a way to create a Schedulable MBean
and pass the JMX Object Name instead (see J2eeDeployer) instead.
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10482"></a>Note</h3>
Only basic data types, Strings and objects taking a String a single argument
of the constructor are supported now. Objects are created with respect to the
given argument types below.
</div>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10485"></a>Note</h3>
Arguments are separated by a comma and therefore you must not use commas in
the attribute value (escaping is not supported yet).
</div>
</p></dd><dt><a name="d0e10488"></a><span class="term">InitTypes</span></dt><dd><p><a name="d0e10491"></a>
Data types of the constructor you want to use to create the Schedulable instance.
The number and type must match with the given InitArguments above. It contains
Name of the basic data type (int, long, byte, char, float, double) or the fully
qualified path of a class like java.lang.String which must have a constructor
taking a single String argument.
</p></dd><dt><a name="d0e10494"></a><span class="term">SchedulableMBean</span></dt><dd><p><a name="d0e10497"></a>
Object Name of the MBean you want to be called when timer send a notification.
Note that this must not be null and must be a valid JMX Object Name.
</p></dd><dt><a name="d0e10500"></a><span class="term">SchedulableMBeanMethod</span></dt><dd><p><a name="d0e10503"></a>
Can contain two parts. The first is the method name which must be in front of
the opening bracket. The second is inside the opening and closing bracket. There
must not be any other characters after the closing bracket except white spaces
which will be trimmed away with <tt>java.lang.String.trim()</tt>.
The second part must be a comma seperated list of the following items (if written
in uppercase then this literal without the colon):
<div class="itemizedlist"><ul><li><p><a name="d0e10510"></a>
NOTIFICATION: Timer Notification object sent by the Timer. The signature
is: javax.management.Notification.
</p></li><li><p><a name="d0e10513"></a>
DATE: Date instance when the timer sent the call. The signature is:
java.util.Date.
</p></li><li><p><a name="d0e10516"></a>
REPETITIONS: Number of remaining repetitions. The signature is: long.
</p></li><li><p><a name="d0e10519"></a>
SCHEDULER_NAME: Object Name of the Scheduler calling the MBean. With
this Object Name you can call back and investigate the Scheduler. The
signature is: javax.management.ObjectName.
</p></li><li><p><a name="d0e10522"></a>
Anything else will be treated as fully qualified Class name where the
Scheduler send a "null" as value but this Class as signature. This
method is here to satisfy your method signature.
</p></li></ul></div>
When you omit the method name "perform" will be set. If you omit the parameter
list (second part) then the same signature as <tt>perform</tt> of
the Schedulable instance will be assumed.
</p></dd><dt><a name="d0e10529"></a><span class="term">InitialStartDate</span></dt><dd><p><a name="d0e10532"></a>
You have now 3 ways to set the Initial Start Date:
<div class="itemizedlist"><ul><li><a name="d0e10536"></a>
NOW: Current date when the Scheduler is started
</li><li><a name="d0e10538"></a>
Milliseconds since 1/1/1970: Date (in milliseconds since 1/1/1970) when the
Timer sends the first Time Notification.
</li><li><a name="d0e10540"></a>
Date Pattern as String: Date as a String understood by the
java.text.SimpleDateFormat (please check your environment). For US this
looks like this: 2/5/02 11:15 am (Fith of February 2002, eleven fifeteen
in the morning).
</li></ul></div>
Note that when a date is set in the past the Scheduler will see if the Schedule
would have a hit in the future with the given period and number of repetitions.
If the number of repetitions is endless the Scheduler will start at the first
scheduled time in the future with respect to the start date and period. Otherwise
if a hit is in the future the Schedule will schedule this one but also reduce
the number of hits which were in the past. If no hit is in the future the Schedule
will be inactive even thought it is started.
</p><p>
The reason to do so is to allow an administrator to restart the JBoss application
server without adjusting the Scheduler every time. If for example you started a
Scheduler on Monday to run every day at 12:00pm for a week (7 times) and you
restart JBoss on Thursday after 12:00pm the same week then the Scheduler will
run for Friday, Saturday and Sunday at 12:00pm.
</p></dd><dt><a name="d0e10545"></a><span class="term">NrOfRepetitions</span></dt><dd><p><a name="d0e10548"></a>
You can define the number of repetitions of timed calls. If you specify -1 then
there is no limit. When all the calls are sent the Scheduler falls into hibernation
because when no Time Notification is sent nothing is going to happen. But then
you can either reuse the Scheduler or remove the MBean from the JMX Agent.
</p></dd><dt><a name="d0e10551"></a><span class="term">StartAtStartup</span></dt><dd><p><a name="d0e10554"></a>
Either "true" or "false" indicats if the Scheduler should be started after
creation or it should wait until you are ready and start the Scheduler by
hand (HTML-Adaptor), another JMX MBean or a client using JMX Connector/Adaptor.
</p></dd></dl></div>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10558"></a>Note</h3>
The first version of the Scheduler passed these values as constructor arguments instead of using
the named attributes here. In this new version it is not necessary but you can still pass
the arguments to the constructor directly.
</div>
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10561"></a>Note</h3>
The Scheduler can be reused when the original schedule is not used anymore or the number of
repetitions felt to 0 meaning no Time Notifications are sent anyhow. But whenever you change
the attributes of the Scheduler you have to restart the Scheduler thus the
change becomes effective. To restart use the <tt>stopScheduler</tt> and <tt>
startSchedule</tt> methods. The <tt>stopScheduler</tt> method needs a boolean
argument indicating if Scheduler should be stopped immediately or wait until the next hit is
performed. Whenever you stop the Scheduler with argument "false" the <tt>isRestartPending
</tt> returns "true" until the Scheduler is stopped. The other <tt>stopService</tt>
belongs to the life-cycle of the MBean and should only be used in this context.
</div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10578"></a>Note</h3>
The Scheduler is showing this message when the Scheduler remaining repetitions went to 0
and became inActive:
<pre class="programlisting">
02:12:00,908 ERROR [Default] java.lang.NullPointerException
02:12:00,908 ERROR [Default] at javax.management.timer.Timer.updateTimerTable(Timer.java:984)
02:12:00,908 ERROR [Default] at javax.management.timer.Timer.notifyAlarmClock(Timer.java:1093)
02:12:00,918 ERROR [Default] at javax.management.timer.TimerAlarmClock.run(Timer.java:1165)
</pre>
Please just ignore this message because it is catched and then reported inside JMX. It seems
to be a bug from a bug fix of Sun.
</div></div><div class="section"><a name="timer-conclusion"></a><div class="titlepage"><div><h3 class="title"><a name="timer-conclusion"></a>Conclusion</h3></div></div><p>
The Timer MBean is not that difficult to use as soon as you understand what the differences
are compared to other scheduling services like CRON. If you are just interested to get the hit after
a certain time with a certain number of repetitions the Scheduler is an easy way to use the
Timer MBean. The Scheduler hides all the details of the Timer and you can focus on the
implementation of what you have to perform when time is up.
</p><p>
If anything is wrong or not correct please contact me at andreas@jboss.org. Also if
you want to know more in detail or have a request for further infos.
</p></div></div><table border="0" cellpadding="0" cellspacing="0" height="65"><tr height="65"><td rowspan="2"><img src="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="432" height="79"></td><td rowspan="2" background="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="100%" align="right" valign="top"><a href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html"><img src="doc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/doc.gif" border="0"></a><a href="ch13.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13.html"><img src="toc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/toc.gif" border="0"></a><a href="ch13s55.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s55.html"><img src="prev.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/prev.gif" border="0"></a><a href="ch13s72.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s72.html"><img src="next.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/next.gif" border="0"></a></td></tr><tr></tr></table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -