码迷,mamicode.com
首页 > 编程语言 > 详细

quartz+spring 实现多任务动态定时器问题

时间:2014-06-16 09:34:53      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   java   http   tar   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    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>

  

1
2
3
4
5
6
7
8
package com.xxx.example.dynamicjob;
 
    public class MyTask
    {
        public void performAction() {
            System.out.println("Hey, you reached me...:)");
        }         
    }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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();
                }
    }
}

  

 

quartz+spring 实现多任务动态定时器问题,布布扣,bubuko.com

quartz+spring 实现多任务动态定时器问题

标签:class   blog   code   java   http   tar   

原文地址:http://www.cnblogs.com/Nbge/p/3783603.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!