1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.umo.UMOComponent; |
15 | |
import org.mule.umo.UMOException; |
16 | |
import org.mule.umo.endpoint.UMOEndpoint; |
17 | |
import org.mule.umo.lifecycle.InitialisationException; |
18 | |
import org.mule.umo.provider.UMOConnector; |
19 | |
import org.mule.util.ObjectUtils; |
20 | |
|
21 | |
import java.util.Iterator; |
22 | |
import java.util.LinkedList; |
23 | |
import java.util.List; |
24 | |
|
25 | |
import edu.emory.mathcs.backport.java.util.concurrent.ScheduledFuture; |
26 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public abstract class AbstractPollingMessageReceiver extends AbstractMessageReceiver |
35 | |
{ |
36 | |
public static final long DEFAULT_POLL_FREQUENCY = 1000; |
37 | 0 | public static final TimeUnit DEFAULT_POLL_TIMEUNIT = TimeUnit.MILLISECONDS; |
38 | |
|
39 | |
public static final long DEFAULT_STARTUP_DELAY = 1000; |
40 | |
|
41 | 0 | private long frequency = DEFAULT_POLL_FREQUENCY; |
42 | 0 | private TimeUnit timeUnit = DEFAULT_POLL_TIMEUNIT; |
43 | |
|
44 | |
|
45 | 0 | protected final List schedules = new LinkedList(); |
46 | |
|
47 | |
public AbstractPollingMessageReceiver(UMOConnector connector, |
48 | |
UMOComponent component, |
49 | |
final UMOEndpoint endpoint) throws InitialisationException |
50 | |
{ |
51 | 0 | super(connector, component, endpoint); |
52 | 0 | } |
53 | |
|
54 | |
protected void doStart() throws UMOException |
55 | |
{ |
56 | |
try |
57 | |
{ |
58 | 0 | synchronized (schedules) |
59 | |
{ |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | 0 | ScheduledFuture schedule = connector.getScheduler().scheduleWithFixedDelay( |
65 | |
new PollingReceiverWorkerSchedule(this.createWork()), DEFAULT_STARTUP_DELAY, |
66 | |
this.getFrequency(), this.getTimeUnit()); |
67 | 0 | schedules.add(schedule); |
68 | |
|
69 | 0 | if (logger.isDebugEnabled()) |
70 | |
{ |
71 | 0 | logger.debug(ObjectUtils.identityToShortString(this) + " scheduled " |
72 | |
+ ObjectUtils.identityToShortString(schedule) + " with " + frequency |
73 | |
+ " " + getTimeUnit() + " polling frequency"); |
74 | |
} |
75 | 0 | } |
76 | |
} |
77 | 0 | catch (Exception ex) |
78 | |
{ |
79 | 0 | this.stop(); |
80 | 0 | throw new InitialisationException(CoreMessages.failedToScheduleWork(), ex, this); |
81 | 0 | } |
82 | 0 | } |
83 | |
|
84 | |
protected void doStop() throws UMOException |
85 | |
{ |
86 | 0 | synchronized (schedules) |
87 | |
{ |
88 | |
|
89 | |
|
90 | 0 | for (Iterator i = schedules.iterator(); i.hasNext();) |
91 | |
{ |
92 | 0 | ScheduledFuture schedule = (ScheduledFuture)i.next(); |
93 | 0 | schedule.cancel(false); |
94 | 0 | i.remove(); |
95 | |
|
96 | 0 | if (logger.isDebugEnabled()) |
97 | |
{ |
98 | 0 | logger.debug(ObjectUtils.identityToShortString(this) + " cancelled polling schedule: " |
99 | |
+ ObjectUtils.identityToShortString(schedule)); |
100 | |
} |
101 | |
} |
102 | 0 | } |
103 | 0 | } |
104 | |
|
105 | |
protected PollingReceiverWorker createWork() |
106 | |
{ |
107 | 0 | return new PollingReceiverWorker(this); |
108 | |
} |
109 | |
|
110 | |
public long getFrequency() |
111 | |
{ |
112 | 0 | return frequency; |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
public void setFrequency(long value) |
118 | |
{ |
119 | 0 | if (value <= 0) |
120 | |
{ |
121 | 0 | frequency = DEFAULT_POLL_FREQUENCY; |
122 | |
} |
123 | |
else |
124 | |
{ |
125 | 0 | frequency = value; |
126 | |
} |
127 | 0 | } |
128 | |
|
129 | |
public TimeUnit getTimeUnit() |
130 | |
{ |
131 | 0 | return timeUnit; |
132 | |
} |
133 | |
|
134 | |
public void setTimeUnit(TimeUnit timeUnit) |
135 | |
{ |
136 | 0 | this.timeUnit = timeUnit; |
137 | 0 | } |
138 | |
|
139 | |
public abstract void poll() throws Exception; |
140 | |
|
141 | |
} |