JSF 2 + Quartz 2 example


In this tutorial, we show you how to run a Quartz job during JSF web application via QuartzInitializerListener listener class in Quartz library. This solution is not only works with JSF 2, the concept is applicable on almost all standard Java web application.

Tools Used :

  1. JSF 2.1.11
  2. Quartz 2.1.5
  3. Maven 3
  4. Eclipse 4.2
  5. Tomcat 7

The previous JSF 2.0 hello world example is reused, and we will enhance it to support Quartz job via QuartzInitializerListener listener class.

P.S This tutorial is only focus on Quartz integration, for JSF, please read above JSF hello world example.

1. Project Folder

Review the final project directory structure.

project directory structure
2. Dependencies

To deploy on Tomcat, you need many JSF dependencies. Read XML comments for detail.

File : pom.xml

<dependencies> ...
                <!-- JSF 2 libraries --> 		<dependency> 			<groupId>com.sun.faces</groupId> 			<artifactId>jsf-api</artifactId> 			<version>2.1.11</version> 		</dependency> 		<dependency> 			<groupId>com.sun.faces</groupId> 			<artifactId>jsf-impl</artifactId> 			<version>2.1.11</version> 		</dependency>  
		<dependency> 			<groupId>javax.servlet</groupId> 			<artifactId>jstl</artifactId> 			<version>1.2</version> 		</dependency>  
		<dependency> 			<groupId>javax.servlet</groupId> 			<artifactId>servlet-api</artifactId> 			<version>2.5</version> 		</dependency>  
		<dependency> 			<groupId>javax.servlet.jsp</groupId> 			<artifactId>jsp-api</artifactId> 			<version>2.1</version> 		</dependency>  
                <!-- Tomcat 6 need this --> 		<dependency> 			<groupId>com.sun.el</groupId> 			<artifactId>el-ri</artifactId> 			<version>1.0</version> 		</dependency>  
		<!-- Quartz scheduler framework --> 		<dependency> 			<groupId>org.quartz-scheduler</groupId> 			<artifactId>quartz</artifactId> 			<version>2.1.5</version> 		</dependency>  
		<!-- Quartz need transaction --> 		<dependency> 			<groupId>javax.transaction</groupId> 			<artifactId>jta</artifactId> 			<version>1.1</version> 		</dependency> ...
3. Quartz Job

Create a Quartz job class. This class is going to schedule and run later.

File : SchedulerJob.java

package com.mkyong.scheduler;  import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException;  public class SchedulerJob implements Job {  
	@Override
	public void execute(JobExecutionContext context) 		throws JobExecutionException {  
		System.out.println("JSF 2 + Quartz 2 example");  
	}  }
4. Quartz Configuration

Create quartz.properties and quartz-config.xml, put it in resources “folder” (Maven structure), for non-Maven project, make sure it can be locate at project classpath.

File : quartz.properties – Configure Quartz instance and read the settings from quartz-config.xml

org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin 
org.quartz.plugin.jobInitializer.fileNames = quartz-config.xml 
org.quartz.plugin.jobInitializer.failOnFileNotFound = true

File : quartz-config.xml – Configure trigger to run com.mkyong.scheduler.SchedulerJob

<?xml version="1.0" encoding="UTF-8"?>  <job-scheduling-data 	xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData" 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 	xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData  	http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd" 	version="1.8">  
	<schedule> 		<job> 			<name>AJob</name> 			<group>AGroup</group> 			<description>Print a welcome message</description> 			<job-class>com.mkyong.scheduler.SchedulerJob</job-class> 		</job>  
		<trigger> 			<cron> 				<name>dummyTriggerName</name> 				<job-name>AJob</job-name> 				<job-group>AGroup</job-group> 				<!-- It will run every 5 seconds --> 				<cron-expression>0/5 * * * * ?</cron-expression> 			</cron> 		</trigger> 	</schedule> </job-scheduling-data>
Note
For detail explanation, please read this Quartz configuration reference article.
5. Integrate Quartz

This is where the integration happened. Declared org.quartz.ee.servlet.QuartzInitializerListener as listener class in web.xml file.

File : web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app ...>  
	<listener> 		<listener-class> 			org.quartz.ee.servlet.QuartzInitializerListener
		</listener-class> 	</listener>  </web-app>
6. Demo

During project start up, Quartz is started and run the scheduled job every 5 seconds.

Jul 26, 2012 3:32:18 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"] Jul 26, 2012 3:32:18 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"] Jul 26, 2012 3:32:18 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3591 ms
JSF 2 + Quartz 2 example
JSF 2 + Quartz 2 example
JSF 2 + Quartz 2 example

转载于:https://my.oschina.net/yida/blog/69617

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值