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
45
46
47 public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
48 this.beanFactory = beanFactory;
49 }
50
51
52
53
54
55 @Required
56 public void setMessageProcessingTrigger(Trigger messageProcessingTrigger) {
57 this.messageProcessingTrigger = messageProcessingTrigger;
58 }
59
60
61
62
63
64 @Required
65 public void setMessageProcessingJobDetail(JobDetail messageProcessingJobDetail) {
66 this.messageProcessingJobDetail = messageProcessingJobDetail;
67 }
68
69
70
71
72 public void afterPropertiesSet() throws Exception {
73 GlobalKCBServiceLocator.init(beanFactory);
74
75 beanFactory = null;
76
77 Scheduler scheduler = getScheduler()==null?KSBServiceLocator.getScheduler():getScheduler();
78 scheduler.addJob(messageProcessingJobDetail, true);
79
80 addTriggerToScheduler(messageProcessingTrigger);
81 }
82
83 private void addTriggerToScheduler(Trigger trigger) throws SchedulerException {
84 boolean triggerExists = (getScheduler().getTrigger(trigger.getName(), trigger.getGroup()) != null);
85 if (!triggerExists) {
86 try {
87 getScheduler().scheduleJob(trigger);
88 } catch (ObjectAlreadyExistsException ex) {
89 getScheduler().rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
90 }
91 } else {
92 getScheduler().rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
93 }
94 }
95
96 public void destroy() throws Exception {
97
98
99 GlobalKCBServiceLocator.destroy();
100 }
101
102
103
104
105 public Scheduler getScheduler() {
106 return this.scheduler;
107 }
108
109
110
111
112 public void setScheduler(Scheduler scheduler) {
113 this.scheduler = scheduler;
114 }
115
116 }