Quartz是纯JAVA的调度框架,应用广泛,支持JAVA应用程序和WEB程序,最近在项目中小试了一下,还不错,下面刨除业务逻辑,用简单的实例展示Quartz在WEB程序中的应用。
准备工作:java IDE(eclipse),quartz-1.6.4.jar,commons-collections-3.1.jar,commons-logging-1.0.4.jar。
新建WEB工程
首先,在web.xml文件中对quartz进行注册,这里采用监听器的注册方式,其他方式可参考官方文档。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.quartz.ee.servlet.QuartzInitializerListener
</listener-class>
</listener>
</web-app>
编写一个job类,要实现Job接口。
package com.ares.quartz;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* @Title: HelloWorld.java
* @Package com.ares.quartz
* @Description: TODO
* @author [email protected]
* @date 2009-12-21 下午09:24:41
* @version V1.0
*/
public class HelloWorld implements Job {
/**
* @Title: execute
* @Description: execute
* @param @param context
* @return void
* @throws
*/
public void execute(JobExecutionContext context)
throws JobExecutionException {
JobDetail jobDetail = context.getJobDetail();
try {
//从jobDataMap获得传入的变量值
System.out.println("Hello World "
+ jobDetail.getJobDataMap().get("NAME") + " " + new Date());
} catch (Exception e) {
e.printStackTrace();
}
}
}
编写一个简单的servlet,用于WEB前端调用,web.xml注册servlet,略。
package com.ares.quartz;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.quartz.impl.StdSchedulerFactory;
/**
* @Title: TestQuartz.java
* @Package com.ares.quartz
* @Description: TODO
* @author [email protected]
* @date 2009-12-21 下午09:54:22
* @version V1.0
*/
public class TestQuartz extends HttpServlet {
/**
* @Fields serialVersionUID : TODO
*/
private static final long serialVersionUID = 1L;
/**
* Constructor of the object.
*/
public TestQuartz() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Retrieve the factory from the ServletContext
StdSchedulerFactory factory = (StdSchedulerFactory) request
.getSession().getServletContext().getAttribute(
QuartzInitializerListener.QUARTZ_FACTORY_KEY);
// Retrieve the scheduler from the factory
Scheduler scheduler;
try {
scheduler = factory.getScheduler();
// Start the scheduler
scheduler.start();
// Create a JobDetail for the Job
JobDetail jobDetail = new JobDetail("job", Scheduler.DEFAULT_GROUP,
HelloWorld.class);
// Configure the directory to scan
JobDataMap jobDataMap = jobDetail.getJobDataMap();
// 传入参数,quartz数据交互的一种方式。
jobDataMap.put("NAME", request.getParameter("name"));
Trigger trigger = new SimpleTrigger("job", null, TriggerUtils
.getEvenMinuteDate(new Date()), null, 0, 0L);
// Associate the trigger with the job in the scheduler
scheduler.scheduleJob(jobDetail, trigger);
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.sendRedirect("/testQuartz/index.jsp");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
编写WEB前端页面,index.jsp
<%@ page language="java" pageEncoding="utf8"%>
<html>
<head>
<title>TEST QUARTZ</title>
</head>
<body>
<form action="servlet/TestQuartz">
<input type="text" name="name" />
<input type="submit"
value="sayHelloWorld" />
</form>
</body>
</html>
将工程部署到WEB容器中,如tomcat等。
在文本框随便输入点字符,点击“sayHelloWorld”按钮。
容器的控制台将在下一分钟输出形如下面的内容
2009-12-21 22:17:47 org.quartz.core.QuartzScheduler start
信息: Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
Hello World Ares Mon Dec 21 22:18:00 CST 2009
附件是实例代码,仅供参考。