spring-quarty两种实现方式

简介: spring-quarty两种实现方式

引言


定时任务在项目中的使用场景还是非常多的,quarty框架也是非常不错的,尤其是和spring整合以后,在使用上更加简单了,这也是备受大家喜爱的原因,在这里不得不佩服spring的强悍的功力。其实想要达到定时任务的方式有很多,比如我在上一个项目中,什么框架都没有使用,直接用while...true 就实现了,还有通过linux系统本身的调度来实现,在这篇文章中主要介绍quarty这个任务调度的两种实现方式(xml配置和注解)。


准备工作:


在已有的ssm框架中,引入下面jar:


         <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>${quartz.version}</version>
        </dependency>

第一种: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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
    <bean id="startQuertz" lazy-init="false" autowire="no"
          class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <!--需要时 打开-->
                <!--<ref bean="sendCaseTrigger"/>-->
            </list>
        </property>
        <property name="quartzProperties">
            <props>
                <prop key="org.quartz.scheduler.skipUpdateCheck">true</prop>
            </props>
        </property>
    </bean>
    <!--定时给fs推送案件任务-->
    <bean id="sendCaseTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="sendCaseTask"/>
        </property>
        <property name="cronExpression">
            <!-- 每秒执行一次 -->
            <value>0/3 * * * * ? </value>
        </property>
    </bean>
    <bean id="sendCaseTask"
          class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="sendCaseTaskJob"/>
        </property>
        <property name="targetMethod">
            <value>sendCaseTask</value>
        </property>
    </bean>
    <bean id="sendCaseTaskJob" class="com.zqf.urgerobot.task.service.TaskService"></bean>
    <!--定时任务二-->
    <bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="testTask"/>
        </property>
        <property name="cronExpression">
            <!-- 每秒执行一次 -->
            <value>0/1 * * * * ? </value>
        </property>
    </bean>
    <bean id="testTask"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="testTaskBean"/>
        </property>
        <property name="targetMethod">
            <value>test</value>
        </property>
    </bean>
    <bean id="testTaskBean" class="com.zqf.urgerobot.task.service.TaskService"></bean>
</beans>

业务实现类:

@Service
public class TaskService {
    private static Logger log = LoggerFactory.getLogger(TaskService.class);
    @Autowired
    private FsCallService fsCallService;
    /**
      * @description 案件推送任务
      * @param
      * @date 10:45 2018/8/25
      * @return
      * @author zhenghao
    */
    public void sendCaseTask(){
        fsCallService.sendCaseTask();
    }
    public void  test(){
        System.out.println("test");
    }
}

这种方式相比没有和spring整合的时候已经非常简单了,但是在配置多个任务的时候,还是比较繁琐一些。下面看一下注解的方式实现。


第二种:注解方式实现

 @Scheduled(cron = "0/3 * * * * ?")
    public void  test(){
        System.out.println("test");
    }


在我们业务处理方法上,添加一个注解@Scheduled即可,我们最关注的是cron这个参数,它的作用也就是控制方法执行的时机。下面看一下这个注解又实现了那些东西:

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
    String cron() default "";
    String zone() default "";
    long fixedDelay() default -1L;
    String fixedDelayString() default "";
    long fixedRate() default -1L;
    String fixedRateString() default "";
    long initialDelay() default -1L;
    String initialDelayString() default "";
}


我们在ssm中多处使用注解,所以对注解非常的熟悉,我们仅仅在方法上添加注解是远远不够的,如果想让注解起到作用我们还需要告诉spring容器,我们要启动这个注解,所以我们还需要增加下面配置:


<?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:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-4.3.xsd"
>
    <context:component-scan base-package="com.zqf.urgerobot" />
    <task:annotation-driven scheduler="synScheduler"/>
    <task:scheduler id="synScheduler" pool-size="10"/>
</beans>

配置文件中的几个配置还是非常简单的,需要注意的是:在Spring配置文件中为task增加相应的命名空间。


总结


注解的方式相对xml方式又简单了很多,简单配置以后,就可以灵活的使用,所以在这个项目中,我采用了注解的方式。希望对各位读者有所帮助。


心情一抒:


心中有所念,喜忧各参半!@过去

目录
相关文章
|
存储 网络协议 API
大端与小端概念、多字节之间与单字节多部分的大小端转换详解
大端与小端概念、多字节之间与单字节多部分的大小端转换详解
957 1
|
监控 druid Java
Spring Boot 3 集成 Druid 连接池详解
在现代的Java应用中,使用一个高效可靠的数据源是至关重要的。Druid连接池作为一款强大的数据库连接池,提供了丰富的监控和管理功能,成为很多Java项目的首选。本文将详细介绍如何在Spring Boot 3项目中配置数据源,集成Druid连接池,以实现更高效的数据库连接管理。
9302 2
Spring Boot 3 集成 Druid 连接池详解
|
Java Maven 开发者
@EnableFeignClients:简化微服务间调用的艺术
@EnableFeignClients:简化微服务间调用的艺术
1733 2
|
消息中间件 设计模式 Java
SpringBoot+Schedule 定时任务的配置开关
SpringBoot+Schedule 定时任务的配置开关
335 0
SpringBoot+Schedule 定时任务的配置开关
|
Java 数据库
request.setAttribute()详解
request.setAttribute()详解
1039 1
|
数据采集 XML 前端开发
Spring MVC(spring-webmvc)之全局数据处理、拦截器、自定义类型转换器等使用指南
Spring MVC(spring-webmvc)之全局数据处理、拦截器、自定义类型转换器等使用指南
452 0
Spring MVC(spring-webmvc)之全局数据处理、拦截器、自定义类型转换器等使用指南
|
XML Java API
Apache POI详解及Word文档读取示例
apache poi资料详解,包括内部jar包依赖关系,及与使用文档的对应关系
2984 0
|
存储 JSON 前端开发
一步到位 SpringBoot 序列化与消息转换器 (你需要的这里都有)
本篇文章记录的为SpringBoot Jackson序列化,ObjectMapper,configureMessageConverters,MappingJackson2HttpMessageConverter消息转换器相关内容,适合在学Java的小白,帮助新手快速上手,也适合复习中,面试中的大佬
4619 0