spring4.x.x quartz计划任务

来源:互联网 发布:周柏豪性格知乎 编辑:程序博客网 时间:2024/06/05 17:17

intellij idea新建maven工程

pom.xml加入spring和quartz的dependency

<dependency>    <groupId>org.quartz-scheduler</groupId>    <artifactId>quartz</artifactId>    <version>2.3.0</version></dependency>

applicationContext.xml内容如下:
红色字体部分为加入设置property为Date所需要的转换器
 
<?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:mvc="http://www.springframework.org/schema/mvc"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">    <bean id="dateConvert" class="com.yf.quartz.util.DateConvert"/>    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">        <property name="converters">            <set>                <ref bean="dateConvert"/>            </set>        </property>    </bean>    <mvc:annotation-driven conversion-service="conversionService"/>    <bean id="myTask" class="com.yf.quartz.entity.MyTask" />    <bean id="anotherTask" class="com.yf.quartz.entity.AnotherTask" />    <bean id="myJob"          class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">        <property name="targetObject" ref="myTask" />        <property name="targetMethod" value="doIt" />        <property name="concurrent" value="false" />    </bean>    <bean id="anotherJob"          class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">        <property name="targetObject" ref="anotherTask" />        <property name="targetMethod" value="doAnotherJob" />        <property name="concurrent" value="false" />    </bean>    <bean id="myTrigger"          class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">        <property name="jobDetail" ref="myJob" />        <property name="startDelay" value="5000" />        <property name="repeatInterval" value="3000" />    </bean>    <bean id="anotherTrigger"          class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">        <property name="jobDetail" ref="anotherJob" />        <property name="startTime">            <value type="java.util.Date">2017-05-04 16:22:00</value>        </property>        <property name="repeatInterval" value="5000" />    </bean>    <!-- 总调度用于启动Spring定时器 -->    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">        <property name="triggers">            <list>                <ref bean="myTrigger"></ref>                <ref bean="anotherTrigger"></ref>            </list>        </property>    </bean></beans>

web.xml中加载applicationContext.xml
<context-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:ApplicationContext.xml</param-value></context-param><listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

新建2个Task的java class文件
public class MyTask {    public void doIt(){        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        System.out.println(sdf.format(new Date()) +  " **doMyJob** ");        try {            Thread.sleep(10000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

public class AnotherTask {    public void doAnotherJob(){        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        System.out.println(sdf.format(new Date()) +  " **doAnotherJob** ");    }}
String转日期的java代码如下:
public class DateConvert implements Converter<String,Date> {    public Date convert(String source) {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        try {            return sdf.parse(source);        } catch (ParseException e) {            e.printStackTrace();            return null;        }    }}

启动web项目,即可看到日志:
2017-05-04 16:21:32 **doMyJob** 2017-05-04 16:21:44 **doMyJob** 2017-05-04 16:21:56 **doMyJob** 2017-05-04 16:22:00 **doAnotherJob** 2017-05-04 16:22:05 **doAnotherJob** 2017-05-04 16:22:08 **doMyJob** 

anotherJob在16:22:00才开始,说明启动时间设置有效.

0 0
原创粉丝点击