1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.model.seda; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.config.MuleConfiguration; |
16 | |
import org.mule.api.config.ThreadingProfile; |
17 | |
import org.mule.api.context.WorkManager; |
18 | |
import org.mule.api.context.WorkManagerSource; |
19 | |
import org.mule.api.lifecycle.InitialisationException; |
20 | |
import org.mule.api.lifecycle.LifecycleException; |
21 | |
import org.mule.api.processor.MessageProcessor; |
22 | |
import org.mule.api.processor.MessageProcessorChainBuilder; |
23 | |
import org.mule.config.ChainedThreadingProfile; |
24 | |
import org.mule.config.QueueProfile; |
25 | |
import org.mule.config.i18n.CoreMessages; |
26 | |
import org.mule.config.i18n.MessageFactory; |
27 | |
import org.mule.interceptor.ProcessingTimeInterceptor; |
28 | |
import org.mule.lifecycle.processor.ProcessIfStartedWaitIfSyncPausedMessageProcessor; |
29 | |
import org.mule.management.stats.ServiceStatistics; |
30 | |
import org.mule.processor.SedaStageInterceptingMessageProcessor; |
31 | |
import org.mule.service.AbstractService; |
32 | |
import org.mule.service.processor.ServiceInternalMessageProcessor; |
33 | |
import org.mule.service.processor.ServiceLoggingMessageProcessor; |
34 | |
import org.mule.service.processor.ServiceOutboundMessageProcessor; |
35 | |
import org.mule.service.processor.ServiceOutboundStatisticsMessageProcessor; |
36 | |
import org.mule.service.processor.ServiceSetEventRequestContextMessageProcessor; |
37 | |
import org.mule.service.processor.ServiceStatisticsMessageProcessor; |
38 | |
import org.mule.util.concurrent.ThreadNameHelper; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public class SedaService extends AbstractService |
46 | |
{ |
47 | |
|
48 | |
|
49 | |
|
50 | |
private static final long serialVersionUID = 7711976708670893015L; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
protected Integer queueTimeout; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
protected ThreadingProfile threadingProfile; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
protected QueueProfile queueProfile; |
68 | |
|
69 | |
protected WorkManager workManager; |
70 | |
|
71 | |
public SedaService(MuleContext muleContext) |
72 | |
{ |
73 | 0 | super(muleContext); |
74 | 0 | } |
75 | |
|
76 | |
@Override |
77 | |
protected MessageProcessor getServiceStartedAssertingMessageProcessor() |
78 | |
{ |
79 | 0 | return new ProcessIfStartedWaitIfSyncPausedMessageProcessor(this, lifecycleManager.getState()); |
80 | |
} |
81 | |
|
82 | |
protected void addMessageProcessors(MessageProcessorChainBuilder builder) |
83 | |
{ |
84 | 0 | builder.chain(new ProcessingTimeInterceptor(null, this)); |
85 | 0 | builder.chain(new ServiceLoggingMessageProcessor(this)); |
86 | 0 | builder.chain(new ServiceStatisticsMessageProcessor(this)); |
87 | 0 | builder.chain(new ServiceSetEventRequestContextMessageProcessor()); |
88 | 0 | if (getThreadingProfile().isDoThreading()) |
89 | |
{ |
90 | 0 | builder.chain(new SedaStageInterceptingMessageProcessor(getName(), queueProfile, queueTimeout, |
91 | |
new WorkManagerSource() |
92 | 0 | { |
93 | |
public WorkManager getWorkManager() throws MuleException |
94 | |
{ |
95 | 0 | return workManager; |
96 | |
} |
97 | |
}, lifecycleManager.getState(), stats, muleContext)); |
98 | |
} |
99 | 0 | builder.chain(new ServiceInternalMessageProcessor(this)); |
100 | 0 | if (asyncReplyMessageSource.getEndpoints().size() > 0) |
101 | |
{ |
102 | 0 | builder.chain(createAsyncReplyProcessor()); |
103 | |
} |
104 | 0 | builder.chain(new ServiceOutboundMessageProcessor(this)); |
105 | 0 | builder.chain(new ServiceOutboundStatisticsMessageProcessor(this)); |
106 | 0 | builder.chain(outboundRouter); |
107 | 0 | } |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
@Override |
117 | |
protected synchronized void doInitialise() throws InitialisationException |
118 | |
{ |
119 | 0 | if (threadingProfile == null) |
120 | |
{ |
121 | 0 | threadingProfile = muleContext.getDefaultServiceThreadingProfile(); |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | 0 | ChainedThreadingProfile threadingProfile = new ChainedThreadingProfile(this.threadingProfile); |
127 | 0 | threadingProfile.setMuleContext(muleContext); |
128 | 0 | threadingProfile.setMaxThreadsActive(threadingProfile.getMaxThreadsActive() + 1); |
129 | |
|
130 | |
|
131 | |
|
132 | 0 | final MuleConfiguration config = muleContext.getConfiguration(); |
133 | 0 | final String threadPrefix = ThreadNameHelper.sedaService(muleContext, getName()); |
134 | 0 | workManager = threadingProfile.createWorkManager(threadPrefix, config.getShutdownTimeout()); |
135 | |
|
136 | 0 | if (queueProfile == null && model != null) |
137 | |
{ |
138 | 0 | queueProfile = ((SedaModel) model).getQueueProfile(); |
139 | |
} |
140 | 0 | if (queueTimeout == null && model != null) |
141 | |
{ |
142 | 0 | setQueueTimeout(((SedaModel) model).getQueueTimeout()); |
143 | |
} |
144 | |
|
145 | |
try |
146 | |
{ |
147 | 0 | if (name == null) |
148 | |
{ |
149 | 0 | throw new InitialisationException( |
150 | |
MessageFactory.createStaticMessage("Service has no name to identify it"), this); |
151 | |
} |
152 | |
} |
153 | 0 | catch (Throwable e) |
154 | |
{ |
155 | 0 | throw new InitialisationException(CoreMessages.objectFailedToInitialise("Service Queue"), e, this); |
156 | 0 | } |
157 | 0 | super.doInitialise(); |
158 | 0 | } |
159 | |
|
160 | |
@Override |
161 | |
protected void doStart() throws MuleException |
162 | |
{ |
163 | |
try |
164 | |
{ |
165 | 0 | workManager.start(); |
166 | |
} |
167 | 0 | catch (Exception e) |
168 | |
{ |
169 | 0 | throw new LifecycleException(CoreMessages.failedToStart("Service: " + getName()), e, this); |
170 | 0 | } |
171 | 0 | super.doStart(); |
172 | 0 | } |
173 | |
|
174 | |
@Override |
175 | |
protected void doStop() throws MuleException |
176 | |
{ |
177 | 0 | super.doStop(); |
178 | 0 | workManager.dispose(); |
179 | 0 | } |
180 | |
|
181 | |
@Override |
182 | |
protected void doForceStop() throws MuleException |
183 | |
{ |
184 | 0 | doStop(); |
185 | 0 | } |
186 | |
|
187 | |
@Override |
188 | |
protected void doDispose() |
189 | |
{ |
190 | 0 | super.doDispose(); |
191 | 0 | if (workManager != null) |
192 | |
{ |
193 | 0 | workManager.dispose(); |
194 | |
} |
195 | 0 | } |
196 | |
|
197 | |
@Override |
198 | |
protected ServiceStatistics createStatistics() |
199 | |
{ |
200 | 0 | return new ServiceStatistics(getName(), threadingProfile.getMaxThreadsActive()); |
201 | |
} |
202 | |
|
203 | |
public QueueProfile getQueueProfile() |
204 | |
{ |
205 | 0 | return queueProfile; |
206 | |
} |
207 | |
|
208 | |
public void setQueueProfile(QueueProfile queueProfile) |
209 | |
{ |
210 | 0 | this.queueProfile = queueProfile; |
211 | 0 | } |
212 | |
|
213 | |
public Integer getQueueTimeout() |
214 | |
{ |
215 | 0 | return queueTimeout; |
216 | |
} |
217 | |
|
218 | |
public void setQueueTimeout(Integer queueTimeout) |
219 | |
{ |
220 | 0 | this.queueTimeout = queueTimeout; |
221 | 0 | } |
222 | |
|
223 | |
public ThreadingProfile getThreadingProfile() |
224 | |
{ |
225 | 0 | return threadingProfile; |
226 | |
} |
227 | |
|
228 | |
public void setThreadingProfile(ThreadingProfile threadingProfile) |
229 | |
{ |
230 | 0 | this.threadingProfile = threadingProfile; |
231 | 0 | } |
232 | |
|
233 | |
} |