More Books
JBoss 4.0 The Official Guide
JBoss® 4.0 The Official Guide
Table of Contents
Copyright
About the Authors
We Want to Hear from You!
Introduction
What This Book Covers
About JBoss
About Open Source
About Professional Open Source
What's New in JBoss 4.0
Chapter 1.  Installing and Building the JBoss Server
Getting the Binary Files
Installing the Binary Package
Basic Installation Testing
Booting from a Network Server
Building the Server from Source Code
Chapter 2.  The JBoss JMX Microkernel
JMX
The JBoss JMX Implementation Architecture
Connecting to the JMX Server
Using JMX as a Microkernel
The JBoss Deployer Architecture
Exposing MBean Events via SNMP
Remote Access to Services, Detached Invokers
Chapter 3.  Naming on JBoss
An Overview of JNDI
The JBossNS Architecture
Chapter 4.  Transactions on JBoss
Transaction and JTA Overview
JBoss Transaction Internals
Chapter 5.  EJBs on JBoss
The EJB Client-Side View
The EJB Server-Side View
The EJB Container
Entity Bean Locking and Deadlock Detection
Chapter 6.  Messaging on JBoss
JMS Examples
JBossMQ Overview
JBossMQ Configuration and MBeans
Specifying the MDB JMS Provider
Chapter 7.  Connectors on JBoss
JCA Overview
An Overview of the JBossCX Architecture
Configuring JDBC Datasources
Configuring Generic JCA Adaptors
Chapter 8.  Security on JBoss
J2EE Declarative Security Overview
An Introduction to JAAS
The JBoss Security Model
The JBossSX Architecture
The Secure Remote Password (SRP) Protocol
Running JBoss with a Java 2 Security Manager
Using SSL with JBoss and JSSE
Configuring JBoss for Use Behind a Firewall
Securing the JBoss Server
Chapter 9.  Web Applications
The Tomcat Service
The Tomcat server.xml File
The Engine Element
The Host Element
Using SSL with the JBoss/Tomcat Bundle
Setting the Context Root of a Web Application
Setting Up Virtual Hosts
Serving Static Content
Using Apache with Tomcat
Using Clustering
Integrating Third-Party Servlet Containers
Chapter 10.  MBean Services Miscellany
System Properties Management
Property Editor Management
Services Binding Management
Scheduling Tasks
The Log4j Service MBean
RMI Dynamic Class Loading
Chapter 11.  The CMP Engine
Example Code
The jbosscmp-jdbc Structure
Entity Beans
CMP Fields
Container-Managed Relationships
Declaring Queries
Optimized Loading
The Loading Process
Transactions
Optimistic Locking
Entity Commands and Primary Key Generation
JBoss Global Defaults
Datasource Customization
Chapter 12.  Web Services
JAX-RPC Service Endpoints
Enterprise JavaBean Endpoints
Web Services ClientsA JAX-RPC Client
Service References
Chapter 13.  Hibernate
The Hibernate MBean
Hibernate Archives
Using Hibernate Objects
Using a HAR File Inside an EAR File
The HAR Deployer
Chapter 14.  Aspect-Oriented Programming (AOP) Support
JBoss AOP: EJB-Style Services for Plain Java Objects
Why AOP?
Basic Concepts of AOP
Building JBoss AOP Applications
The JBoss AOP Deployer
Packaging and Deploying AOP Applications to JBoss
Appendix A.  The GNU Lesser General Public License (LGPL)
GNU General Public License
Appendix B.  Example Installation
Index
SYMBOL
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X

Scheduling Tasks

Java includes a simple timer-based capability through the java.util.Timer and java.util.TimerTask utility classes. JMX also includes a mechanism for scheduling JMX notifications at a given time with an optional repeat interval as the javax.management.timer.TimerMBean agent service.

JBoss includes two variations of the JMX timer service in the org.jboss.varia.scheduler.Scheduler and org.jboss.varia. scheduler.ScheduleManager MBeans. Both of these MBeans rely on the JMX timer service for the basic scheduling. They extend the behavior of the timer service as described in the following sections.

The org.jboss.varia.scheduler.Scheduler MBean

The Scheduler MBean differs from TimerMBean in that Scheduler directly invokes a callback on an instance of a user-defined class or an operation of a user-specified MBean.

