Tuesday, November 4, 2014

Quartz Scheduler Application in Java

Quartz Scheduler Simple Application in Java

Most of the time Quartz is helps to Java developers to build the schedule based applications. In the Quartz, developers able to schedule the job for predefined date and time. Developers able to define multiple schedule jobs in a single application.

In this tutorial you able to learn for how to schedule the simple job with using Quartz. 

Objective : In this tutorial we are going to develop a scheduler application with using Quartz and print the Welcome message in console at predefined time period.

Scope : Develop a Scheduler application with using Quartz.

Requirements : In order to achieve the above objective we need to set it up the below items.
  • Eclipse Java EE IDE
  • Maven

Step 1 : Create a Maven project as "SpringQuartzSample" with using Eclipse. 

While you creating the project provide the input parameters are below.
 - New > Other > Maven > Maven Project
 - Click Next button with out modify any default settings.
 - The default archetype is "maven-archetype-quickstart". No need to change any of the default setting click Next.
 - Enter "TestQuartz" as GroupId, "SpringQuartzSample" as artifactId and "com.findanidea.quartz.service" as package .

Step 2 :  Update the pom.xml as mentioned below.
In this pom.xml file we specified the spring also one of the dependency in addition of quartz.

/SpringQuartzSample/pom.xml


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<project
    xmlns = "http://maven.apache.org/POM/4.0.0"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>TestQuartz</groupId>
    <artifactId>SpringQuartzSample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>SpringQuartzSample</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>3.1.1.RELEASE</spring.version>
        <log4j.version>1.2.16</log4j.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.2</version>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>opensymphony</groupId>
            <artifactId>quartz</artifactId>
            <version>1.6.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>
</project>


Step 3 : Create "TestService" class under the package defined in the step 1.

The following service method will invoke the time we specified as cron expression. 

/SpringQuartzSample/src/main/java/com/findanidea/quartz/service/TestService.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package com.findanidea.quartz.service;

import java.util.Date;
import org.springframework.stereotype.Service;

@Service
public class TestService {
 public int counter = 0;

 public void testServiceMethod() {
  Date date = new Date();
  System.out.println(counter + "  - The testServiceMethod() invoked: "
      + date.toString() + "."
      + " Welcome to findanidea.blogspot.com");
  counter++;
 }
}


Step 4 : Now the time to play with the spring configurations. In the below spring configuration file we defined the following items.

1. TestService class defined in the line number 11 as testService Bean.
2. In the line number 16 configured the testServiceMethod as targetMethod in jobDetail bean.
3. The cron expression configured in the line number 22 as cronExpression property in the cronTrigger bean.
4. Finally the cron trigger added to the schedule factory bean at line number 28.

/SpringQuartzSample/src/main/resources/spring-config.xml


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">


 <!-- Our test service bean -->
 <bean id="testService" class="com.findanidea.quartz.service.TestService" />

 <bean id="jobDetail"
  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="testService" />
  <property name="targetMethod" value="testServiceMethod" />
  <property name="concurrent" value="false" />
 </bean>

 <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="jobDetail" />
  <property name="cronExpression" value="0/2 * * * * ?" />
 </bean>

 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
   <list>
    <ref bean="cronTrigger" />
   </list>
  </property>
  <property name="quartzProperties">
   <props>
    <prop key="org.quartz.scheduler.skipUpdateCheck">true</prop>
   </props>
  </property>
 </bean>

</beans>


You able to define multiple schedule jobs with different cron expression in a single "SchedulerFactoryBean" bean.

That's all...  Now move to next step for test the quartz service.

Step 5 : Create a "RunTest" class under the "com.findanidea.quartz.service" package. In the following class we specified that, the thread sleep time at 30 sec.

/SpringQuartzSample/src/main/java/com/findanidea/quartz/service/RunTest.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.findanidea.quartz.service;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RunTest {
 public static void main(String[] args) {

  ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
  System.out.println("*************************************************************************");
  System.out.println("***************** The Quarts Service going to Start. ********************");
  System.out.println("*************************************************************************");
  try {
   Thread.sleep(30000);
  } catch (InterruptedException ex) {
   System.out.println(ex.getMessage());
  }
  System.out.println("*************************************************************************");
  System.out.println("***************** The Quarts Service Stoped. ********************");
  System.out.println("*************************************************************************");
  applicationContext.stop();

 }
}

Once execute the RunTest class, then you able to see the below output in your console...


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
INFO: Scheduler org.springframework.scheduling.quartz.SchedulerFactoryBean#0_$_NON_CLUSTERED started.
*************************************************************************
***************** The Quarts Service going to Start. ********************
*************************************************************************
0  - The testServiceMethod() invoked: Tue Nov 04 12:53:54 IST 2014. Welcome to findanidea.blogspot.com
1  - The testServiceMethod() invoked: Tue Nov 04 12:53:56 IST 2014. Welcome to findanidea.blogspot.com
2  - The testServiceMethod() invoked: Tue Nov 04 12:53:58 IST 2014. Welcome to findanidea.blogspot.com
3  - The testServiceMethod() invoked: Tue Nov 04 12:54:00 IST 2014. Welcome to findanidea.blogspot.com
4  - The testServiceMethod() invoked: Tue Nov 04 12:54:02 IST 2014. Welcome to findanidea.blogspot.com
5  - The testServiceMethod() invoked: Tue Nov 04 12:54:04 IST 2014. Welcome to findanidea.blogspot.com
6  - The testServiceMethod() invoked: Tue Nov 04 12:54:06 IST 2014. Welcome to findanidea.blogspot.com
7  - The testServiceMethod() invoked: Tue Nov 04 12:54:08 IST 2014. Welcome to findanidea.blogspot.com
8  - The testServiceMethod() invoked: Tue Nov 04 12:54:10 IST 2014. Welcome to findanidea.blogspot.com
9  - The testServiceMethod() invoked: Tue Nov 04 12:54:12 IST 2014. Welcome to findanidea.blogspot.com
10  - The testServiceMethod() invoked: Tue Nov 04 12:54:14 IST 2014. Welcome to findanidea.blogspot.com
11  - The testServiceMethod() invoked: Tue Nov 04 12:54:16 IST 2014. Welcome to findanidea.blogspot.com
12  - The testServiceMethod() invoked: Tue Nov 04 12:54:18 IST 2014. Welcome to findanidea.blogspot.com
13  - The testServiceMethod() invoked: Tue Nov 04 12:54:20 IST 2014. Welcome to findanidea.blogspot.com
14  - The testServiceMethod() invoked: Tue Nov 04 12:54:22 IST 2014. Welcome to findanidea.blogspot.com
*************************************************************************
***************** The Quarts Service Stoped. ********************
*************************************************************************







In the above output able to see that the welcome message printed every 2 sec.

Additional details : 
  • You able to find the Quartz API here.
  • Using the "CronMaker" you able to create an cron expression based on your requirements.


No comments:

Post a Comment