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

📄 ch13s58.html

📁 详细介绍了jboss3.0的配置等
💻 HTML
📖 第 1 页 / 共 3 页
字号:
               the Event Item. Therefore the Event Item should be added first. If you don't use
               the Notification Filter or filter on the client side then you can perform the second
               step as the first step, too.
            </p><ol type="1"><li><a name="d0e10368"></a><p>
                  First add the Event Item to the Timer Service. Keep the number of the Event Item
                  because it is necessary for the Notification Filter. Because there are different
                  ways to register an Event Item let us go through them one by one.
               </p><div class="itemizedlist"><ul><li><p><a name="d0e10372"></a>
                        This code just adds an Event Item which sends a Time Notification once
                        after one minute from now.
                     </p><pre class="programlisting">
   Date lNext = new Date( new Date().getTime() + Timer.ONE_MINUTE );
   Integer lOneMinuteTimer = (Integer) lServer.invoke(
      lTimer.getObjectName(),
      "addNotification",
      new Object[] { 
         "IDoNotKnowWhatTypeIs",
         "I call you with this timer once",
         // No user object
         null,
         // In one minute from now
         lNext,
      },
      new String[] {
         "".getClass().getName(),
         "".getClass().getName(),
         "java.lang.Object",
         Date.class.getName()
      }
   );
                     </pre></li><li><p><a name="d0e10377"></a>
                        This code sets up an Event Item which sends ten Time Notifications one minute
                        apart starting after one hour from now.
                     </p><pre class="programlisting">
   Date lNext = new Date( new Date().getTime() + Timer.ONE_HOUR );
   Integer lOneHourTimer = (Integer) lServer.invoke(
      lTimer.getObjectName(),
      "addNotification",
      new Object[] { 
         "IDoNotKnowWhatTypeIs", 
         "I call you with this timer ten times",
         // No user object
         null,
         // In one hour from now
         lNext,
         new Long( Timer.ONE_MINUTE ),
         new Long( 10 )
      },
      new String[] {
         "".getClass().getName(),
         "".getClass().getName(),
         "java.lang.Object",
         Date.class.getName(),
         Long.TYPE.getName(),
         Long.TYPE.getName()
      } 
   );
                     </pre></li><li><p><a name="d0e10382"></a>
                        This will remove an Event Item by its ID
                     </p><pre class="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() 
      }
   );
                     </pre></li></ul></div></li><li><a name="d0e10387"></a><p>
                  Now we have to register a Notification Listener at the JMX Agent to retrieve
                  the Time Notifications. The Notification Filter should be used whenever you
                  have more than one Event Item added to the Timer Service. The filter should only
                  pass Time Notification the client is interested into (it is assumed here that
                  the Id of the Event Item (above it would be "lOneMinuteTimer" or "lOneHourTimer")
                  is stored in the class variable "mTimerId").
               </p><pre class="programlisting"> 
   lServer.addNotificationListener(
      lTimer.getObjectName(),
      // Notification Listener
      new NotificationListener() {
         public void handleNotification(
            Notification pNotification,
            Object pHandback 
         ) {
            // Add here you call any code or application or perform whatever you want to
            System.out.println( "You got a Notification: " + pNotification );
         }
      },
      // Filter Listener
      new NotificationFilter() {
         public boolean isNotificationEnabled( Notification pNotification ) {
            if( pNotification instanceof TimerNotification ) {
               TimerNotification lTimerNotification = (TimerNotification) pNotification;
               return lTimerNotification.getNotificationID().equals( mTimerId );
            }
            return false;
         }
      },
      // No object handback necessary
      null
   );
               </pre><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10392"></a>Note</h3>
                  Here both listener as well as filter are implemented as anonymous classes but
                  this is not necessary. Have a look at the Scheduler to see how regular classes
                  can be used.
               </div></li></ol></div>
      </p></div><div class="section"><a name="scheduler-preparation"></a><div class="titlepage"><div><h3 class="title"><a name="scheduler-preparation"></a>Scheduler Preparation</h3></div></div><p>
         The JBoss Scheduler service enables you to use the Timer Service easier and to call any
         instance implementing the Schedulable interface.
         <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10400"></a>Note</h3>
            One Scheduler Instance is to schedule one task. Therefore to schedule more than one
            task create more than one Scheduler MBeans.
         </div>
         <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e10403"></a>Note</h3>
            Every Scheduler Instance can be reused by reseting the MBean attributes but your old
            schedule is then lost.
         </div>
      </p><p>
         To use the Scheduler you need a target which the Scheduler will call when the Timer sends
         a notification based on your settings. This can be either an instance of a class implementing
         the Schedulable interface (which will be created by the Scheduler) or another MBean available
         in the JMX MBeanServer of JBoss. From now on we will call this a Schedulable (class / instance
         or MBean).
      </p><div class="orderedlist"><ol type="1"><li><p><a name="d0e10409"></a>
               One way to use the Scheduler service is to provide a class that implements the
               Schedulable interface. When the Scheduler receives a Notification from the Timer
               it will call the Schedulable's <tt>perform()</tt> method. This method
               takes 2 paramaters: the first is the time that the notification was sent; the second
               is the number of the remain occurences(where -1 means no limit).
            </p><p>
               The example coming with the Scheduler what a Schedulable class should look like is
               the internal class SchedulableExample. It just takes a name and a value used to identify
               the log when a timed call comes in. To create a Schedulable class just build a Constructor
               taking basic data types, String or any other class having a constructor taking a single String
               as parameter. Then implement the <tt>perform</tt> method where you can use the
               given date of call, number of remaining repetitions and the value set by the constructor.
               The <tt>perform</tt> method can start backups, nightly data transfers, purge old
               data from the DB etc. The default example looks like this:
               <pre class="programlisting">
   public static class SchedulableExample
      implements Schedulable
   {
      private String mName;
      private int mValue;

      public SchedulableExample(
         String pName,
         int pValue
      ) {
         mName = pName;
         mValue = pValue;
      }

      /**
       * Just log the call
       **/
      public void perform(
         Date pTimeOfCall,
         long pRemainingRepetitions
      ) {
         System.out.println( "Schedulable Examples is called at: " + pTimeOfCall +
            ", remaining repetitions: " + pRemainingRepetitions +
            ", test, name: " + mName + ", value: " + mValue );
      }
   }
               </pre>
            </p><p>
               The last step of the preparation is that all the new classes coming along with
               your Schedulable must be made available to the JMX agent. Therefore you have to add
               it to the classpath or with JBoss 3 reference the JAR file in the service.xml file.
            </p></li><li><p><a name="d0e10428"></a>
               The other way is to tell the Scheduler which method on which MBean schould be

⌨️ 快捷键说明

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