浅谈Quartz(WEB)

本文介绍如何使用Quartz调度框架在Web程序中实现定时任务。通过具体示例展示了如何配置Quartz并创建定时任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 better.Jiang@hand-china.com
* @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 better.Jiang@hand-china.com
* @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等。
[img]http://dl.iteye.com/upload/attachment/183515/e220afd8-ebde-3ece-a206-e4109cc34eaa.jpg[/img]
在文本框随便输入点字符,点击“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

附件是实例代码,仅供参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值