Java定时任务调度工具详解(9)— Quartz 之 与 Spring 整合

使用 Quartz 配置作业的两种方式:

  • MethodInvokingJobDetailFactoryBean
  • JobDetailFactoryBean

源码地址:https://gitee.com/liupeifeng3514/Spring-Quartz

方式一:使用 MethodInvokingJobDetailFactoryBean

调用 myBean 的 printMessage 方法:

<bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="myBean" />                                                           
    <property name="targetMethod" value="printMessage" />                                                   
</bean>                                                                                                     

MyBean 类:

@Component("myBean")
public class MyBean {
    public void printMessage() {
        // 打印当前的执行时间,格式为2017-01-01 00:00:00
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("MyBean Executes!" + sf.format(date));
    }
}
方式二:使用 JobDetailFactoryBean

需要给作业传递数据,想要更加灵活的话就是用这种方式:

<bean id="firstComplexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
    <property name="jobClass" value="com.imooc.springquartz.quartz.FirstScheduledJob" />            
    <property name="jobDataMap">                                                                    
        <map>                                                                                       
            <entry key="anotherBean" value-ref="anotherBean" />                                     
        </map>                                                                                      
    </property>                                                                                     
    <property name="Durability" value="true" />                                                     
</bean>                                                                                             

FirstScheduleJob类:

public class FirstScheduledJob extends QuartzJobBean{
     private AnotherBean anotherBean;

     public void setAnotherBean(AnotherBean anotherBean){
         this.anotherBean = anotherBean;
     }

    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("FirstScheduledJob Executes!" + sf.format(date));
        this.anotherBean.printAnotherMessage();     
    }
}

AnotherBean 类:

@Component("anotherBean")
public class AnotherBean {
    public void printAnotherMessage() {
        System.out.println("AnotherMessage");
    }
}
完整的 xml 配置

<?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"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    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  
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context.xsd"
    default-lazy-init="true">

    <!-- 通过mvc:resources设置静态资源,这样servlet就会处理这些静态资源,而不通过控制器 -->
    <!-- 设置不过滤内容,比如:css,jquery,img 等资源文件 -->
    <mvc:resources location="/*.html" mapping="/**.html" />
    <mvc:resources location="/css/*" mapping="/css/**" />
    <mvc:resources location="/js/*" mapping="/js/**" />
    <mvc:resources location="/images/*" mapping="/images/**" />

    <!-- 设定消息转换的编码为utf-8,防止controller返回中文乱码 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>

    <!-- 添加注解驱动 -->
    <mvc:annotation-driven />

    <!-- 默认扫描的包路径 -->
    <context:component-scan base-package="com.imooc.springquartz" />

    <!-- mvc:view-controller可以在不需要Controller处理request的情况,转向到设置的View -->
    <!-- 像下面这样设置,如果请求为/,则不通过controller,而直接解析为/index.jsp -->
    <mvc:view-controller path="/" view-name="index" />

    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <!-- 配置jsp路径前缀 -->
        <property name="prefix" value="/"></property>
        <!-- 配置URl后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myBean" />
        <property name="targetMethod" value="printMessage" />
    </bean>

    <bean id="firstComplexJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.imooc.springquartz.quartz.FirstScheduledJob" />
        <property name="jobDataMap">
            <map>
                <entry key="anotherBean" value-ref="anotherBean" />
            </map>
        </property>
        <property name="Durability" value="true" />
    </bean>

    <!-- 距离当前时间1秒之后执行,之后每隔两秒钟执行一次 -->
    <bean id="mySimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="simpleJobDetail" />
        <property name="startDelay" value="1000" />
        <property name="repeatInterval" value="2000" />
    </bean>

    <!-- 每隔5秒钟执行一次 -->
    <bean id="myCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="firstComplexJobDetail" />
        <property name="cronExpression" value="0/5 * * ? * *" />
    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="simpleJobDetail" />
                <ref bean="firstComplexJobDetail" />
            </list>
        </property>
        <property name="triggers">
            <list>
                <ref bean="mySimpleTrigger" />
                <ref bean="myCronTrigger" />
            </list>
        </property>
    </bean>
</beans>  
程序运行结果

MyBean Executes!2018-05-04 23:17:47
MyBean Executes!2018-05-04 23:17:49
FirstScheduledJob Executes!2018-05-04 23:17:50
AnotherMessage
MyBean Executes!2018-05-04 23:17:51
MyBean Executes!2018-05-04 23:17:53
FirstScheduledJob Executes!2018-05-04 23:17:55
AnotherMessage
MyBean Executes!2018-05-04 23:17:55
MyBean Executes!2018-05-04 23:17:57
MyBean Executes!2018-05-04 23:17:59
FirstScheduledJob Executes!2018-05-04 23:18:00
AnotherMessage
MyBean Executes!2018-05-04 23:18:01
MyBean Executes!2018-05-04 23:18:03
FirstScheduledJob Executes!2018-05-04 23:18:05
AnotherMessage
MyBean Executes!2018-05-04 23:18:05
MyBean Executes!2018-05-04 23:18:07
MyBean Executes!2018-05-04 23:18:09
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值