zoukankan      html  css  js  c++  java
  • spring配置 quartz-config.xml

        <!-- 配置调度程序quartz ,其中配置JobDetail有两种方式-->
        <!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法-->
        <!-- 定义目标bean和bean中的方法 -->
        <bean id="SpringQtzJob" class="com.sky.JobSchedule.Job.JobTest"/>
        <bean id="SpringQtzJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject">
                <ref bean="SpringQtzJob"/>
            </property>
            <property name="targetMethod">  <!-- 要执行的方法名称 -->
                <value>helloSky</value>
            </property>
            <property name="arguments" value="11"/>
            <property name="concurrent " value="false"></property >     <!--非并发-->
        </bean>
    
        <!--  调度触发器  -->
        <bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="SpringQtzJobMethod"></property>
            <property name="cronExpression" value="0/5 * * * * ?"></property>
        </bean>
    
        <!--  调度工厂  -->
        <bean id="SpringJobSchedulerFactoryBean" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="CronTriggerBean"/>
                </list>
            </property>
        </bean>

      

    public class JobTest {
        String name;
    
        public JobTest() {
            System.out.println("Hello, Quartz! ----------------------");
        }
    
        public void helloSky(int age) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
            System.out.println("Hello, " + age + " sky !" + formatter.format(new Date()));
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    

      

        public static void main(String[] args) throws SchedulerException {
            ApplicationContext context = new ClassPathXmlApplicationContext("classpath:quartz-config.xml");
        }
    

      

    另外一种方式:任务类必须继承QuartzJobBean或者实现Job方法。

    <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
            <property name="jobClass" value="com.mc.bsframe.job.TestJob"></property>
            <property name="durability" value="true"></property>
        </bean>
    
        <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
            <property name="jobDetail" ref="jobDetail" />
            <property name="startDelay" value="3000" />
            <property name="repeatInterval" value="2000" />
        </bean>
    
        <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
        <bean id="DefaultQuartzScheduler" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <!-- 管理trigger -->
            <property name="triggers">
                <list>
                    <ref bean="simpleTrigger" />
                </list>
            </property>
          <property name="configLocation" value="classpath:quartz.properties" />
    </bean>

    只持久化jobDetail

    <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
            <property name="jobClass" value="com.sky.JobSchedule.Job.HelloQuartzJob"></property>
            <property name="durability" value="true"></property>
        </bean>
    
        <bean id="DefaultQuartzScheduler" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="jobDetails">
                <list>
                    <ref bean="jobDetail" />
                </list>
            </property>
            <property name="configLocation" value="classpath:quartz.properties" />
        </bean>
    

      

    两种方法的说明

    使用QuartzJobBean,需要继承。而使用MethodInvokeJobDetailFactoryBean则需要指定targetObject(任务实例)和targetMethod(实例中要执行的方法)

    后者优点是无侵入,业务逻辑简单,一目了然,缺点是无法持久化(目前还不太清楚这点!)

    从我使用的经验来说,我更推荐的第二种,其中一个很重要的原因就是因为定时任务中注入相关Service的时候,后者可以直接注入,而前者还需要进行Schedular的替换修改。

    上述配置的SchedulerFactoryBean的Id需要注意的是:

    假若需要进行可视化调度管理的话,不可缺的是获取所有的jobDetail等等,这时候需要注意的是,获取过程中,实例化Scheduler时,instanceName会根据quartz.properties来进行获取,没有的话默认“QuartzSchelduer”,会获取数据库中SCHED_NAME一致的数据。

    自定义的名字,可以直接保持配置的id和instanceName一致。

    或者显示的调用SchedulerFactoryBean:

    StdSchedulerFactory sf = new StdSchedulerFactory();
    Properties props = new Properties();
    props.put("org.quartz.scheduler.instanceName", "你定义的名字");
    props.put("org.quartz.threadPool.threadCount", "10");#必填
    sf.initialize(props);
    scheduler = sf.getScheduler();
    System.out.println(scheduler.getSchedulerName());
    scheduler.shutdown();
    

      

    添加监听器:在spring-quartz中,监听器:http://www.cnblogs.com/skyLogin/p/6928431.html

    <?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.xsd">
    
        <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
            <property name="jobClass" value="com.sky.JobSchedule.Job.HelloQuartzJob"></property>
            <property name="durability" value="true"></property>
        </bean>
        <bean id="jobDetail2" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
            <property name="jobClass" value="com.sky.JobSchedule.Job.JobCron"></property>
            <property name="name" value="test2"></property>
            <property name="durability" value="true"></property>
            <property name="jobDataAsMap">
                <map>
                    <entry key="name" value="sky"></entry>
                </map>
            </property>
        </bean>
    
        <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
            <property name="jobDetail" ref="jobDetail" />
            <property name="repeatInterval" value="2000" />
        </bean>
        <bean id="jobCountListener" class="com.sky.JobSchedule.Listener.JobCountListener" />
        <!--id-->
        <bean id="DefaultQuartzScheduler" lazy-init="false"  autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <!--<property name="autoStartup" value="false"></property>-->
            <property name="jobDetails">
                <list>
                    <ref bean="jobDetail" />
                    <ref bean="jobDetail2" />
                </list>
            </property>
            <!-- 管理trigger -->
            <property name="triggers">
                <list>
                    <ref bean="simpleTrigger" />
                </list>
            </property>
            <property name="configLocation" value="classpath:quartz.properties" />
            <property name="globalJobListeners" ref="jobCountListener"></property>
        </bean>
    </beans>
    

      

    quartz启动流程

    当服务器启动时,就会装载相关的bean。SchedulerFactoryBean实现了InitializingBean接口,因此在初始化bean的时候,会执行afterPropertiesSet方法,该方法将会调用SchedulerFactory(DirectSchedulerFactory 或者 StdSchedulerFactory,通常用StdSchedulerFactory)创建Scheduler。SchedulerFactory在创建quartzScheduler的过程中,将会读取配置参数,初始化各个组件

    1. ThreadPool:一般是使用SimpleThreadPool,SimpleThreadPool创建了一定数量的WorkerThread实例来使得Job能够在线程中进行处理。WorkerThread是定义在SimpleThreadPool类中的内部类,它实质上就是一个线程。在SimpleThreadPool中有三个list:workers-存放池中所有的线程引用,availWorkers-存放所有空闲的线程,busyWorkers-存放所有工作中的线程;

    2. JobStore:分为存储在内存的RAMJobStore和存储在数据库的JobStoreSupport(包括JobStoreTX和JobStoreCMT两种实现,JobStoreCMT是依赖于容器来进行事务的管理,而JobStoreTX是自己管理事务),若要使用集群要使用JobStoreSupport的方式;

    3. QuartzSchedulerThread:

    SchedulerFactoryBean还实现了SmartLifeCycle接口,因此初始化完成后,会执行start()方法,该方法将主要会执行以下的几个动作:

    1. 创建ClusterManager线程并启动线程:该线程用来进行集群故障检测和处理,将在下文详细讨论;
    2. 创建MisfireHandler线程并启动线程:该线程用来进行misfire任务的处理,将在下文详细讨论;
    3. 置QuartzSchedulerThread的paused=false,调度线程才真正开始调度;
  • 相关阅读:
    DPDK安装方法 17.12.13
    numa.h:No such file or directory 解决方法
    17秋 软件工程 第六次作业 Beta冲刺 Scrum3
    17秋 软件工程 第六次作业 Beta冲刺 总结博客
    17秋 软件工程 第六次作业 Beta冲刺 Scrum2
    Paper Reviews and Presentations
    17秋 软件工程 第六次作业 Beta冲刺 Scrum1
    17秋 软件工程 第六次作业 Beta冲刺
    error: could not create '/System/Library/Frameworks/Python.framework/Versions/2.7/share': Operation not permitted
    17秋 软件工程 个人作业 软件产品案例分析
  • 原文地址:https://www.cnblogs.com/skyLogin/p/6899156.html
Copyright © 2011-2022 走看看