1
2
3
4
5
6
7
8
9
10
11 package org.mule.routing;
12
13 import org.mule.api.MessagingException;
14 import org.mule.api.MuleContext;
15 import org.mule.api.MuleEvent;
16 import org.mule.api.MuleException;
17 import org.mule.api.construct.FlowConstruct;
18 import org.mule.api.construct.FlowConstructAware;
19 import org.mule.api.context.MuleContextAware;
20 import org.mule.api.lifecycle.Initialisable;
21 import org.mule.api.lifecycle.InitialisationException;
22 import org.mule.api.lifecycle.Startable;
23 import org.mule.api.lifecycle.Stoppable;
24 import org.mule.api.routing.Aggregator;
25 import org.mule.api.routing.MessageInfoMapping;
26 import org.mule.api.service.Service;
27 import org.mule.processor.AbstractInterceptingMessageProcessor;
28 import org.mule.routing.correlation.EventCorrelator;
29 import org.mule.routing.correlation.EventCorrelatorCallback;
30 import org.mule.util.concurrent.ThreadNameHelper;
31
32
33
34
35
36
37
38
39 public abstract class AbstractAggregator extends AbstractInterceptingMessageProcessor
40 implements Initialisable, MuleContextAware, FlowConstructAware, Aggregator, Startable, Stoppable
41 {
42
43 protected EventCorrelator eventCorrelator;
44 protected MuleContext muleContext;
45 protected FlowConstruct flowConstruct;
46 protected MessageInfoMapping messageInfoMapping;
47
48 private long timeout = 0;
49 private boolean failOnTimeout = true;
50 protected boolean persistentStores;
51 protected String storePrefix = null;
52
53 public void initialise() throws InitialisationException
54 {
55 if (messageInfoMapping == null)
56 {
57 messageInfoMapping = flowConstruct.getMessageInfoMapping();
58 }
59 if (storePrefix == null)
60 {
61 storePrefix = String.format("%s%s.%s.", ThreadNameHelper.getPrefix(muleContext),
62 flowConstruct.getName(), this.getClass().getName());
63 }
64 eventCorrelator = new EventCorrelator(getCorrelatorCallback(muleContext), next, messageInfoMapping,
65 muleContext, flowConstruct.getName(), persistentStores, storePrefix);
66
67
68
69 if (flowConstruct instanceof Service)
70 {
71 Service service = (Service) flowConstruct;
72 if (service.getAsyncReplyMessageSource().getMessageProcessors().contains(this))
73 {
74 failOnTimeout = service.getAsyncReplyMessageSource().isFailOnTimeout();
75 }
76 }
77
78 eventCorrelator.setTimeout(timeout);
79 eventCorrelator.setFailOnTimeout(isFailOnTimeout());
80 }
81
82 public void start() throws MuleException
83 {
84 if (timeout != 0)
85 {
86 eventCorrelator.start();
87 }
88 }
89
90 public void stop() throws MuleException
91 {
92 eventCorrelator.stop();
93 }
94
95 public void setMuleContext(MuleContext context)
96 {
97 this.muleContext = context;
98 }
99
100 protected abstract EventCorrelatorCallback getCorrelatorCallback(MuleContext muleContext);
101
102 public MuleEvent process(MuleEvent event) throws MuleException
103 {
104 MuleEvent result = eventCorrelator.process(event);
105 if (result == null)
106 {
107 return null;
108 }
109 return processNext(result);
110 }
111
112 public void expireAggregation(String groupId) throws MessagingException
113 {
114 eventCorrelator.forceGroupExpiry(groupId);
115 }
116
117 public long getTimeout()
118 {
119 return timeout;
120 }
121
122 public void setTimeout(long timeout)
123 {
124 this.timeout = timeout;
125 }
126
127 public boolean isFailOnTimeout()
128 {
129 return failOnTimeout;
130 }
131
132 public void setFailOnTimeout(boolean failOnTimeout)
133 {
134 this.failOnTimeout = failOnTimeout;
135 }
136
137 public void setFlowConstruct(FlowConstruct flowConstruct)
138 {
139 this.flowConstruct = flowConstruct;
140 }
141
142 public void setMessageInfoMapping(MessageInfoMapping messageInfoMapping)
143 {
144 this.messageInfoMapping = messageInfoMapping;
145 }
146
147 public boolean isPersistentStores()
148 {
149 return persistentStores;
150 }
151
152 public void setPersistentStores(boolean persistentStores)
153 {
154 this.persistentStores = persistentStores;
155 }
156
157 public String getStorePrefix()
158 {
159 return storePrefix;
160 }
161
162 public void setStorePrefix(String storePrefix)
163 {
164 this.storePrefix = storePrefix;
165 }
166
167 }