工程的pom.xml添加依赖
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.0</version>
</dependency>
创建quatz.properties文件
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = QuartzScheduler
org.quartz.scheduler.instanceId = AUTO
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
org.quartz.threadPool.threadPriority = 5
#============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#============================================================================
# Configure Plugins
#============================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz_job.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval =10
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false
org.quartz.scheduler.skipUpdateCheck = true
创建quartz_job.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data
xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_2_0.xsd"
version="2.2.0">
<schedule>
<!-- 今日校准检查 -->
<job>
<name>CheckOrgEnterLogsJob</name>
<group>marketGroup</group>
<description>my job</description>
<job-class>com.xx.xx.quartz.CheckOrgEnterLogsQuartz</job-class>
</job>
<trigger>
<cron>
<name>CheckOrgEnterLogsTrigger</name>
<group>marketGroup</group>
<job-name>CheckOrgEnterLogsJob</job-name>
<job-group>marketGroup</job-group>
<!-- 常用的corn表达式
*隔5秒执行一次:*/5 * * * * ?
*隔1分钟执行一次:0 */1 * * * ?
*23点执行一次:0 0 23 * * ?
*晨1点执行一次:0 0 1 * * ?
*月1号凌晨1点执行一次:0 0 1 1 * ?
*月最后一天23点执行一次:0 0 23 L * ?
*周星期天凌晨1点实行一次:0 0 1 ? * L
*在26分、29分、33分执行一次:0 26,29,33 * * * ?
*的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?
-->
<cron-expression>0 0 1 * * ?</cron-expression>
</cron>
</trigger>
</schedule>
</job-scheduling-data>
创建QuatzServlet.java:Quatz初始化
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
import com.cloudcc.market.bean.MarketConstantInfo;
/**
* Quatz定时任务
*/
@WebListener
public class QuatzServlet implements ServletContextListener {
private static Log log = LogFactory.getLog(QuatzServlet.class);
Scheduler scheduler = null;
@Override
public void contextInitialized(ServletContextEvent sce) {
log.info("Quatz定时任务已启动");
try {
MarketConstantInfo.PATH = sce.getServletContext().getRealPath("/");
//1.从StdSchedulerFactory工厂中获取一个任务调度器
scheduler = StdSchedulerFactory.getDefaultScheduler();
//2. 启动调度器
scheduler.start();
log.info("Quatz启动");
} catch (Exception e) {
e.printStackTrace();
log.error("启动定时任务发生异常:",e);
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
try{
//关闭调度器
scheduler.shutdown();
log.info("Quatz已停止");
} catch (Exception e) {
e.printStackTrace();
log.error("停止定时任务发生异常:",e);
}
}
}
定时任务CheckLoginQuartz.java :处理service
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.cloudcc.market.bean.MarketConstantInfo;
import com.cloudcc.market.service.CheckLoginSerice;
import com.cloudcc.market.util.DataUtils;
/**
* 登录检查
*/
public class CheckLoginQuartz implements Job {
private static Log log = LogFactory.getLog(CheckLoginQuartz.class);
public void execute(JobExecutionContext context) throws JobExecutionException {
try{
if( System.getProperty("reyo.localPort").equals("设置允许定时任务的端口")) CheckLoginSerice.checkLogin();//执行定时操作
}catch(Exception e){
log.error("登录检查发生异常 :" ,e);
}
}
}
注意:
在定时任务的时候多个tomcate集群,定时任务会执行很多次。
所以在tomcate中/bin/catalina.bat中设置允许定时任务访问的端口:
增加set JAVA_OPTS=-Dreyo.localPort="8080" ;(每个tomcate都要设置相同conf/service.xml中的端口)
catalina.sh中增加JAVA_OPTS=-Dreyo.localPort="8080"
在执行任务的服务类中进行System.getProperty("reyo.localPort")获取当前访问的tomcate的端口,然后进行验证。这样就会解决定时任务只在设定的端口下才会访问执行。