自己写的一个spring的Quartz定时任务_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 自己写的一个spring的Quartz定时任务

自己写的一个spring的Quartz定时任务

 2011/11/9 8:27:07  shp808  http://shp808.iteye.com  我要评论(0)
  • 摘要:1:在web.xml中加入:<servlet><servlet-name>QuartzInitializer</servlet-name><servlet-class>com.asire.ida.servlet.DefaultServlet</servlet-class><init-param><!--quartz配置文件--><param-name>quartzConfig</param
  • 标签:Quartz 一个 Spring 自己
1:在web.xml中加入:
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<servlet-class>
com.asire.ida.servlet.DefaultServlet
</servlet-class>
<init-param>
            <!--quartz配置文件-->
            <param-name>quartzConfig</param-name>
            <param-value>xxx/quartz/quartz.properties</param-value>
        </init-param>
<load-on-startup>1</load-on-startup>
</servlet>

2.要写一个Servlet 类:
package com.asire.ida.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.lang.StringUtils;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
public class DefaultServlet extends HttpServlet{

/**
*
*/
private static final long serialVersionUID = 1L;


/**
     * quartz调度器.
     */
    protected static Scheduler scheduler = null;

@Override
public void init() throws ServletException {

// 初始化quartz配置.
initQuartz();
}
/**
* 初始化quartz配置(quartz配置文件目录等).
*/
public void initQuartz() {
String quartzConfig = this.getInitParameter("quartzConfig");
if (quartzConfig == null || quartzConfig.length() == 0) {
System.out.println("没有指定quartz配置文件相对路径!");
return;
}
System.out.println("没有指定quartz配置文件相对路径=="+quartzConfig);
// 得到quartz配置文件全路径名
String quartzConfigFileName = "E:/shp_tomcat/apache1/bin/config/quartz/quartz.properties";//ConfigurationHelper.getFullFileName(quartzConfig);
FileInputStream fileInputStream;
try {
System.out.println("得到quartz配置文件全路径名:"+quartzConfigFileName);
fileInputStream = new FileInputStream(quartzConfigFileName);
} catch (FileNotFoundException e) {
System.out.println("找不到quartz配置文件,文件名:" + quartzConfigFileName);
return;
}

// 得到quartz配置文件的目录
String quartzConfigPath = StringUtils.substringBeforeLast(quartzConfigFileName, "/");
System.out.println("得到quartz配置文件的目录:"+quartzConfigPath);
// 加载配置文件
Properties properties = new Properties();
try {
properties.load(fileInputStream);
System.out.println("====================="+File.separator);
// 设置quartz_jobs.xml路径
properties.setProperty("org.quartz.plugin.jobInitializer.fileName",
quartzConfigPath + File.separator + "quartz_jobs.xml");
System.out.println("========11111111=============");
} catch (IOException e) {
System.out.println("加载quartz配置文件失败,文件名:" + quartzConfigFileName);
return;
}
System.out.println("========ddddd=============");
// 启动quartz
StdSchedulerFactory factory ;
try {
factory = new StdSchedulerFactory(properties);
System.out.println("========222222222=============");
} catch (SchedulerException e) {
System.out.println("初始化quartz失败,失败原因:" + e.getMessage());
return;
}

// 启动quartz任务调度器
try {
System.out.println("========5555555555=============");
scheduler = factory.getScheduler();
System.out.println("========66666=============");
scheduler.start();
System.out.println("========7777777=============");
} catch (SchedulerException e) {
System.out.println("启动quartz任务调度失败,失败原因:" + e.getMessage());
return;
}

System.out.println("启动quartz任务调度成功!");
}

}

这里特别注意:
引入的包必须是:commons-collections-3.*.jar
它依赖:commons-beanutils-1.6.1.jar
当然呢,肯定有:quartz-1.6.0.jar
3.任务类:
package com.asire.ida.task;


import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class ShpTask  implements Job{

public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
System.out.println("我就是定时器................................"+new Date());
}

}

最后,我这个项目呢,在tomcat下,建了一个config的目录,config下又有一个quartz包
也就是config/quartz/文件
文件包括:quartz.properties和quartz_jobs.xml。
那么quartz.properties文件的内容:

#
# Configure Main Scheduler Properties
#

org.quartz.scheduler.instanceName = OP_Job_Scheduler
org.quartz.scheduler.instanceId = one

#
# Configure ThreadPool
#

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 1
org.quartz.threadPool.threadPriority = 8

#
# Configure JobStore
#

org.quartz.jobStore.misfireThreshold = 5000

org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

# ===========================================================================
# Configure SchedulerPlugins  ===============================================
# ===========================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingTriggerHistoryPlugin

org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileName = quartz_jobs.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = false
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 600

org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true



而quartz_jobs.xml文件的内容:
<?xml version='1.0' encoding='utf-8'?>
<quartz>
    <job>
        <job-detail>
            <name>PosCertApplyTask</name>
            <group>Default</group>
            <description>Quartz task for POS certificate apply.
            </description>
            <job-class>
                com.asire.ida.task.ShpTask
            </job-class>
        </job-detail>
          
        <trigger>
            <cron>
                <name>PosCertApplyTaskCron</name>
                <group>Default</group>
                <job-name>PosCertApplyTask</job-name>
                <job-group>Default</job-group>
                <!-- 每隔10分钟执行一次任务 -->
                <cron-expression>0 0/1 * * * ?</cron-expression>
            </cron>
        </trigger>          
    </job>

</quartz>

就这样,运行结果
我就是定时器................................Tue Nov 08 18:51:00 CST 2011
我就是定时器................................Tue Nov 08 18:52:00 CST 2011
我就是定时器................................Tue Nov 08 18:53:00 CST 2011
我就是定时器................................Tue Nov 08 18:54:00 CST 2011
我就是定时器................................Tue Nov 08 18:55:00 CST 2011
我就是定时器................................Tue Nov 08 18:56:00 CST 2011
我就是定时器................................Tue Nov 08 18:57:00 CST 2011
我就是定时器................................Tue Nov 08 18:58:00 CST 2011
我就是定时器................................Tue Nov 08 18:59:00 CST 2011
我就是定时器................................Tue Nov 08 19:00:00 CST 2011
我就是定时器................................Tue Nov 08 19:01:00 CST 2011
我就是定时器................................Tue Nov 08 19:02:00 CST 2011
我就是定时器................................Tue Nov 08 19:03:00 CST 2011
我就是定时器................................Tue Nov 08 19:04:00 CST 2011
我就是定时器................................Tue Nov 08 19:05:00 CST 2011



  • commons-beanutils-1.6.1.jar (115.9 KB)
  • 下载次数: 0
  • commons-collections-3.1.jar (546.3 KB)
  • 下载次数: 0
  • quartz-1.6.0.jar (411.7 KB)
  • 下载次数: 0
发表评论
用户名: 匿名