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等。
在文本框随便输入点字符,点击“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
附件是实例代码,仅供参考。
- 大小: 40.4 KB
分享到:
相关推荐
网上能找到的Quartz Web管理的资料都是使用的一个国外人写的Quartz WebApp的东东,功能也很全面。但是作为自己的应用其实用不了那么多功能,一般我们只要可以定义一个job,指定一个Cron表达式完成工作即可,附带的...
web工程中使用quartz的配置方法,完整实例
web项目集成quartz,从简单到复杂,1-9分开展示quartz的集成到配置,希望和大家一起学习一下
quartz_web.rar quartz定时任务小例子
quartz框架在web中的应用,代码简单完整,且可以正常运行,对初学者很有帮助。
好像很多人整合出现了问题,所以抽个demo出来 是一个demo包含源码和lib 在tomcat下可以执行
quartz的web整合简单小例子,比较适合做一些方便的工作,定时器可以轻松搞定。
一个完整的简易排程例子,如想应用,只需在execute方法里填加自己的逻辑处理即可
quartz通过cron实现web项目定时器;这是个maven项目,通过quartz架构是现在不同cron表达式定时启动。
NULL 博文链接:https://gghaomm.iteye.com/blog/1753098
NULL 博文链接:https://liangjian103.iteye.com/blog/1141253
基于springmvc+mybatius+quartz实现的web管理的持久化定时任务。使用了JDBC和插件方式实现任务的添加。只需修改jdbc配置并将sql文件在数据库中运行即可运行。
博文链接:https://daoger.iteye.com/blog/195486
Spring Quartz 定时器示例(Web工程版),欢迎下载。
Quartz学习和讲解的word和ppt各一份,web开发者必备。
tomcat启动时立即调用quartz执行一次
quartz定时任务,在web项目中的使用方法;有例子和包直接拿到工程里陪陪就能用
quartz2.1.7jar包
文件里面包括 1:Quartz开发指南.pdf 2:Quartz从入门到进阶.pdf 3:QuartzBeginnerExample一个附带的工程例子 4:quartz-1.6.1.zip Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它...