1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.component.timer; |
18 |
|
|
19 |
|
import java.lang.reflect.InvocationHandler; |
20 |
|
import java.lang.reflect.InvocationTargetException; |
21 |
|
import java.lang.reflect.Method; |
22 |
|
import java.lang.reflect.Proxy; |
23 |
|
import java.util.Timer; |
24 |
|
import java.util.TimerTask; |
25 |
|
|
26 |
|
import org.apache.camel.Processor; |
27 |
|
import org.apache.camel.component.bean.BeanExchange; |
28 |
|
import org.apache.camel.component.bean.BeanInvocation; |
29 |
|
import org.apache.camel.impl.DefaultConsumer; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
public class TimerConsumer extends DefaultConsumer<BeanExchange> implements InvocationHandler { |
35 |
|
|
36 |
|
private final TimerEndpoint endpoint; |
37 |
|
private Timer timer; |
38 |
|
|
39 |
|
public TimerConsumer(TimerEndpoint endpoint, Processor processor) { |
40 |
3 |
super(endpoint, processor); |
41 |
3 |
this.endpoint = endpoint; |
42 |
3 |
} |
43 |
|
|
44 |
|
@Override |
45 |
|
protected void doStart() throws Exception { |
46 |
3 |
TimerComponent component = endpoint.getComponent(); |
47 |
3 |
component.addConsumer(this); |
48 |
3 |
timer = createTimerAndTask(); |
49 |
3 |
} |
50 |
|
|
51 |
|
@Override |
52 |
|
protected void doStop() throws Exception { |
53 |
3 |
if (timer != null) { |
54 |
3 |
timer.cancel(); |
55 |
|
} |
56 |
3 |
TimerComponent component = endpoint.getComponent(); |
57 |
3 |
component.removeConsumer(this); |
58 |
3 |
} |
59 |
|
|
60 |
|
private Timer createTimerAndTask() { |
61 |
|
|
62 |
3 |
final Runnable proxy = createProxy(); |
63 |
3 |
TimerTask task = new TimerTask() { |
64 |
|
@Override |
65 |
3 |
public void run() { |
66 |
15 |
proxy.run(); |
67 |
15 |
} |
68 |
|
}; |
69 |
|
|
70 |
3 |
Timer result = new Timer(endpoint.getTimerName(), endpoint.isDaemon()); |
71 |
3 |
if (endpoint.isFixedRate()) { |
72 |
3 |
if (endpoint.getTime() != null) { |
73 |
0 |
result.scheduleAtFixedRate(task, endpoint.getTime(), endpoint.getPeriod()); |
74 |
0 |
} else { |
75 |
3 |
result.scheduleAtFixedRate(task, endpoint.getDelay(), endpoint.getPeriod()); |
76 |
|
} |
77 |
3 |
} else { |
78 |
0 |
if (endpoint.getTime() != null) { |
79 |
0 |
if (endpoint.getPeriod() >= 0) { |
80 |
0 |
result.schedule(task, endpoint.getTime(), endpoint.getPeriod()); |
81 |
0 |
} else { |
82 |
0 |
result.schedule(task, endpoint.getTime()); |
83 |
|
} |
84 |
0 |
} else { |
85 |
0 |
if (endpoint.getPeriod() >= 0) { |
86 |
0 |
result.schedule(task, endpoint.getDelay(), endpoint.getPeriod()); |
87 |
0 |
} else { |
88 |
0 |
result.schedule(task, endpoint.getDelay()); |
89 |
|
} |
90 |
|
} |
91 |
|
} |
92 |
3 |
return result; |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
public Runnable createProxy() { |
99 |
3 |
return (Runnable)Proxy.newProxyInstance(Runnable.class.getClassLoader(), |
100 |
|
new Class[] {Runnable.class}, this); |
101 |
|
} |
102 |
|
|
103 |
|
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
104 |
15 |
if (!isStarted()) { |
105 |
0 |
throw new IllegalStateException("The endpoint is not active: " + getEndpoint().getEndpointUri()); |
106 |
|
} |
107 |
15 |
BeanInvocation invocation = new BeanInvocation(proxy, method, args); |
108 |
15 |
BeanExchange exchange = getEndpoint().createExchange(); |
109 |
15 |
exchange.setInvocation(invocation); |
110 |
15 |
getProcessor().process(exchange); |
111 |
15 |
Throwable fault = exchange.getException(); |
112 |
15 |
if (fault != null) { |
113 |
0 |
throw new InvocationTargetException(fault); |
114 |
|
} |
115 |
15 |
return exchange.getOut().getBody(); |
116 |
|
} |
117 |
|
|
118 |
|
} |