⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch13s58.html

📁 详细介绍了jboss3.0的配置等
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>How To use the Timer MBean and Scheduler Service on JBoss 3</title><link rel="stylesheet" href="styles.css" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/styles.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets Vimages/callouts/"><link rel="home" href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html" title="JBoss 3.0 Documentation"><link rel="up" href="ch13.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13.html" title="Chapter 13. HOWTO"><link rel="previous" href="ch13s55.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s55.html" title="How To use the Timer MBean"><link rel="next" href="ch13s72.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s72.html" title="Deployment on JBoss"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table border="0" cellpadding="0" cellspacing="0" height="65"><tr height="65"><td rowspan="2"><img src="jboss.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/jboss.gif" border="0"></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><div class="section"><a name="howto.timer.II"></a><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="howto.timer.II"></a>How To use the Timer MBean and Scheduler Service on JBoss 3</h2></div></div><p>Author:
      <span class="author">Andreas Schaefer</span>
      <tt>&lt;<a href="mailto:andreas@jboss.org">andreas@jboss.org</a>&gt;</tt>
   </p><div class="section"><a name="Introduction"></a><div class="titlepage"><div><h3 class="title"><a name="Introduction"></a>Introduction</h3></div></div><p>
         As part of the JMX specification each JMX compliant agent must provide a timer service to trigger
         notifications sent at a certain time, in a certain interval and/or number of occurrences.
         Therefore you can check for mails, check for changes on a target (auto deployer) or notify the
         client about a date.
      </p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10181"></a>Note</h3>
         The Time Service only notifies the client that time is up and does not start any script or does
         anything else. It is up to the client implementation to perform whatever he/she has in mind.
         This is the big difference to other timely services like CRON etc.
      </div><div class="variablelist"><dl><dt><a name="d0e10184"></a><span class="term">Timer Service, </span><span class="term">Timer MBean</span></dt><dd><p><a name="d0e10189"></a>
                  The Timer Service in a JMX Agent is implemented by the Timer MBean.
                  In this HowTo it means the same whereas Timer Service more stresses the
                  fact of being an Agent Service and Timer MBean more stresses the fact of
                  being an MBean.
               </p></dd><dt><a name="d0e10192"></a><span class="term">Event Item</span></dt><dd><p><a name="d0e10195"></a>
                  An Event-Item is an added "Notification" on the Timer MBean. Please note
                  that this is not the same as the adding of a Notification Listener on the
                  JMX Agent which is also the reason to name it differently here. Whenever
                  a client creates a new Event-Item through the various <tt>addNotification</tt>
                  methods it will send one or more notifications to the registered notification
                  listener of the Timer MBean.
               </p></dd><dt><a name="d0e10201"></a><span class="term">Timer Notification Listener</span></dt><dd><p><a name="d0e10204"></a>
                  Every client who wants to receive notification about an "Event Item" must
                  register a Notification Listener. The Notification Listener can belong to another
                  client than the one adding the Event Item(s). Each Notification Listener will
                  without filtering receiver all the notifications triggered by all added Event Items
                  on this Timer MBean. As example two clients added one Event Item and one Notification
                  Listener. Now both Notification Listeners will receive all the notifications triggered
                  by the two Event Items and not only the notifications trigger from their Event Item.
                  <div class="important" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10207"></a>Important</h3>
                     Always deliver a Notification Filter to receive only the notifications you are
                     interested into because the filter reduces the traffic to the client and the
                     Notification Listener does not have to figure out which is "his" Notifications
                     and which not.
                  </div>
               </p></dd><dt><a name="d0e10210"></a><span class="term">Time Notification</span></dt><dd><p><a name="d0e10213"></a>
                  A notification triggered by an added Event Item and without filtering sent to all
                  Notification Listener of this Timer MBean.
               </p></dd></dl></div><p>
         Just to make it perfectly clear: Each Timer MBean manages a list of Event Items which
         forces the Timer MBean to send Notifications at the given time and as many times as
         specified to all the Notification Listener registered to listen for Notification from
         this Timer MBean instance. Please use a Notification Filter to reduce the Timer
         Notification to what you are interested into especially when you are on a remote server.
      </p><div class="itemizedlist"><ul><li><p><a name="d0e10219"></a>
					<a href="ch13s58.html#timer-preparation" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s58.html#timer-preparation" title="Preparation">Preparation</a>
				</p></li><li><p><a name="d0e10225"></a>
					<a href="ch13s58.html#timer-setup" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s58.html#timer-setup" title="Timer - Setup">Setup of the Timer Service</a>
				</p></li><li><p><a name="d0e10231"></a>
					<a href="ch13s58.html#timer-usage" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s58.html#timer-usage" title="Usage of a Timer MBean">Using the Timer Service</a>
				</p></li><li><p><a name="d0e10237"></a>
					<a href="ch13s58.html#scheduler-preparation" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s58.html#scheduler-preparation" title="Scheduler Preparation">Preparation of the Scheduler</a>
				</p></li><li><p><a name="d0e10243"></a>
					<a href="ch13s58.html#scheduler-usage" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s58.html#scheduler-usage" title="Using the Scheduler">Using the Scheduler</a>
				</p></li><li><p><a name="d0e10249"></a>
					<a href="ch13s58.html#timer-conclusion" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s58.html#timer-conclusion" title="Conclusion">Conclusion</a>
				</p></li></ul></div></div><div class="section"><a name="timer-preparation"></a><div class="titlepage"><div><h3 class="title"><a name="timer-preparation"></a>Preparation</h3></div></div><p>
         To use the Timer Service we have to prepare the environment. Whenever you are using the JBoss
         application server you can skip this section and jump to <a href="ch13s58.html#timer-setup" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s58.html#timer-setup" title="Timer - Setup">
         Timer Setup</a>. If not then you have to do one of the following steps:
      </p><div class="itemizedlist"><ul><li><p><a name="d0e10265"></a>Create a MBeanServer</p></li><li><p><a name="d0e10268"></a>Find and select the appropriate MBeanServer</p></li><li><p><a name="d0e10271"></a>Connect to a remote MBeanServer</p></li></ul></div><div class="section"><a name="d0e10274"></a><div class="titlepage"><div><h4 class="title"><a name="d0e10274"></a>Create a MBeanServer</h4></div></div><p>
            Whenever your application does not create a MBeanServer beforehand you have to
            create one now. Note that you can have more than one MBeanServer running in the
            same JVM at the same time but normally you only need one MBeanServer instance.
            Therefore when you are sure that you don't have one now or you need a new one
            create a MBeanServer this way:
            <pre class="programlisting">
   MBeanServer lServer = MBeanServerFactory.createMBeanServer();
            </pre>              
         </p></div><div class="section"><a name="d0e10282"></a><div class="titlepage"><div><h4 class="title"><a name="d0e10282"></a>Find and select a MBeanServer</h4></div></div><p>
            Whenever there is already a MBeanServer running to find and select a
            MBeanServer by:
            <pre class="programlisting">
   // Get a list of all locally registered MBeanServers
   List lServers = MBeanServerFactory.findMBeanServer( null );
   // Select one. Here it is just the first one
   MBeanServer lServer = (MBeanServer) lServers.get( 0 );
            </pre>              
         </p></div><div class="section"><a name="d0e10290"></a><div class="titlepage"><div><h4 class="title"><a name="d0e10290"></a>Connect to a remote MBeanServer</h4></div></div><p>
            You can also use the Timer on a remote site but then you have to connect
            to the remote MBeanServer by using a Connector. Using the RMI-Connector 
            would look like this:
            <pre class="programlisting">
   // Connect to a remote MBeanServer by using the RMI-Connector (RMI-Adaptor must be looked up beforehand)
   RemoteMBeanServer lServer = new RMIConnectorImpl( lRMIAdaptor );
            </pre>              
            Using the EJB-Connector would look like this:
            <pre class="programlisting">
   // Connect to a remote MBeanServer by using the EJB-Connector (EJB-Adaptor must be looked up beforehand)
   RemoteMBeanServer lServer = new EJBConnector( lEJBAdaptor );
            </pre>              
         </p></div></div><div class="section"><a name="timer-setup"></a><div class="titlepage"><div><h3 class="title"><a name="timer-setup"></a>Timer - Setup</h3></div></div><p>
         To use the Timer service a Timer MBean must be created, registered and activated. There is no need
         to create a Timer MBean each time you want to use the Timer Service but in order to receive the
         Timer Notifications you have to register a Notification Listner on this Timer MBean which in
         turn sends you all the Timer Notifications triggered by any Event Item. But you can supply a
         Notification Filter to receive only Time Notifications you want to receive.
         <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10306"></a>Note</h3>
            Each client listening for notifications from a Timer MBean gets all notifications from all
            added time notifications. Therefore the client has to figure out which notification is
            for which Event Item.
         </div>
         <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10309"></a>Note</h3>
            You only have to create a Timer MBean when there is no Timer MBean registered on the
            actual MBeanServer.
         </div>
         To create a Timer MBean you have 3 choices. The first two only work when used in conjunction
         with a JBoss application server 3. The last one works always.
      </p><div class="itemizedlist"><ul><li><p><a name="d0e10313"></a>Add it to jboss-service.xml</p></li><li><p><a name="d0e10316"></a>Use a Timer service.xml file</p></li><li><p><a name="d0e10319"></a>Create the Timer through the MBeanServer</p></li></ul></div><div class="section"><a name="d0e10322"></a><div class="titlepage"><div><h4 class="title"><a name="d0e10322"></a>Add it to jboss-service.xml</h4></div></div><p>
            The Timer MBean should always be started when JBoss is started then add it to the
            jboss-service.mxl file in the current configuration directory (under /conf). Please add
            this line but keep in mind that you can change the value of attribute "name" whenever
            you like but it must be unique on the current MBeanServer:
            <pre class="programlisting">
   &lt;mbean code="javax.management.timer.Timer" name="DefaultDomain:service=timer"/&gt;
            </pre>
            <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10330"></a>Note</h3>
               There is no need to add any classes to the classpath or classloader because
               the class is part of the JMX-Reference Implementation.
            </div>
         </p></div><div class="section"><a name="d0e10333"></a><div class="titlepage"><div><h4 class="title"><a name="d0e10333"></a>Use a Timer service.xml file</h4></div></div><p>
            The other approach with JBoss is to create your own service.xml giving you the chance to
            deploy it at runtime and don't have the burden to change the root jboss-service.xml file.
            <div class="important" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10338"></a>Important</h3>
               Ensure that the name of the service.xml is unique within all your deployment
               directories (by default "/deploy" and "/deploy/lib").
            </div>
            The timer-service.xml would look like this
            <pre class="programlisting">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;server&gt;

  &lt;!-- ==================================================================== --&gt;
  &lt;!-- JMX Timer MBean                                                      --&gt;
  &lt;!-- ==================================================================== --&gt;

   &lt;mbean code="javax.management.timer.Timer" name="DefaultDomain:service=timer"/&gt;

