最近接到了一个需求,要求在指定时间执行不同任务。上网找了一些资料发现Quartz是一款很不错的插件。这里简单把自己写好的定时分享下。
1.需求执行任务 Task1
package com.zldigital.mobilevoice.util;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class Task1 extends QuartzJobBean{
private static final long serialVersionUID = 1L;
@Override@Test
protected void executeInternal(JobExecutionContext arg0)throws JobExecutionException {
System.out.println("任务1开始了");
}
}
}
2.需求执行任务 Task2
package com.zldigital.mobilevoice.util;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class Task2 extends QuartzJobBean{
private static final long serialVersionUID = 1L;
@Override@Test
protected void executeInternal(JobExecutionContext arg0)throws JobExecutionException {
System.out.println("任务2开始了");
}
}
}
quartz 配置文件这里分别在不同的时间内执行不同的任务。
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- Task1 -->
<bean name="job" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<!--执行任务的路径-->
<value>com.zldigital.mobilevoice.util.Task1</value>
</property>
</bean>
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="job" />
</property>
<property name="cronExpression">
<!--执行时间 5:28:01-->
<value>01 28 17 * * ?</value>
<!-- <value>0-1 59 23 * * ?</value>-->
</property>
</bean>
<!-- Task2 -->
<bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.zldigital.mobilevoice.util.Task2</value>
</property>
</bean>
<bean id="cronTrigger1"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="job1" />
</property>
<property name="cronExpression">
<value>01 23 17 * * ?</value>
</property>
</bean>
<!-- 任务列表 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- property name="concurrent" value="false"/-->
<property name="triggers">
<list>
<!-- 列表中需要执行的任务-->
<ref local="cronTrigger" />
<ref local="cronTrigger1" />
</list>
</property>
</bean>
</beans>
这里只是简单的配置了下,更高深的配置还需求慢慢探究。希望各位能给些建议!