Friday, June 21, 2013

How to create a Quartz Job ?

What is Quartz Scheduler?

1] Quartz is a full-featured, open source job scheduling service that can be integrated with, or used with any Java application - from the smallest stand-alone application to the largest e-commerce system.

2] Using Quartz we can create simple or complex schedules for executing tens to thousands of jobs; jobs whose tasks are defined as standard Java components that may execute virtually anything you may program them to do.

clip_image002

 

3] Quartz is freely usable, licensed under the Apache 2.0 license.

Where can I download Quartz Enterprise Scheduler?

http://quartz-scheduler.org/

 

clip_image004

 

How can we implement Quartz Scheduler?

1. Download Quartz Scheduler using maven build tool

You can get the Quartz library from Maven central repository.

 
<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>com.tutorial.quartz</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mavenproject1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-oracle</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-weblogic</artifactId>
<version>2.1.7</version>
</dependency>
</dependencies>
</project>



clip_image006

2. Create a Quartz Job:



import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job
{
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("Hello Quartz Scheduler!!!");
}
}


3. Create Trigger and Scheduler:



package com.tutorial.quartz;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class SimpleTrigger {
public static void main(String[] args) throws Exception {
// Create a Job
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("sampleJobName", "group1").build();
// Trigger the job
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("sampleTriggerName", "group1")
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5).repeatForever())
.build();
// Schedule the Job
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
}

Output:


clip_image008


References:


1. http://quartz-scheduler.org/downloads/catalog

No comments:

Post a Comment