&lt;/server&gt;
            </pre>
            <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10344"></a>Note</h3>
               Keep in mind that there will be no notification about the startup. To check use the JMX
               HTML-Adaptor on port 8082 by default. The timer is loaded from the jmxri.jar file in /lib
               directory.
            </div>
         </p></div><div class="section"><a name="d0e10347"></a><div class="titlepage"><div><h4 class="title"><a name="d0e10347"></a>Create the Timer through the MBeanServer</h4></div></div><p>
            When you don't have the JBoss application server running then you have to create, register
            and start the Timer MBean by your own using the MBeanServer or RemoteMBeanSErver interface.
            To create and register the Timer MBean:
            <pre class="programlisting">
   ObjectInstance lTimer = lServer.createMBean(
      "javax.management.timer.Timer",
      new ObjectName( "DefaultDomain", "service", "timer" )
   ); 
            </pre>
            To start the Timer MBean which is always necessary here (JBoss is doing this for you):
            <pre class="programlisting">
   lServer.invoke(
      lTimer.getObjectName(), 
      "start", 
      new Object[] {},
      new String[] {}
   ); 
            </pre>
            Of course you can also use an Adaptor like the HTML-Adaptor to create and setup the
            Timer MBean.
         </p></div></div><div class="section"><a name="timer-usage"></a><div class="titlepage"><div><h3 class="title"><a name="timer-usage"></a>Usage of a Timer MBean</h3></div></div><p>
         <div class="procedure"><p><a name="d0e10363"></a><b>Procedure 13.2. Procedure to use the Timer Service</b></p><p>
               Now we are ready to configure the Timer Service and to finally use it. Both steps
               could be done without an order but for the Notification Filter we need the ID number of

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -