spring-quartz

首先在web的xml
<servlet>
        <servlet-name>defaultSystemInit</servlet-name>
        <servlet-class>xxx.xxx.xxx.app.common.servlet.DefaultInitServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <!--log4j配置文件相对路径(子系统必须要配置log4j)-->
            <param-name>log4jConfig</param-name>
            <param-value>xxx/log4j/log4j.xml</param-value>
        </init-param>
        <init-param>
            <!--quartz配置文件-->
            <param-name>quartzConfig</param-name>
            <param-value>xxx/quartz/quartz.properties</param-value>
        </init-param>
<init-param>
            <!--quartz配置文件-->
            <param-name>initAuthContext</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>

xxx.xxx.xxx.app.common.servlet.DefaultInitServlet类要extends HttpServlet
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public void init() throws ServletException {
        // BeanUtils使用Convert转换字段,重新注册转换器 add by lhm
        ConvertUtils.register(new IntegerConverter(null), Integer.class);
        ConvertUtils.register(new LongConverter(null), Long.class);

        // 初始化配置文件根目录
        initRootConfigPath();

        // 初始化log4j配置(日志输出目录等)
        initLog4j();

        // 初始化quartz配置(如果子系统有用quartz).
initQuartz();

            }
// 初始化quartz配置(quartz配置文件目录等).
// 从init-param中得到quartz.properties配置文件相对路径.
String quartzConfig = this.getInitParameter("quartzConfig");
if (quartzConfig == null || quartzConfig.length() == 0) {
logger.info("没有指定quartz配置文件相对路径!");
return;
}

// 得到quartz配置文件全路径名
String quartzConfigFileName = ConfigurationHelper.getFullFileName(quartzConfig);
FileInputStream fileInputStream ;
try {
logger.info("得到quartz配置文件全路径名:"+quartzConfigFileName);
fileInputStream = new FileInputStream(quartzConfigFileName);
} catch (FileNotFoundException e) {
logger.error("找不到quartz配置文件,文件名:" + quartzConfigFileName);
return;
}

// 得到quartz配置文件的目录
String quartzConfigPath = StringUtils.substringBeforeLast(quartzConfigFileName, "/");
logger.info("得到quartz配置文件的目录:"+quartzConfigPath);
// 加载配置文件
Properties properties = new Properties();
try {
properties.load(fileInputStream);
logger.info("====================="+File.separator);
// 设置quartz_jobs.xml路径
properties.setProperty("org.quartz.plugin.jobInitializer.fileName",
quartzConfigPath + File.separator + "job.xml");

} catch (IOException e) {
logger.error("加载quartz配置文件失败,文件名:" + quartzConfigFileName);
return;
}

// 启动quartz
StdSchedulerFactory factory ;
try {
factory = new StdSchedulerFactory(properties);
} catch (SchedulerException e) {
logger.error("初始化quartz失败,失败原因:" + e.getMessage());
return;
}

// 启动quartz任务调度器
try {
scheduler = factory.getScheduler();
scheduler.start();
} catch (SchedulerException e) {
logger.error("启动quartz任务调度失败,失败原因:" + e.getMessage());
return;
}
得到quartz配置文件全路径名:D:\Tomcat 5.5.0_CS\bin\config\op/quartz/quartz.properties

得到quartz配置文件的目录:D:\Tomcat 5.5.0_CS\bin\config\op/quartz
=====================\


接下来:
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

接下来:job.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.xxx.xxx.app.xxx.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/10 * * * ?</cron-expression>
            </cron>
        </trigger>          
    </job>

</quartz>
最后就是任务类:
package com.xxx.xxx.app.xxx.task;

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("我就是定时器................................");
}

}

你可能感兴趣的:(spring,xml,servlet,quartz,job)