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 public static final TimeUnit DEFAULT_POLL_TIMEUNIT = TimeUnit.MILLISECONDS;
38
39 public static final long DEFAULT_STARTUP_DELAY = 1000;
40
41 private long frequency = DEFAULT_POLL_FREQUENCY;
42 private TimeUnit timeUnit = DEFAULT_POLL_TIMEUNIT;
43
44
45 protected final List schedules = new LinkedList();
46
47 public AbstractPollingMessageReceiver(UMOConnector connector,
48 UMOComponent component,
49 final UMOEndpoint endpoint) throws InitialisationException
50 {
51 super(connector, component, endpoint);
52 }
53
54 protected void doStart() throws UMOException
55 {
56 try
57 {
58 synchronized (schedules)
59 {
60
61
62
63
64 ScheduledFuture schedule = connector.getScheduler().scheduleWithFixedDelay(
65 new PollingReceiverWorkerSchedule(this.createWork()), DEFAULT_STARTUP_DELAY,
66 this.getFrequency(), this.getTimeUnit());
67 schedules.add(schedule);
68
69 if (logger.isDebugEnabled())
70 {
71 logger.debug(ObjectUtils.identityToShortString(this) + " scheduled "
72 + ObjectUtils.identityToShortString(schedule) + " with " + frequency
73 + " " + getTimeUnit() + " polling frequency");
74 }
75 }
76 }
77 catch (Exception ex)
78 {
79 this.stop();
80 throw new InitialisationException(CoreMessages.failedToScheduleWork(), ex, this);
81 }
82 }
83
84 protected void doStop() throws UMOException
85 {
86 synchronized (schedules)
87 {
88
89
90 for (Iterator i = schedules.iterator(); i.hasNext();)
91 {
92 ScheduledFuture schedule = (ScheduledFuture)i.next();
93 schedule.cancel(false);
94 i.remove();
95
96 if (logger.isDebugEnabled())
97 {
98 logger.debug(ObjectUtils.identityToShortString(this) + " cancelled polling schedule: "
99 + ObjectUtils.identityToShortString(schedule));
100 }
101 }
102 }
103 }
104
105 protected PollingReceiverWorker createWork()
106 {
107 return new PollingReceiverWorker(this);
108 }
109
110 public long getFrequency()
111 {
112 return frequency;
113 }
114
115
116
117 public void setFrequency(long value)
118 {
119 if (value <= 0)
120 {
121 frequency = DEFAULT_POLL_FREQUENCY;
122 }
123 else
124 {
125 frequency = value;
126 }
127 }
128
129 public TimeUnit getTimeUnit()
130 {
131 return timeUnit;
132 }
133
134 public void setTimeUnit(TimeUnit timeUnit)
135 {
136 this.timeUnit = timeUnit;
137 }
138
139 public abstract void poll() throws Exception;
140
141 }