testQuartz.javajava
package com.common.quartz; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 定時任務類 * * @auther * @date */ @Component("testQuartz") public class testQuartz { private static final Logger log = LoggerFactory.getLogger(testQuartz.class); /** * 具體執行的方法 * * @auther * @date */ public void method() { log.info("start"); try { log.info("成功"); } } catch (Exception e) { log.error("失敗:" + e.getMessage()); } log.info("end"); } }
quartz.xmlspring
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 掃描定時器的包,有註解的都會被初始化爲定時器服務類 --> <context:component-scan base-package="com.common.scheduler" /> <!--禁止相同的任務併發--> <!-- <property name="concurrent" value="true" /> --> <!-- 定時器執行工廠 ,分別指定服務類jobDetails和觸發器triggers --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="testTrigger"/> </list> </property> </bean> <bean id="testDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="testQuartz" /> <property name="targetMethod" value="method" /> </bean> <bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="testDetail" /> <property name="cronExpression" value="0 0/1 * * * ?" /> <!-- cron表達式:設置啓動時間 --> </bean> </beans>
ApplicationContext.xml (spring配置文件)併發
<import resource="quartz/quartz.xml"/>
cron表達式在線生成器:http://cron.qqe2.com/component