1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport; |
8 | |
|
9 | |
import org.mule.api.MuleException; |
10 | |
import org.mule.api.construct.FlowConstruct; |
11 | |
import org.mule.api.endpoint.InboundEndpoint; |
12 | |
import org.mule.api.lifecycle.CreateException; |
13 | |
import org.mule.api.transport.Connector; |
14 | |
import org.mule.config.i18n.CoreMessages; |
15 | |
import org.mule.util.ObjectUtils; |
16 | |
|
17 | |
import java.util.HashMap; |
18 | |
import java.util.Iterator; |
19 | |
import java.util.Map; |
20 | |
|
21 | |
import edu.emory.mathcs.backport.java.util.concurrent.Future; |
22 | |
import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException; |
23 | |
import edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService; |
24 | |
import edu.emory.mathcs.backport.java.util.concurrent.ScheduledFuture; |
25 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public abstract class AbstractPollingMessageReceiver extends AbstractMessageReceiver |
34 | |
{ |
35 | |
public static final long DEFAULT_POLL_FREQUENCY = 1000; |
36 | 0 | public static final TimeUnit DEFAULT_POLL_TIMEUNIT = TimeUnit.MILLISECONDS; |
37 | |
|
38 | |
public static final long DEFAULT_STARTUP_DELAY = 1000; |
39 | |
|
40 | 0 | private long frequency = DEFAULT_POLL_FREQUENCY; |
41 | 0 | private TimeUnit timeUnit = DEFAULT_POLL_TIMEUNIT; |
42 | |
|
43 | |
|
44 | 0 | protected final Map<ScheduledFuture, PollingReceiverWorker> schedules = new HashMap<ScheduledFuture, PollingReceiverWorker>(); |
45 | |
|
46 | |
public AbstractPollingMessageReceiver(Connector connector, |
47 | |
FlowConstruct flowConstruct, |
48 | |
final InboundEndpoint endpoint) throws CreateException |
49 | |
{ |
50 | 0 | super(connector, flowConstruct, endpoint); |
51 | 0 | } |
52 | |
|
53 | |
@Override |
54 | |
protected void doStart() throws MuleException |
55 | |
{ |
56 | |
try |
57 | |
{ |
58 | 0 | this.schedule(); |
59 | |
} |
60 | 0 | catch (Exception ex) |
61 | |
{ |
62 | 0 | this.stop(); |
63 | 0 | throw new CreateException(CoreMessages.failedToScheduleWork(), ex, this); |
64 | 0 | } |
65 | 0 | } |
66 | |
|
67 | |
@Override |
68 | |
protected void doStop() throws MuleException |
69 | |
{ |
70 | 0 | this.unschedule(); |
71 | 0 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
protected void schedule() |
84 | |
throws RejectedExecutionException, NullPointerException, IllegalArgumentException |
85 | |
{ |
86 | 0 | synchronized (schedules) |
87 | |
{ |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | 0 | PollingReceiverWorker pollingReceiverWorker = this.createWork(); |
93 | 0 | ScheduledFuture schedule = connector.getScheduler().scheduleWithFixedDelay( |
94 | |
new PollingReceiverWorkerSchedule(pollingReceiverWorker), DEFAULT_STARTUP_DELAY, |
95 | |
this.getFrequency(), this.getTimeUnit()); |
96 | 0 | schedules.put(schedule, pollingReceiverWorker); |
97 | |
|
98 | 0 | if (logger.isDebugEnabled()) |
99 | |
{ |
100 | 0 | logger.debug(ObjectUtils.identityToShortString(this) + " scheduled " |
101 | |
+ ObjectUtils.identityToShortString(schedule) + " with " + frequency |
102 | |
+ " " + getTimeUnit() + " polling frequency"); |
103 | |
} |
104 | 0 | } |
105 | 0 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
protected void unschedule() |
113 | |
{ |
114 | 0 | synchronized (schedules) |
115 | |
{ |
116 | |
|
117 | 0 | for (Iterator<ScheduledFuture> i = schedules.keySet().iterator(); i.hasNext();) |
118 | |
{ |
119 | 0 | ScheduledFuture schedule = i.next(); |
120 | 0 | schedule.cancel(false); |
121 | |
|
122 | 0 | int shutdownTimeout = connector.getMuleContext().getConfiguration().getShutdownTimeout(); |
123 | 0 | PollingReceiverWorker worker = schedules.get(schedule); |
124 | 0 | for (int elapsed = 0; worker.isRunning() && elapsed < shutdownTimeout; elapsed += 50) |
125 | |
{ |
126 | |
try |
127 | |
{ |
128 | 0 | Thread.sleep(50); |
129 | |
} |
130 | 0 | catch (InterruptedException e) |
131 | |
{ |
132 | 0 | logger.warn( |
133 | |
ObjectUtils.identityToShortString(this) + " interrupted while waiting for poll() to complete as part of message receiver stop.", |
134 | |
e); |
135 | 0 | break; |
136 | 0 | } |
137 | |
} |
138 | 0 | i.remove(); |
139 | |
|
140 | 0 | if (logger.isDebugEnabled()) |
141 | |
{ |
142 | 0 | logger.debug(ObjectUtils.identityToShortString(this) + " cancelled polling schedule: " |
143 | |
+ ObjectUtils.identityToShortString(schedule)); |
144 | |
} |
145 | 0 | } |
146 | 0 | } |
147 | 0 | } |
148 | |
|
149 | |
public void disableNativeScheduling() |
150 | |
{ |
151 | 0 | this.unschedule(); |
152 | 0 | } |
153 | |
|
154 | |
protected PollingReceiverWorker createWork() |
155 | |
{ |
156 | 0 | return new PollingReceiverWorker(this); |
157 | |
} |
158 | |
|
159 | |
public long getFrequency() |
160 | |
{ |
161 | 0 | return frequency; |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
public void setFrequency(long value) |
167 | |
{ |
168 | 0 | if (value <= 0) |
169 | |
{ |
170 | 0 | frequency = DEFAULT_POLL_FREQUENCY; |
171 | |
} |
172 | |
else |
173 | |
{ |
174 | 0 | frequency = value; |
175 | |
} |
176 | 0 | } |
177 | |
|
178 | |
public TimeUnit getTimeUnit() |
179 | |
{ |
180 | 0 | return timeUnit; |
181 | |
} |
182 | |
|
183 | |
public void setTimeUnit(TimeUnit timeUnit) |
184 | |
{ |
185 | 0 | this.timeUnit = timeUnit; |
186 | 0 | } |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
protected int getBatchSize(int available) |
199 | |
{ |
200 | 0 | if (available <= 0) |
201 | |
{ |
202 | 0 | return 0; |
203 | |
} |
204 | |
|
205 | 0 | int maxThreads = connector.getReceiverThreadingProfile().getMaxThreadsActive(); |
206 | 0 | return Math.max(1, Math.min(available, ((maxThreads / 2) - 1))); |
207 | |
} |
208 | |
|
209 | |
public abstract void poll() throws Exception; |
210 | |
|
211 | |
} |