SSM 下的定时调度任务 quartz 技术 和 @Scheduled 的两种方法

@Scheduled适用与监听任务较少的,而Quartz适合较多的,为确保可伸缩性,Quartz采用了基于多线程的架构。启动时,框架初始化一套worker线程,这套线程被调度器用来执行预定的作业。这就是Quartz怎样能并发运行多个作业的原理。Quartz依赖一套松耦合的线程池管理部件来管理线程环境。 

定时任务调度

一:基于spring 自带的注解调度

1 首先引入

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
 
2  然后进行配置文件配置
<?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:task="http://www.springframework.org/schema/task"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/task
      http://www.springframework.org/schema/task/spring-task.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">

    <!--spring 自带的定时器-->
    <task:annotation-driven/>
    <context:annotation-config/>
    <bean  class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    <context:component-scan base-package="com.food.wodo.springquartz"/><!-- 下面XzcfYjTask类在的包名 -->

</beans>

然后在spring 中引入

 

3基于注解任务类编写  要有3个注解@Component  @EnableSceduling  @Scheduling    cron 为设置时间, 0 为每   *  任意   ?  未指定

import com.food.wodo.dao.XzcfYjDao;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

@Component
@EnableScheduling
public class XzcfYjTask {

    @Autowired
    private XzcfYjDao xzcfYjDao;

    @Scheduled(cron = "0 0/5 * * * ?")
    public void sendMessage(){
        
    }


}

 二:quartz 技术的定时调度任务

1 首先引入

    <!--定时调度quartz技术-->
    <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz</artifactId>
      <version>2.2.2</version>
    </dependency>
    <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz-jobs</artifactId>
      <version>2.2.2</version>
    </dependency>
 

 日志包

<!--log4j && slf4j-->
 90         <dependency>
 91             <groupId>org.slf4j</groupId>
 92             <artifactId>slf4j-api</artifactId>
 93             <version>${slf4j.version}</version>
 94         </dependency>
 95         <dependency>
 96             <groupId>org.slf4j</groupId>
 97             <artifactId>slf4j-log4j12</artifactId>
 98             <version>${slf4j.version}</version>
 99         </dependency>
100         <dependency>
101             <groupId>org.slf4j</groupId>
102             <artifactId>jcl-over-slf4j</artifactId>
103             <version>${slf4j.version}</version>
104             <scope>runtime</scope>
105         </dependency>

 

 

 2  然后进行配置文件配置

<?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"
       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.xsd ">

    <!--扫描quartz类-->
    <context:component-scan base-package="com.food.*.quartz"/>

    <bean id="SpjyxkzdqYjJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.food.wodo.quartz.SpjyxkzdqYjJob"/>
        <property name="jobDataAsMap">
            <map>
                <entry key="timeout" value="5"/>
            </map>
        </property>
    </bean>

    <!-- ======================== 调度触发器 ======================== -->
    <bean id="cronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="SpjyxkzdqYjJob"></property>
        <property name="cronExpression" value="0 0/5 * * * ?"></property>
    </bean>

    <!-- ======================== 调度工厂 ======================== -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobFactory" ref="jobFactory" />
        <property name="triggers">
            <list>
                <ref bean="cronTriggerBean" />
            </list>
        </property>
    </bean>

</beans>

 

3 类的创建
 <!-- ======================== 调度工厂 ======================== -->
package com.food.wodo.quartz;


import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Service;

@Service("jobFactory")
public class JobFactory extends AdaptableJobFactory {
    @Autowired
    private AutowireCapableBeanFactory autowireCapableBeanFactory;

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
       Object  jobInstance=  super.createJobInstance(bundle);
        autowireCapableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }

}
quartz类-
package com.food.wodo.quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.Date;

public class SpjyxkzdqYjJob implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Date date=new Date();
        System.out.println("定时调度任务"+date);
    }
}

 


 

 

转载于:https://www.cnblogs.com/jsbk/p/9439704.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值