1)依賴spring
<!-- 定時任務開始 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>1.8.5</version> </dependency> <!-- 定時任務結束-->
<?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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 定時任務 --> <!-- 任務1 --> <bean name="quartzTest" class="top.heyuantao.manager.controller.Task" /> <bean id="quartzTestJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="quartzTest"></property> <property name="targetMethod" value="test"></property> <property name="concurrent" value="false"></property> </bean> <!-- 任務1指定策略 --> <bean id="quartzTestTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="quartzTestJob"/> <!-- 每十秒執行一次 --> <property name="cronExpression" value="0/10 * * * * ?"></property> </bean> <!-- 任務2 --> <bean name="quartzmain" class="top.heyuantao.manager.controller.Task" /> <bean id="quartzmainJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="quartzTest"></property> <property name="targetMethod" value="main"></property> <property name="concurrent" value="false"></property> </bean> <!-- 任務2指定策略 --> <bean id="quartzmainTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="quartzmainJob"/> <!-- 每五秒執行一次 --> <property name="cronExpression" value="0/5 * * * * ?"></property> </bean> <!-- 啓動觸發器的配置 --> <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="quartzTestTrigger" /> <ref local="quartzmainTrigger" /> </list> </property> </bean> </beans>
package top.heyuantao.manager.controller;
import top.heyuantao.manager.utli.Time;
public class Task {
public void test() {
System.out.println("任務1執行中"+"---------->"+Time.getTime());
}
public static void main(String[] args) {
System.out.println("任務2執行中"+"---------->"+Time.getTime());
}
}
http://cron.qqe2.com/mvc