Spring 任务调度

来源:互联网 发布:怎么抢购淘宝一元拍 编辑:程序博客网 时间:2024/06/02 03:35
Quartz
Spring 创建 Quartz 的 Schedule、Trigger 和 JobDetail 提供便利的 FactoryBean类,就能够使用 Spring 注入方式。概括起来提供两方面支持 :
1> 为 Quartz 的重要组件类提供更具 Bean 风格的扩展类
2> 提供创建 Schedule 的 BeanFactory类,方便在 Spring 环境下创建对应的组建对象,并组合 Spring 容器生命周期进行启动和停止动作

使用步骤
1> 在 web.xml 中引入对应的配置文件
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

2> 在 applicationContext.xml 中的配置信息
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="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.2.xsd"
      default-lazy-init="true">

    <!-- Job 任务详情 -->
   <beanid="testJob"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <propertyname="targetObject"ref="testQuartzService"/><!-- 目标任务 -->
       <propertyname="targetMethod"value="test1"/>
        <propertyname="concurrent"value="false"/>
    </bean>

    <!-- Trigger 调度触发器 -->
   <beanid="testTrigger"class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <propertyname="jobDetail"ref="testJob"/>
        <propertyname="cronExpression"value="0/5 * * * * ?"/>
    </bean>
   
    <!-- Scheduler 调度工厂 -->
   <beanid="testScheduler"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <propertyname="triggers">
            <list>
               <refbean="testTrigger"/>
            </list>
        </property>
    </bean>
</beans>

也可以单独将配置放入一个文件中
<!--quartz-->
<importresource="classpath:quartz/applicationContext-quartz.xml"/>

3> 对应的接口实现方法
public interfaceTestQuartz {
    void test1();
}

@Service("testQuartzService")
@Transactional
public classTestQuartzImpl implements TestQuartz {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    public void test1() {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.logger.debug(this.getClass().getName() + ">>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<< "+ sdf.format(newDate()));
    }
}

Quatz 实现集群的方式
方式一 : 通过一个中间数据库,将集群节点相互感知,以实现故障切换
方式二 : 通过 Terracotta 进行集群,Terracotta 是一个 JVM 级的开源集群框架,简单的说就是将多个 JVM 透明化成一个 JVM 使用

Timer 和 TimerTask
TimerTask 代表一个需要多次执行的任务,它实现 Runnable 接口,可以在  run() 方法中定义任务逻辑

TimerTask 相当于 Quartz 中 Job,代表一次任务调度,区别在于 : 每当执行任务时,Quartz 都会创建一个 Job 实例,而 JDK Timer 则使用相同的 TimerTask 实例

Spring 在 org.springframework.schedulingtimer 中提供几个 JDK  Timer 的主支持类,主要在以下三个方面对 JDK Timer 提供支持 :
1> ScheduledTimerTask,它对 TimerTask 提供封装并提供相关的配置
2> 通过 MethodInvokingTimerTaskFactoryBean 类可以将一个 Bean 的方法封装封装为 TimerTask
3> 通过 TimerFactoryBean 可以更方便地配置 Timer,此外让 Timer 的生命周期和 Spring容器的生命周期相关,在初始化 TimerFactoryBean 后,启动 Timer,在 Spring 容器关闭前取消 Timer

引入配置文件的方式同上
<importresource="classpath:applicationContext-timer.xml"/>

applicationContext-timer.xml 中的配置内容
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <beanid="myService"class="com.chenshun.test.timer.MyService"/>
    <beanid="timerTask1"
         class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"
         p:targetObject-ref="myService"
         p:targetMethod="doJob"/>
    <!-- 创建定时任务 -->
   <beanid="scheduledTask"
         class="org.springframework.scheduling.timer.ScheduledTimerTask"
         p:delay="10000"
         p:period="10000"
         p:timerTask-ref="timerTask1"/>
    <!-- 要运行的定时任务 -->
   <beanid="timer"class="org.springframework.scheduling.timer.TimerFactoryBean">
        <propertyname="scheduledTimerTasks">
            <list>
               <refbean="scheduledTask"/>
            </list>
        </property>
    </bean>
</beans>

对应的执行方法
public classMyService {
    public void doJob(){
        System.out.println("in MyService.dojob().");
    }
}

注 : org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean、org.springframework.scheduling.timer.ScheduledTimerTask、org.springframework.scheduling.timer.TimerFactoryBean 这三个类目前都已经被丢弃,在实际开发时使用 Quartz 方式更合适

创建动态任务的方式 :
1> 业务流程产生型 : 在进行一个业务时,在业务操作过程中产生业务
2> 扫面线程产生型 : 由一个任务线程定时扫描业务数据库的任务表,并根据业务数据产生任务

Web 应用程序中调度器的启动和关闭问题
静态变量是ClassLoader 级别,如果 Web应用程序停止,这些静态变量也会从 JVM 中清除。但是线程则是JVM级别的,如果用户在 Web 应用中启动一个线程,这个线程的生命周期并不会和 Web 应用程序保持同步。也就是说,及时停止 Web 应用,这个应用依旧是活跃的。若手动使用 JDK Timer 或 Quartz 的 Scheduler,在 Web 容器启动后再关闭,这个Timer 或 Scheduler还在继续运行

使用Spring 提供 TimerFactoryBean 和 SchedulerFactoryBean 能够和 Spring 容器一起关联,在 Spring 容器停止时,相关调度器停止 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 出租车从业资格证到期怎么办 养老金认证身份证不符怎么办 硬盘指示灯不亮怎么办 做业务产品质量很差怎么办 没有户籍证明了怎么办 回执编号忘了怎么办 泳镜里面花了怎么办 网页打印预览空白怎么办 中专毕业证掉了怎么办 会计证年检忘了怎么办 会计准考证丢了怎么办 从业资格证没带怎么办 安全证过期了怎么办 安全员证掉了怎么办 局部抗浮不满足怎么办 职称代评被骗怎么办 租到了公租房怎么办 公租房不住了怎么办 公寓不退押金怎么办 找物业租房被骗怎么办 租房子被骗了怎么办 公租房怎么办入住手续 重庆公租房摇到号怎么办 重庆公租房摇到号后怎么办 我被网上起诉怎么办 摇号摇到了不买怎么办 保障房离婚了怎么办 深圳有房子户口怎么办 北京户口没有房怎么办 选房顺序号靠后怎么办 房子拖着不开盘怎么办 楼开盘人多怎么办 低保取消廉租房怎么办 航空公司病退审核慢怎么办 江里水流大跑线怎么办 js逻辑好难怎么办 怀孕了好无聊怎么办 做表率我们怎么办心得 晚上左转逆行了怎么办 鼓浪屿船票买不到怎么办 信用卡逾期房贷批不下来怎么办