JAVA语言定时任务调度工具详解 - Quartz 之 与 Spring 整合方法
小标 2018-12-26 来源 : 阅读 952 评论 0

摘要:本文主要向大家介绍JAVA语言定时任务调度工具详解 - Quartz 之 与 Spring 整合方法了,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍JAVA语言定时任务调度工具详解 - Quartz 之 与 Spring 整合方法了,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。


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


MethodInvokingJobDetailFactoryBean JobDetailFactoryBean

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


方式一:使用 MethodInvokingJobDetailFactoryBean


调用 myBean 的 printMessage 方法:


<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" id="simpleJobDetail">

    <property name="targetObject" ref="myBean">                                                           

    <property name="targetMethod" value="printMessage">                                                   

</property></property></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 class="org.springframework.scheduling.quartz.JobDetailFactoryBean" id="firstComplexJobDetail">

    <property name="jobClass" value="com.imooc.springquartz.quartz.FirstScheduledJob">            

    <property name="jobDataMap"><map>                                                                                       

            <entry key="anotherBean" value-ref="anotherBean">                                     

        </entry></map>                                                                                      

    </property>                                                                                     

    <property name="Durability" value="true">                                                     

</property></property></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 default-lazy-init="true" xmlns="https://www.springframework.org/schema/beans" xmlns:context="https://www.springframework.org/schema/context" xmlns:mvc="https://www.springframework.org/schema/mvc" xmlns:p="https://www.springframework.org/schema/p" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="https://www.springframework.org/schema/beans  

            https://www.springframework.org/schema/beans/spring-beans.xsd  

            https://www.springframework.org/schema/mvc  

            https://www.springframework.org/schema/mvc/spring-mvc.xsd  

            https://www.springframework.org/schema/context  

            https://www.springframework.org/schema/context/spring-context.xsd">

 

    <!-- 通过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>

    

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

   


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程