The following are the configurable attributes of Scheduler MBean:

  • InitialStartDate The date when the initial call is scheduled. It can be one of the following:

    • NOW, in which case the date will be the current time plus 1 second.

    • A number representing the milliseconds since 1/1/1970.

    • The date as a string that is able to be parsed by SimpleDateFormat, with the default format pattern "M/d/yy h:mm a". If the date is in the past, the Scheduler MBean will search a start date in the future with respect to the initial repetitions and the period between calls. This means that when you restart the MBean (that is, restart JBoss and so on), it will start at the next scheduled time. When no start date is available in the future, Scheduler will not start.

    For example, if you start your Schedulable class every day at noon and you restart your JBoss server, it will start at the next noon.

  • InitialRepetitions The number of times the scheduler will invoke the target's callback. If -1, then the callback will be repeated until the server is stopped.

  • StartAtStartup A flag that determines whether Scheduler will start when it receives its startService life cycle notification. If TRue, the Scheduler MBean starts on its startup. If false, an explicit startSchedule operation must be invoked on the Scheduler to begin.

  • SchedulePeriod The interval between scheduled calls, in milliseconds. This value must be bigger than 0.

  • SchedulableClass The fully qualified classname of the org.jboss.varia. scheduler.Schedulable interface implementation that is to be used by the Scheduler Mbean. SchedulableArguments and SchedulableArgumentTypes must be populated to correspond to the constructor of the Schedulable implementation.

  • SchedulableArguments A comma-separated list of arguments for the Schedulable implementation class constructor. Only primitive data types, String, and classes with a constructor that accepts a String as its sole argument are supported.

  • SchedulableArgumentTypes A comma-separated list of argument types for the Schedulable implementation class constructor. This will be used to find the correct constructor via reflection. Only primitive data types, String, and classes with a constructor that accepts a String as its sole argument are supported.

  • SchedulableMBean The fully qualified JMX ObjectName name of the schedulable MBean to be called. If the MBean is not available, it will not be called, but the remaining repetitions will be decremented. When you're using SchedulableMBean, SchedulableMBeanMethod must also be specified.

  • SchedulableMBeanMethod The operation name to be called on the schedulable MBean. It can optionally be followed by an opening bracket, a comma-separated list of parameter keywords, and a closing bracket. The supported parameter keywords include the following:

    • NOTIFICATION, which will be replaced by the timer's notification instance (javax.management.Notification)

    • DATE, which will be replaced by the date of the notification call (java.util.Date)

    • REPETITIONS, which will be replaced by the number of remaining repetitions (long)

    • SCHEDULER_NAME, which will be replaced by the ObjectName of the Scheduler

    • Any fully qualified classname, which the Scheduler will set to null. A given Scheduler instance supports only a single schedulable instance. If you need to configure multiple scheduled events, you use multiple Scheduler instances, each with a unique ObjectName. The following is an example of configuring a Scheduler MBean to call a Schedulable implementation as well as a configuration for calling an MBean:

<server>
    <mbean code="org.jboss.varia.scheduler.Scheduler"
           name="jboss.docs.chap10:service=Scheduler">
        <attribute name="StartAtStartup">true</attribute>
        <attribute name="SchedulableClass">org.jboss.chap10.ex2.ExSchedulable</attribute>
        <attribute name="SchedulableArguments">TheName,123456789</attribute>
        <attribute name="SchedulableArgumentTypes">java.lang.String,long</attribute>

       <attribute name="InitialStartDate">NOW</attribute>
       <attribute name="SchedulePeriod">60000</attribute>
       <attribute name="InitialRepetitions">-1</attribute>
   </mbean>
</server>

The SchedulableClass org.jboss.chap10.ex2.ExSchedulable example class is shown here:

package org.jboss.chap10.ex2;

import java.util.Date;
import org.jboss.varia.scheduler.Schedulable;

import org.apache.log4j.Logger;

/**
 * A simple Schedulable example.
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.5 $
 */
public class ExSchedulable implements Schedulable
{
    private static final Logger log = Logger.getLogger(ExSchedulable.class);

    private String name;
    private long value;

    public ExSchedulable(String name, long value)
    {
        this.name = name;
        this.value = value;
        log.info("ctor, name: " + name + ", value: " + value);
    }

    public void perform(Date now, long remainingRepetitions)
    {
        log.info("perform, now: " + now +
                 ", remainingRepetitions: " + remainingRepetitions +
                 ", name: " + name + ", value: " + value);
    }
}

You deploy the timer SAR by running this command:

[examples]$ ant -Dchap=chap10 -Dex=2 run-example

The server console shows the following, which includes the first two timer invocations, separated by 60 seconds:


21:09:27,716 INFO [ExSchedulable] ctor, name: TheName, value: 123456789
21:09:28,925 INFO [ExSchedulable] perform, now: Mon Dec 20 21:09:28 CST 2004,
 remainingRepetitions: -1,
21:10:28,899 INFO [ExSchedulable] perform, now: Mon Dec 20 21:10:28 CST 2004,
 remainingRepetitions: -1,
21:11:28,897 INFO [ExSchedulable] perform, now: Mon Dec 20 21:11:28 CST 2004,
 remainingRepetitions: -1,