Job Scheduling is one of the most important in while working in admin profile or for the developers. There were various options available for job scheduling, but as this has become a most findamental requirement for most of the applications or admins, So now as part of EE6 we can crate our own Automatic Schedulers/timers using the annotation “javax.ejb.Schedule”.

Enterprise bean timers are either programmatic timers or automatic timers. In this example we will see how we can create an “Automatic Timer”, Timers can be set according to a calendar-based schedule and these are very easy to maintain. Schedule a timer for automatic creation with a timeout schedule based on a cron-like time expression. The annotated method is used as the timeout callback method. All elements of this annotation are optional. If none are specified a persistent timer will be created with callbacks occuring every day at midnight in the default time zone associated with the container in which the application is executing. For more information on this you can refer to the following link:

Step1). Create a Directory somewhere inside your file system where we can create our web application. Suppose i am creating a directory as “/home/userone/EJB31_Automatic_Scheduler” and the create a subdirectory with name “src” inside “/home/userone/EJB31_Automatic_Scheduler”

Step2). Now we will write the Scheduler Bean “AutomaticSchedulerBean.java” program inside the “/home/userone/EJB31_Automatic_Scheduler/src” directory which will have a method “backgroundProcessing()” and it will run on scheduled time as mentioned below:

dayOfWeek=* means all the seven days of a week
hour = “*” means every hour of a day
minute = “*” means every minute
second=”*/5″ means every five second interval
year=”2012″ means in the year 2012

package test;
import javax.ejb.Stateless;
import java.util.Date;
import javax.ejb.Schedule;

public class AutomaticSchedulerBean
     @Schedule(dayOfWeek = "*", hour = "*", minute = "*", second = "*/5",year="2012", persistent = false)
     public void backgroundProcessing()
		   System.out.println("nnt AutomaticSchedulerBean's backgroundProcessing() called....at: "+new Date());

Step3). Now we will write a simple ant “build.xml” file in order to build the scheduler JAR and deploy it on JBoss AS7. So write the following “build.xml” file inside “/home/userone/EJB31_Automatic_Scheduler” as following:

<project name="Secured EJB Remote" default="all">
<property name="jboss.home" value="/home/userone/jboss-as-7.1.0.CR1b" />
<property name="jboss.module.dir" value="${jboss.home}/modules" />
<property name="basedir" value="." />
<property name="tmp.dir" value="tmp" />
<property name="src.dir" value="src" />
<property name="output.dir" value="build" />
<property name="ejb.jar" value="schedulerEJB.jar" />

        <path id="jboss.classpath">
           <fileset dir="${jboss.module.dir}">
               <include name="**/*.jar"/>

        <target name="all" depends="deploy" />

        <target name="build_ejb_jar">
           <delete dir="${output.dir}" />
           <delete dir="${tmp.dir}" />
           <mkdir dir="${output.dir}" />
           <mkdir dir="${tmp.dir}" />
           <javac srcdir="${src.dir}" destdir="${tmp.dir}"  includes="*.java" classpathref="jboss.classpath"/> 
           <jar jarfile="${tmp.dir}/${ejb.jar}" basedir="${tmp.dir}" compress="true" />
           <copy file="${tmp.dir}/${ejb.jar}" tofile="${output.dir}/${ejb.jar}"/>

           <delete dir="${tmp.dir}"/>

        <target name="deploy" depends="build_ejb_jar">
            <echo message="*******************  Deploying the EAR file ${ear.name} *********************" />  
            <echo message="********** ${output.dir}/${ejb.jar} to ${jboss.home}/standalone/deployments **********" />  
            <copy todir="${jboss.home}/standalone/deployments/">
                <fileset dir="${output.dir}/">
                  <include name="${ejb.jar}"/> 
            <echo message="*******************  Deployed Successfully   *********************" />  
            <echo message="******************* Check JBoss AS7 Console  *********************" /> 

NOTE: The only change in the above file you need to do is to change the “jboss.home” directory path in the second line of the above script is to point to your own JBoss AS7 directory home directory.

Step4). Now before running your ANT script to build and deploy the above webapplication you should have the ANT as well as JAVA set in the $PATH variable of the Shell / command prompt as following:

For Unix Based OS:
export PATH=/home/userone/jdk1.6.0_21/bin:/home/userone/apache-ant-1.8.2/bin:$PATH

For Windows Based OS:
set PATH=C:/jdk1.6.0_21/bin;C:/apache-ant-1.8.2/bin;%PATH%

Step5). Now run the ant file from the directory where you have placed the “build.xml” file as following:

[userone@localhost EJB31_Automatic_Scheduler]$ ant
Buildfile: build.xml

   [delete] Deleting directory /home/userone/EJB31_Automatic_Scheduler/build
    [mkdir] Created dir: /home/userone/EJB31_Automatic_Scheduler/build
    [mkdir] Created dir: /home/userone/EJB31_Automatic_Scheduler/tmp
    [javac] Compiling 1 source file to /home/userone/EJB31_Automatic_Scheduler/tmp
      [jar] Building jar: /home/userone/EJB31_Automatic_Scheduler/tmp/schedulerEJB.jar
     [copy] Copying 1 file to /home/userone/EJB31_Automatic_Scheduler/build
   [delete] Deleting directory /home/userone/EJB31_Automatic_Scheduler/tmp

     [echo] *******************  Deploying the EAR file ${ear.name} *********************
     [echo] ********** build/schedulerEJB.jar to /home/userone/jboss-as-7.1.0.CR1b/standalone/deployments **********
     [copy] Copying 1 file to /home/userone/jboss-as-7.1.0.CR1b/standalone/deployments
     [echo] *******************  Deployed Successfully   *********************
     [echo] ******************* Check JBoss AS7 Console  *********************


Total time: 4 seconds

Step6). Now as soon the above Scheduler is deployed on the JBoss AS7 we will see this kind of messages in the JBoss AS7 Console.

15:14:33,655 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-7) Starting deployment of "schedulerEJB.jar"
15:14:34,085 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-5) JNDI bindings for session bean named AutomaticSchedulerBean in deployment unit deployment "schedulerEJB.jar" are as follows:


15:14:34,477 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "schedulerEJB.jar"
15:14:35,014 INFO  [stdout] (pool-9-thread-1) 
15:14:35,015 INFO  [stdout] (pool-9-thread-1) 
15:14:35,015 INFO  [stdout] (pool-9-thread-1) 	 AutomaticSchedulerBean's backgroundProcessing() called....at: Sun Jan 29 15:14:35 IST 2012
15:14:40,015 INFO  [stdout] (pool-9-thread-2) 
15:14:40,015 INFO  [stdout] (pool-9-thread-2) 
15:14:40,015 INFO  [stdout] (pool-9-thread-2) 	 AutomaticSchedulerBean's backgroundProcessing() called....at: Sun Jan 29 15:14:40 IST 2012
15:14:45,003 INFO  [stdout] (pool-9-thread-3) 
15:14:45,003 INFO  [stdout] (pool-9-thread-3) 
15:14:45,003 INFO  [stdout] (pool-9-thread-3) 	 AutomaticSchedulerBean's backgroundProcessing() called....at: Sun Jan 29 15:14:45 IST 2012
15:14:50,003 INFO  [stdout] (pool-9-thread-4) 
15:14:50,003 INFO  [stdout] (pool-9-thread-4) 
15:14:50,003 INFO  [stdout] (pool-9-thread-4) 	 AutomaticSchedulerBean's backgroundProcessing() called....at: Sun Jan 29 15:14:50 IST 2012

These timers are good to perform some background activities like cleaning up some logs …etc.
MiddlewareMagic Team

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.