1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.component.quartz; |
19 |
|
|
20 |
|
import org.apache.camel.CamelContext; |
21 |
|
import org.apache.camel.impl.DefaultComponent; |
22 |
|
import org.apache.camel.util.IntrospectionSupport; |
23 |
|
import org.apache.camel.util.ObjectHelper; |
24 |
|
import org.apache.commons.logging.Log; |
25 |
|
import org.apache.commons.logging.LogFactory; |
26 |
|
import org.quartz.JobDetail; |
27 |
|
import org.quartz.Scheduler; |
28 |
|
import org.quartz.SchedulerException; |
29 |
|
import org.quartz.SchedulerFactory; |
30 |
|
import org.quartz.Trigger; |
31 |
|
import org.quartz.CronTrigger; |
32 |
|
import org.quartz.impl.StdSchedulerFactory; |
33 |
|
|
34 |
|
import java.util.Map; |
35 |
|
import java.net.URI; |
36 |
|
|
37 |
|
import com.sun.jndi.toolkit.url.Uri; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
4 |
public class QuartzComponent extends DefaultComponent<QuartzExchange> { |
45 |
1 |
private static final transient Log log = LogFactory.getLog(QuartzComponent.class); |
46 |
|
private SchedulerFactory factory; |
47 |
|
private Scheduler scheduler; |
48 |
|
private Map<Trigger, JobDetail> triggers; |
49 |
|
|
50 |
4 |
public QuartzComponent() { |
51 |
4 |
} |
52 |
|
|
53 |
|
public QuartzComponent(CamelContext context) { |
54 |
0 |
super(context); |
55 |
0 |
} |
56 |
|
|
57 |
|
@Override |
58 |
|
protected QuartzEndpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception { |
59 |
4 |
QuartzEndpoint answer = new QuartzEndpoint(uri, this, getScheduler()); |
60 |
|
|
61 |
|
|
62 |
4 |
URI u = new URI(uri); |
63 |
|
String name; |
64 |
4 |
String group = "Camel"; |
65 |
4 |
String path = u.getPath(); |
66 |
4 |
CronTrigger cronTrigger = null; |
67 |
4 |
if (path != null && path.length() > 1) { |
68 |
3 |
if (path.startsWith("/")) { |
69 |
3 |
path = path.substring(1); |
70 |
|
} |
71 |
3 |
int idx = path.indexOf('/'); |
72 |
3 |
if (idx > 0) { |
73 |
1 |
cronTrigger = new CronTrigger(); |
74 |
1 |
name = path.substring(0, idx); |
75 |
1 |
String cronExpression = path.substring(idx + 1); |
76 |
|
|
77 |
1 |
cronExpression = cronExpression.replace('/', ' '); |
78 |
1 |
cronExpression = cronExpression.replace('$', '?'); |
79 |
1 |
log.debug("Creating cron trigger: " + cronExpression); |
80 |
1 |
cronTrigger.setCronExpression(cronExpression); |
81 |
1 |
answer.setTrigger(cronTrigger); |
82 |
1 |
} |
83 |
|
else { |
84 |
2 |
name = path; |
85 |
|
} |
86 |
3 |
group = u.getHost(); |
87 |
3 |
} |
88 |
|
else { |
89 |
1 |
name = u.getHost(); |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
4 |
Trigger trigger = cronTrigger; |
102 |
4 |
if (trigger == null) { |
103 |
3 |
trigger = answer.getTrigger(); |
104 |
|
} |
105 |
4 |
trigger.setName(name); |
106 |
4 |
trigger.setGroup(group); |
107 |
|
|
108 |
4 |
Map triggerParameters = IntrospectionSupport.extractProperties(parameters, "trigger."); |
109 |
4 |
Map jobParameters = IntrospectionSupport.extractProperties(parameters, "job."); |
110 |
|
|
111 |
4 |
IntrospectionSupport.setProperties(trigger, triggerParameters); |
112 |
4 |
IntrospectionSupport.setProperties(answer.getJobDetail(), jobParameters); |
113 |
|
|
114 |
4 |
return answer; |
115 |
|
} |
116 |
|
|
117 |
|
@Override |
118 |
|
protected void doStart() throws Exception { |
119 |
4 |
super.doStart(); |
120 |
4 |
getScheduler().start(); |
121 |
4 |
} |
122 |
|
|
123 |
|
@Override |
124 |
|
protected void doStop() throws Exception { |
125 |
4 |
if (scheduler != null) { |
126 |
4 |
scheduler.shutdown(); |
127 |
|
} |
128 |
4 |
super.doStop(); |
129 |
4 |
} |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
public SchedulerFactory getFactory() { |
134 |
4 |
if (factory == null) { |
135 |
4 |
factory = createSchedulerFactory(); |
136 |
|
} |
137 |
4 |
return factory; |
138 |
|
} |
139 |
|
|
140 |
|
public void setFactory(SchedulerFactory factory) { |
141 |
0 |
this.factory = factory; |
142 |
0 |
} |
143 |
|
|
144 |
|
public Scheduler getScheduler() throws SchedulerException { |
145 |
8 |
if (scheduler == null) { |
146 |
4 |
scheduler = createScheduler(); |
147 |
|
} |
148 |
8 |
return scheduler; |
149 |
|
} |
150 |
|
|
151 |
|
public void setScheduler(Scheduler scheduler) { |
152 |
0 |
this.scheduler = scheduler; |
153 |
0 |
} |
154 |
|
|
155 |
|
public Map getTriggers() { |
156 |
0 |
return triggers; |
157 |
|
} |
158 |
|
|
159 |
|
public void setTriggers(Map triggers) { |
160 |
0 |
this.triggers = triggers; |
161 |
0 |
} |
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
protected SchedulerFactory createSchedulerFactory() { |
166 |
4 |
return new StdSchedulerFactory(); |
167 |
|
} |
168 |
|
|
169 |
|
protected Scheduler createScheduler() throws SchedulerException { |
170 |
4 |
return getFactory().getScheduler(); |
171 |
|
} |
172 |
|
} |