spring 基于注解的@Scheduled和quartz定时器两种实现

一、使用quartz
1.由于我的项目jar包使用的maven托管的,在pom文件中加入quartz的依赖就可以
2.配置quartz-context.xml,确保xml文件能被加载到

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>    
        <!-- 要调用的工作类 -->
        <bean id="quartzJob" class="com.jsrush.common.web.QuartzJob"></bean>
        <!-- 定义调用对象和调用对象的方法 -->
        <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <!-- 调用的类 -->
            <property name="targetObject">
                <ref bean="quartzJob"/>
            </property>
            <!-- 调用类中的方法 -->
            <property name="targetMethod">
                <value>work</value>
            </property>
        </bean>
        <!-- 定义触发时间 -->
        <bean id="dotime" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail">
                <ref bean="jobtask"/>
            </property>
            <!-- cron表达式 -->
            <property name="cronExpression">
                <value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value>
            </property>
        </bean>
        <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->
        <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="dotime"/>
                </list>
            </property>
        </bean>
</beans>

二、使用spring @Scheduled注解的方式
1.这两个是一定要写的,加入task的命名空间

 <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:task="http://www.springframework.org/schema/task"
            xsi:schemaLocation="
            ......
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

task任务扫描注解与扫描位置

<task:annotation-driven/> 
<context:component-scan base-package="com.jsrush.common.web" />  

2.编写实现类

package com.jsrush.common.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class QuartzJob {


    @Scheduled(cron = "0,10,20,30,40,50 * * * * ? ")  
    public void work()
    {
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date=sdf.format(new Date());
    System.out.println(date+"---执行Quartz的任务调度!!!");
   // System.out.println(date+"---执行@Scheduled的任务调度!!!");
    }
}

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

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值