后台工程师技术交流

两种方式部署定时任务,Spring整合Quartz,Spring

2019-02-26  本文已影响10人  prisoner_mirror

(Notice:以下所有经验也是我根据网上的经验整理的,如有侵权可以联系我删除,欢迎交流和沟通,Wx:IT_Ezra,QQ 654303408。 有问题讨论也可联系我。)

(Tips:我是第一次开发,一个刚毕业的java工程师,我觉得我并非天赋异禀,我能学会,相信聪敏的你,一定可以)

(PS:定时任务是一个非常常见的功能,在我们进行系统开发中,经常会用到,有点类似于OS中的批处理。对于一些实时性不高的任务,比如退款,消息推送,会用到定时任务。)

首先需要使用的jar包:用maven在pom.xml中配置。版本有很多哦,自己可以去世界库找一个。

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

项目结构图如下

image.png

先上代码。提前说一点,每一个class对应的****FactoryBean是对应的。一共有三个, 要么都用***Bean,要么都用****FactoryBean。用混的情况下是会报错的。(曾经在eclipse没报错,但是在idea报错了)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    <!-- 申明执行任务的类 -->
    <bean id="messageSendJob" class="com.dfs.job.MessageSendJob"></bean>

    <!-- 申请线程池 -->
    <bean id="schedulerExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="10" />
        <property name="maxPoolSize" value="100" />
        <property name="queueCapacity" value="500" />
    </bean>

    <!-- 调度业务 -->
    <bean id="messageSendJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="messageSendJob" />
        <property name="targetMethod" value="message" />
    </bean>

    <!-- 触发器 -->
    <bean id="messageSendCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="messageSendJobDetail" />
        <property name="cronExpression" value="* 0/20 * * * ?" />
    </bean>

    <!-- 设置调度 -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="messageSendCronTrigger" />
            </list>
        </property>
        <property name="taskExecutor" ref="schedulerExecutor" />
    </bean>
</beans>

其次一个需要介绍的就是具体的调度任务,也就是如下代码块

<!-- 调度业务 -->
    <bean id="messageSendJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="messageSendJob" />
        <property name="targetMethod" value="message" />
    </bean>

bean的id自己命名,property中name自己命名。而ref中的调度业务。 我的调度业务如下

package com.dfs.job;

/**
 * @ author ezra
 * @ date 2019/2/18 15:56
 */

import com.dfs.service.SenseAgroMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 消息推送定时任务
 * @author zjj
 * @date 2019-02-13
 */
@Component
public class MessageSendJob {

    private static final Logger logger = LoggerFactory.getLogger(MessageSendJob.class);

    /**
     * 每隔5分钟查询一次是否有消息
     */
    @Autowired
    private SenseAgroMessageService senseAgroMessageService;

    public void message() {
        logger.info("【begin】执行批量消息推送定时任务");
            // 执行批量消息推送操作
            senseAgroMessageService.messageAll();
        logger.info("【end】执行批量消息推送定时任务");
    }


    /**
     * 每隔一星期设置无效form(暂时不实现)
     */
/*
    public void templete() {
        logger.info("【begin】执行批量formid失效删除定时任务");
        // 为了防止定时任务多线程并行执行时出现退款多次的情况,对过程加上互斥锁保证该任务串行执行
        // 如果后期应用改为多机部署,需要将此处改为分布式锁或者由分布式任务调度系统协调串行执行
        synchronized (OrderRefundJob.class) {
            // 执行批量退款操作
            senseAgroMessageService.templateDelete();
        }
        logger.info("【end】执行批量formid失效删除定时任务");
    }
*/
}

完成以上操作之后,基本上定时任务已经配置完成。接下来最后一步,是通过web.xml去加载,应该每个人都知道applicationContext.xml的加载吧,在后面添加classpath:spring/spring-scheduler.xml即可。

  <!-- 获取requst respose对象-->
    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>
    <!-- 利用Spring提供的ContextLoaderListener监听器去监听ServletContext对象的创建,并初始化WebApplicationContext对象 -->
   <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
   <!-- Context Configuration locations for Spring XML files(默认查找/WEB-INF/applicationContext.xml) -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml,classpath:spring/spring-scheduler.xml</param-value>
    </context-param>

方法二,非常暴力,在application里面扫描添加扫描项<task:annotation-driven/>

通过注解完成。@Scheduled 后面接cron表达式。相当于集成spring-task

@Component
public class OrderRefundJob {

    private static final Logger logger = LoggerFactory.getLogger(OrderRefundJob.class);

    @Autowired
    private SenseAgroRefundService senseAgroRefundService;


    @Scheduled(cron = "* 0/5 *  * * ?")
    public void refund() {
        logger.info("【begin】执行批量退款定时任务开始");
        // 执行批量退款操作
        senseAgroRefundService.refundAll();
        logger.info("【end】执行批量退款定时任务结束");
    }

}
上一篇 下一篇

猜你喜欢

热点阅读