• 如何在spring中运行多个schedulers quartz 实例


    http://wzping.iteye.com/blog/468263

    1、定义一个JOB 
    <!-- 使用pojo来做job,指定pojo和method --> 
        <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
            <property name="targetObject" ref="pojoObject"/> 
            <property name="targetMethod" value="execute"/> 
        </bean> 

    2、定义一个触发时间 
        <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
            <property name="jobDetail" ref="jobDetail"/> 
            <!--朝九晚五工作时间内每半小时执行一次--> 
            <property name="cronExpression" value="0 0/30 9-17 *,* * ?"/> 
        </bean> 

    3、定义管理器 
        <bean name="quartzScheduler" 
              class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
            <property name="triggers"> 
                <list> 
                    <ref bean="cronTrigger"/> 
                </list> 
            </property> 
        </bean> 

    一般的应用,只要配置上面这些东东就可以了。 

    但如果有大量的定时执行程序,而且有时需要分别启动,这个时候就要用到两个Scheduler, 在spring中是怎么实现的呢?简单地配置两个SchedulerFactoryBean是不解决问题的。原因如下: 

    看一下SchedulerFactoryBean的代码,里面一个参数叫做:schedulerName,SchedulerFactoryBean通过 StdSchedulerFactory返回一个具体的Scheduler的。而且每个Scheduler是注册在 SchedulerRepository中的。 
    SchedulerRepository中的每个Scheduler都是放在一个MAP中的,根据名字作为KEY。 

    private HashMap schedulers; 

    这样就比较好办了。 
        <bean name="quartzScheduler" 
              class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
            <property name="triggers"> 
                <list> 
                    <ref bean="cronTrigger"/> 
                </list> 
            </property> 
            <property name="schedulerName"><value>first</value></property> 
        </bean> 
    可以声明多个。并设上不同名字,这样scheduler就分有多个实例,可以分别启动、停止了。

  • 相关阅读:
    php_sphinx安装使用
    深入Web请求过程(笔记)
    php的单例模式
    解决rsync 同步auth failed on module问题
    生成shadow中hash字串
    python程序不支持中文
    xshell4无法使用小键盘问题解决
    LVS客户端启动脚本
    更改linux系统提示信息
    使用md5判断网站内容是否被篡改
  • 原文地址:https://www.cnblogs.com/exmyth/p/6897832.html
Copyright © 2020-2023  润新知