1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kcb.config;
17
18 import org.kuali.rice.kcb.service.GlobalKCBServiceLocator;
19 import org.kuali.rice.ksb.service.KSBServiceLocator;
20 import org.quartz.JobDetail;
21 import org.quartz.ObjectAlreadyExistsException;
22 import org.quartz.Scheduler;
23 import org.quartz.SchedulerException;
24 import org.quartz.Trigger;
25 import org.springframework.beans.BeansException;
26 import org.springframework.beans.factory.BeanFactory;
27 import org.springframework.beans.factory.BeanFactoryAware;
28 import org.springframework.beans.factory.DisposableBean;
29 import org.springframework.beans.factory.InitializingBean;
30 import org.springframework.beans.factory.annotation.Required;
31
32
33
34
35
36
37
38 public class KCBInitializer implements BeanFactoryAware, InitializingBean, DisposableBean {
39 private BeanFactory beanFactory;
40 private Trigger messageProcessingTrigger;
41 private JobDetail messageProcessingJobDetail;
42 protected Scheduler scheduler;
43
44 @Override
45 public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
46 this.beanFactory = beanFactory;
47 }
48
49
50
51
52
53 @Required
54 public void setMessageProcessingTrigger(Trigger messageProcessingTrigger) {
55 this.messageProcessingTrigger = messageProcessingTrigger;
56 }
57
58
59
60
61
62 @Required
63 public void setMessageProcessingJobDetail(JobDetail messageProcessingJobDetail) {
64 this.messageProcessingJobDetail = messageProcessingJobDetail;
65 }
66
67
68 @Override
69 public void afterPropertiesSet() throws Exception {
70 GlobalKCBServiceLocator.init(beanFactory);
71
72 beanFactory = null;
73
74 Scheduler scheduler = getScheduler()==null?KSBServiceLocator.getScheduler():getScheduler();
75 scheduler.addJob(messageProcessingJobDetail, true);
76
77 addTriggerToScheduler(messageProcessingTrigger);
78 }
79
80 private void addTriggerToScheduler(Trigger trigger) throws SchedulerException {
81 boolean triggerExists = (getScheduler().getTrigger(trigger.getName(), trigger.getGroup()) != null);
82 if (!triggerExists) {
83 try {
84 getScheduler().scheduleJob(trigger);
85 } catch (ObjectAlreadyExistsException ex) {
86 getScheduler().rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
87 }
88 } else {
89 getScheduler().rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
90 }
91 }
92
93 @Override
94 public void destroy() throws Exception {
95
96
97 GlobalKCBServiceLocator.destroy();
98 }
99
100
101
102
103 public Scheduler getScheduler() {
104 return this.scheduler;
105 }
106
107
108
109
110 public void setScheduler(Scheduler scheduler) {
111 this.scheduler = scheduler;
112 }
113
114 }