zoukankan      html  css  js  c++  java
  • quartz+spring 实现多任务动态定时器问题

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">
    
    
        <!-- The Task Bean-->
        <bean id="myTask" class="com.xxx.example.dynamicjob.MyTask" />
    
        <!-- The quartz scheduler configuration -->
        <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"/>
    
    </beans>
    

      

    package com.xxx.example.dynamicjob;
    
        public class MyTask
        {
            public void performAction() {
                System.out.println("Hey, you reached me...:)");
            }          
        }
    

      

    package com.xxx.example.dynamicjob;
    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.scheduling.quartz.CronTriggerBean;
    import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
    
    public class DynamicJobExample {
    
        /**
        * @param args
        */
        public static void main(String[] args) {
    
                    ClassPathResource res = new ClassPathResource("dynamic-jobs.xml");
                    XmlBeanFactory factory = new XmlBeanFactory(res);
    
                    //get the quartzFactory bean
                    Scheduler scheduler = (Scheduler) factory.getBean("scheduler");
    
                    //get the task bean
                    MyTask myTask = (MyTask) factory.getBean("myTask");
    
                    try {
                    // create JOB
                    MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
                    jobDetail.setTargetObject(myTask);
                    jobDetail.setTargetMethod("performAction");
                    jobDetail.setName("MyJobDetail");
                    jobDetail.setConcurrent(false);
                    jobDetail.afterPropertiesSet();
    
                    /* SimpleTriggerBean trigger = new SimpleTriggerBean();
                    trigger.setBeanName("MyTrigger");
                    trigger.setJobDetail((JobDetail) jobDetail.getObject());
                    trigger.setRepeatInterval(5000);
                    trigger.afterPropertiesSet();
                    */
    
                    // create CRON Trigger
                    CronTriggerBean cronTrigger = new CronTriggerBean();
                    cronTrigger.setBeanName("CRON0001");
    
                    // Execute after each 5 second
                    String expression = "5 * * * * ?";
                    cronTrigger.setCronExpression(expression);
                    cronTrigger.afterPropertiesSet();
    
                    //scheduler.scheduleJob(jobDetail, cronTrigger);
    
                    scheduler.scheduleJob((JobDetail) jobDetail.getObject(), cronTrigger);
    
                    // Start Scheduler        
                    scheduler.start();
    
                    } catch (Exception e) {                      
                        e.printStackTrace();
                    } 
        }
    }
    

      

     

  • 相关阅读:
    php常用函数
    检测到有潜在危险的 Request.Form 值
    未能加载文件或程序集“XXX”或它的一个依赖项,试图加载格式不正确的程序
    尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题
    java基础知识
    .net中excel遇到的一些问题
    easyui验证
    .NET牛人需要了解的问题[转]
    关于easyui遇到的一些问题
    MVC 路由介绍
  • 原文地址:https://www.cnblogs.com/Nbge/p/3783603.html
Copyright © 2011-2022 走看看