項目開發中常常須要執行一些定時任務,好比須要在天天凌晨時候,分析一次前一天的日誌信息,藉此機會整理了一下定時任務的幾種實現方式,因爲項目採用spring框架,因此我都將結合spring框架來介紹。java
import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class Job1 extends QuartzJobBean { private int timeout; private static int i = 0; //調度工廠實例化後,通過timeout時間開始執行調度 public void setTimeout(int timeout) { this.timeout = timeout; } /** * 要調度的具體任務 */ @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("定時任務執行中…"); } }
Xml代碼 <bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.gy.Job1" /> <property name="jobDataAsMap"> <map> <entry key="timeout" value="0" /> </map> </property> </bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="job1" /> <property name="startDelay" value="0" /><!-- 調度工廠實例化後,通過0秒開始執行調度 --> <property name="repeatInterval" value="2000" /><!-- 每2秒調度一次 --> </bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="job1" /> <!—天天12:00運行一次 --> <property name="cronExpression" value="0 0 12 * * ?" /> </bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean>
public class Job2 { public void doJob2() { System.out.println("不繼承QuartzJobBean方式-調度進行中..."); } }
<bean id="job2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <bean class="com.gy.Job2" /> </property> <property name="targetMethod" value="doJob2" /> <property name="concurrent" value="false" /><!-- 做業不併發調度 --> </bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="job2" /> <property name="startDelay" value="0" /><!-- 調度工廠實例化後,通過0秒開始執行調度 --> <property name="repeatInterval" value="2000" /><!-- 每2秒調度一次 --> </bean>
Xml代碼 <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="job2" /> <!—天天12:00運行一次 --> <property name="cronExpression" value="0 0 12 * * ?" /> </bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean>
import org.springframework.stereotype.Service; @Service public class TaskJob { public void job1() { System.out.println(「任務進行中。。。」); } }
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" 。。。。。。 xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<task:scheduled-tasks> <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/> </task:scheduled-tasks> <context:component-scan base-package=" com.gy.mytask " />
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Scheduled { public abstract String cron(); public abstract long fixedDelay(); public abstract long fixedRate(); }
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component(「taskJob」) public class TaskJob { @Scheduled(cron = "0 0 3 * * ?") public void job1() { System.out.println(「任務進行中。。。」); } }
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" default-lazy-init="false"> <context:annotation-config /> <!—spring掃描註解的配置 --> <context:component-scan base-package="com.gy.mytask" /> <!—開啓這個配置,spring才能識別@Scheduled註解 --> <task:annotation-driven scheduler="qbScheduler" mode="proxy"/> <task:scheduler id="qbScheduler" pool-size="10"/>