|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| package org.codehaus.wadi.group.impl; |
|
18 |
| |
|
19 |
| import java.util.ArrayList; |
|
20 |
| import java.util.Collection; |
|
21 |
| import java.util.IdentityHashMap; |
|
22 |
| import java.util.Iterator; |
|
23 |
| |
|
24 |
| import org.apache.commons.logging.Log; |
|
25 |
| import org.apache.commons.logging.LogFactory; |
|
26 |
| import org.codehaus.wadi.group.Envelope; |
|
27 |
| import org.codehaus.wadi.group.ServiceEndpoint; |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| class BasicEnvelopeDispatcherManager implements EnvelopeDispatcherManager { |
|
34 |
| protected static final Log _messageLog = LogFactory.getLog("org.codehaus.wadi.INBOUND_MESSAGES"); |
|
35 |
| |
|
36 |
| private final AbstractDispatcher _dispatcher; |
|
37 |
| private final Log _log = LogFactory.getLog(getClass()); |
|
38 |
| private final IdentityHashMap _msgDispatchers = new IdentityHashMap(); |
|
39 |
| private final ThreadPool _executor; |
|
40 |
| |
|
41 |
8
| public BasicEnvelopeDispatcherManager(AbstractDispatcher dispatcher, ThreadPool executor) {
|
|
42 |
8
| _dispatcher = dispatcher;
|
|
43 |
8
| _executor = executor;
|
|
44 |
| } |
|
45 |
| |
|
46 |
3
| public void register(ServiceEndpoint msgDispatcher) {
|
|
47 |
3
| synchronized (_msgDispatchers) {
|
|
48 |
3
| _msgDispatchers.put(msgDispatcher, new ServiceEndpointWrapper(msgDispatcher));
|
|
49 |
| } |
|
50 |
| } |
|
51 |
| |
|
52 |
3
| public void unregister(ServiceEndpoint msgDispatcher, int nbAttemp, long delayMillis) {
|
|
53 |
3
| ServiceEndpointWrapper seWrapper;
|
|
54 |
3
| synchronized (_msgDispatchers) {
|
|
55 |
3
| seWrapper = (ServiceEndpointWrapper) _msgDispatchers.remove(msgDispatcher);
|
|
56 |
3
| if (null == seWrapper) {
|
|
57 |
0
| throw new IllegalArgumentException(msgDispatcher + " is unknown.");
|
|
58 |
| } |
|
59 |
| } |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
3
| for (int i= nbAttemp; seWrapper.getNumberOfCurrentDispatch()>0 && i > 0; i--) {
|
|
64 |
0
| try {
|
|
65 |
0
| Thread.sleep(delayMillis);
|
|
66 |
| } catch (InterruptedException e) { |
|
67 |
| |
|
68 |
| } |
|
69 |
| } |
|
70 |
| } |
|
71 |
| |
|
72 |
3
| public void onEnvelope(Envelope message) {
|
|
73 |
3
| Collection targetDispatchers;
|
|
74 |
3
| synchronized (_msgDispatchers) {
|
|
75 |
3
| targetDispatchers = new ArrayList(_msgDispatchers.values());
|
|
76 |
| } |
|
77 |
| |
|
78 |
3
| if (targetDispatchers.size() == 0) {
|
|
79 |
0
| if (_log.isWarnEnabled()) {
|
|
80 |
0
| _log.warn("spurious message received: " + message);
|
|
81 |
| } |
|
82 |
0
| return;
|
|
83 |
| } |
|
84 |
| |
|
85 |
3
| try {
|
|
86 |
3
| for (Iterator iter = targetDispatchers.iterator(); iter.hasNext();) {
|
|
87 |
3
| ServiceEndpointWrapper dispatcher = (ServiceEndpointWrapper) iter.next();
|
|
88 |
3
| boolean dispatchMessage = dispatcher.testDispatchEnvelope(message);
|
|
89 |
3
| if (dispatchMessage) {
|
|
90 |
3
| if (_messageLog.isTraceEnabled()) {
|
|
91 |
0
| _messageLog.trace(message +
|
|
92 |
| " {"+ message.getReplyTo() + |
|
93 |
| "->"+ message.getAddress() + |
|
94 |
| "} - " + |
|
95 |
| message.getTargetCorrelationId() + |
|
96 |
| "/" + |
|
97 |
| message.getSourceCorrelationId()); |
|
98 |
| } |
|
99 |
3
| dispatcher.beforeDispatch();
|
|
100 |
3
| _executor.execute(new DispatchRunner(dispatcher, message));
|
|
101 |
3
| break;
|
|
102 |
| } |
|
103 |
| } |
|
104 |
| } catch (InterruptedException e) { |
|
105 |
0
| _log.warn("bad message", e);
|
|
106 |
| } |
|
107 |
| } |
|
108 |
| |
|
109 |
| class DispatchRunner implements Runnable { |
|
110 |
| protected final ServiceEndpointWrapper _msgDispatcher; |
|
111 |
| protected final Envelope _message; |
|
112 |
| |
|
113 |
3
| public DispatchRunner(ServiceEndpointWrapper msgDispatcher, Envelope message) {
|
|
114 |
3
| _msgDispatcher=msgDispatcher;
|
|
115 |
3
| _message=message;
|
|
116 |
| } |
|
117 |
| |
|
118 |
3
| public void run() {
|
|
119 |
3
| try {
|
|
120 |
3
| _dispatcher.hook();
|
|
121 |
3
| _msgDispatcher.dispatch(_message);
|
|
122 |
3
| _msgDispatcher.afterDispatch();
|
|
123 |
| } catch (Exception e) { |
|
124 |
0
| _log.error("problem dispatching message", e);
|
|
125 |
| } |
|
126 |
| } |
|
127 |
| } |
|
128 |
| |
|
129 |
| } |