1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.processor; |
8 | |
|
9 | |
import org.mule.api.MessagingException; |
10 | |
import org.mule.api.MuleEvent; |
11 | |
import org.mule.api.MuleException; |
12 | |
import org.mule.api.config.ThreadingProfile; |
13 | |
import org.mule.api.context.WorkManager; |
14 | |
import org.mule.api.context.WorkManagerSource; |
15 | |
import org.mule.api.lifecycle.Startable; |
16 | |
import org.mule.api.lifecycle.Stoppable; |
17 | |
import org.mule.api.processor.MessageProcessor; |
18 | |
import org.mule.config.i18n.CoreMessages; |
19 | |
import org.mule.work.AbstractMuleEventWork; |
20 | |
import org.mule.work.MuleWorkManager; |
21 | |
|
22 | |
import java.util.Collections; |
23 | |
import java.util.List; |
24 | |
|
25 | |
import org.apache.commons.logging.Log; |
26 | |
import org.apache.commons.logging.LogFactory; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class AsyncDelegateMessageProcessor extends AbstractMessageProcessorOwner |
35 | |
implements MessageProcessor, Startable, Stoppable |
36 | |
{ |
37 | 0 | protected Log logger = LogFactory.getLog(getClass()); |
38 | |
|
39 | |
protected WorkManagerSource workManagerSource; |
40 | 0 | protected boolean doThreading = true; |
41 | |
protected WorkManager workManager; |
42 | |
protected MessageProcessor delegate; |
43 | |
|
44 | |
public AsyncDelegateMessageProcessor(ThreadingProfile threadingProfile, String name, int shutdownTimeout) |
45 | 0 | { |
46 | 0 | this.doThreading = threadingProfile.isDoThreading(); |
47 | 0 | workManager = threadingProfile.createWorkManager(name, shutdownTimeout); |
48 | 0 | workManagerSource = new WorkManagerSource() |
49 | 0 | { |
50 | |
public WorkManager getWorkManager() throws MuleException |
51 | |
{ |
52 | 0 | return workManager; |
53 | |
} |
54 | |
}; |
55 | 0 | } |
56 | |
|
57 | |
public void start() throws MuleException |
58 | |
{ |
59 | 0 | if (workManager != null) |
60 | |
{ |
61 | 0 | workManager.start(); |
62 | |
} |
63 | 0 | super.start(); |
64 | 0 | } |
65 | |
|
66 | |
public void stop() throws MuleException |
67 | |
{ |
68 | 0 | if (workManager != null) |
69 | |
{ |
70 | 0 | workManager.dispose(); |
71 | |
} |
72 | 0 | super.stop(); |
73 | 0 | } |
74 | |
|
75 | |
public MuleEvent process(MuleEvent event) throws MuleException |
76 | |
{ |
77 | |
|
78 | |
|
79 | 0 | if (delegate != null) |
80 | |
{ |
81 | |
try |
82 | |
{ |
83 | 0 | workManagerSource.getWorkManager().scheduleWork(new AsyncMessageProcessorWorker(event), |
84 | |
WorkManager.INDEFINITE, null, new AsyncWorkListener(delegate)); |
85 | |
} |
86 | 0 | catch (Exception e) |
87 | |
{ |
88 | 0 | new MessagingException( |
89 | |
CoreMessages.errorSchedulingMessageProcessorForAsyncInvocation(delegate), event, e); |
90 | 0 | } |
91 | |
} |
92 | 0 | return event; |
93 | |
} |
94 | |
|
95 | |
public void setDelegate(MessageProcessor delegate) |
96 | |
{ |
97 | 0 | this.delegate = delegate; |
98 | 0 | } |
99 | |
|
100 | |
@Override |
101 | |
protected List<MessageProcessor> getOwnedMessageProcessors() |
102 | |
{ |
103 | 0 | return Collections.singletonList(delegate); |
104 | |
} |
105 | |
|
106 | |
class AsyncMessageProcessorWorker extends AbstractMuleEventWork |
107 | |
{ |
108 | |
public AsyncMessageProcessorWorker(MuleEvent event) |
109 | 0 | { |
110 | 0 | super(event); |
111 | 0 | } |
112 | |
|
113 | |
@Override |
114 | |
protected void doRun() |
115 | |
{ |
116 | |
try |
117 | |
{ |
118 | 0 | delegate.process(event); |
119 | |
} |
120 | 0 | catch (MuleException e) |
121 | |
{ |
122 | 0 | event.getFlowConstruct().getExceptionListener().handleException(e, event); |
123 | 0 | } |
124 | 0 | } |
125 | |
} |
126 | |
|
127 | |
} |