1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.apache.commons.messagelet; |
11 | |
|
12 | |
import java.util.Iterator; |
13 | |
|
14 | |
import javax.jms.Destination; |
15 | |
import javax.jms.JMSException; |
16 | |
import javax.jms.MessageListener; |
17 | |
import javax.servlet.ServletContext; |
18 | |
import javax.servlet.ServletException; |
19 | |
|
20 | |
import org.apache.commons.logging.Log; |
21 | |
import org.apache.commons.logging.LogFactory; |
22 | |
import org.apache.commons.messagelet.model.Subscription; |
23 | |
import org.apache.commons.messagelet.model.SubscriptionList; |
24 | |
import org.apache.commons.messenger.Messenger; |
25 | |
import org.apache.commons.messenger.MessengerManager; |
26 | |
import org.apache.commons.messenger.tool.StopWatchMessageListener; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class SubscriptionManager { |
37 | |
|
38 | |
|
39 | 0 | private static final Log log = LogFactory.getLog(SubscriptionManager.class); |
40 | |
|
41 | |
|
42 | |
private MessengerManager manager; |
43 | |
|
44 | |
|
45 | |
private SubscriptionList subscriptionList; |
46 | |
|
47 | |
|
48 | |
private ServletContext servletContext; |
49 | |
|
50 | |
|
51 | 0 | private boolean useStopWatch = false; |
52 | |
|
53 | |
|
54 | 0 | public SubscriptionManager() { |
55 | 0 | } |
56 | |
|
57 | |
|
58 | |
protected void subscribe() throws JMSException, ServletException { |
59 | 0 | for (Iterator iter = getSubscriptionList().getSubscriptions().iterator(); iter.hasNext(); ) { |
60 | 0 | Subscription subscription = (Subscription) iter.next(); |
61 | 0 | subscribe( subscription ); |
62 | 0 | } |
63 | 0 | } |
64 | |
|
65 | |
public void subscribe( Subscription subscription ) throws JMSException, ServletException{ |
66 | 0 | String name = subscription.getConnection(); |
67 | 0 | Messenger messenger = getMessenger( name ); |
68 | 0 | if ( messenger == null ) { |
69 | 0 | throw new JMSException( "No such Messenger called: " + name + " for subscription: " + subscription ); |
70 | |
} |
71 | 0 | String subject = subscription.getSubject(); |
72 | 0 | if ( subject == null || subject.length() == 0 ) { |
73 | 0 | throw new JMSException( "No destination defined for subscription: " + subscription ); |
74 | |
} |
75 | |
|
76 | 0 | Destination destination = messenger.getDestination( subject ); |
77 | 0 | if ( destination == null ) { |
78 | 0 | throw new JMSException( "No destination could be found for name: " + subject + " for subscription: " + subscription ); |
79 | |
} |
80 | |
|
81 | 0 | MessageListener listener = subscription.getMessageListener(); |
82 | 0 | if ( listener == null ) { |
83 | 0 | throw new JMSException( "No MessageListener is defined for subscription: " + subscription ); |
84 | |
} |
85 | |
|
86 | |
|
87 | 0 | if ( listener instanceof MessageDrivenObject ) { |
88 | 0 | MessageDrivenObject mdo = (MessageDrivenObject) listener; |
89 | 0 | if ( mdo instanceof MessengerMDO ) { |
90 | 0 | MessengerMDO messengerMDO = (MessengerMDO) mdo; |
91 | 0 | messengerMDO.setMessenger( messenger ); |
92 | 0 | messengerMDO.setMessengerManager( getMessengerManager() ); |
93 | |
} |
94 | 0 | mdo.init( getServletContext() ); |
95 | |
} |
96 | |
|
97 | 0 | listener = wrapInStopWatch( listener ); |
98 | |
|
99 | 0 | String selector = subscription.getSelector(); |
100 | |
|
101 | 0 | ConsumerThread thread = subscription.getConsumerThread(); |
102 | 0 | if (thread != null) { |
103 | 0 | log.info( "Subscribing to messenger: " + name + " destination: " + subject + " selector: " + selector + " with: " + thread ); |
104 | |
|
105 | 0 | thread.setMessenger(messenger); |
106 | 0 | thread.setDestination(destination); |
107 | 0 | thread.setSelector(selector); |
108 | 0 | thread.setListener(listener); |
109 | 0 | thread.start(); |
110 | |
} |
111 | |
else { |
112 | 0 | if ( selector != null && selector.length() > 0 ) { |
113 | 0 | log.info( "Subscribing to messenger: " + name + " destination: " + subject + " selector: " + selector ); |
114 | |
|
115 | 0 | messenger.addListener( destination, selector, listener ); |
116 | |
} |
117 | |
else { |
118 | 0 | log.info( "Subscribing to messenger: " + name + " destination: " + subject ); |
119 | |
|
120 | 0 | messenger.addListener( destination, listener ); |
121 | |
} |
122 | |
|
123 | 0 | log.info( "Subscribed with listener: " + listener ); |
124 | |
} |
125 | 0 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
public void unsubscribe() throws JMSException, ServletException { |
130 | 0 | SubscriptionList list = getSubscriptionList(); |
131 | 0 | if ( list != null ) { |
132 | 0 | for (Iterator iter = list.getSubscriptions().iterator(); iter.hasNext(); ) { |
133 | 0 | Subscription subscription = (Subscription) iter.next(); |
134 | 0 | unsubscribe( subscription ); |
135 | 0 | } |
136 | |
} |
137 | 0 | } |
138 | |
|
139 | |
public void unsubscribe( Subscription subscription ) throws JMSException, ServletException { |
140 | |
|
141 | 0 | String name = subscription.getConnection(); |
142 | 0 | Messenger messenger = getMessenger( name ); |
143 | |
|
144 | 0 | MessageListener listener = subscription.getMessageListener(); |
145 | 0 | if ( messenger != null && listener != null ) { |
146 | 0 | Destination destination = null; |
147 | 0 | String subject = subscription.getSubject(); |
148 | 0 | if ( subject == null || subject.length() == 0 ) { |
149 | 0 | log.error( "No destination defined for subscription: " + subscription ); |
150 | |
} |
151 | |
else { |
152 | |
try { |
153 | 0 | destination = messenger.getDestination( subject ); |
154 | 0 | if ( destination == null ) { |
155 | 0 | log.error( "No destination could be found for name: " + subject + " for subscription: " + subscription ); |
156 | |
} |
157 | |
} |
158 | 0 | catch (JMSException e) { |
159 | 0 | log.error( "Could not create destination for name: " + subject + " for subscription: " + subscription, e ); |
160 | 0 | } |
161 | |
} |
162 | 0 | if ( destination != null ) { |
163 | |
try { |
164 | 0 | String selector = subscription.getSelector(); |
165 | 0 | if ( selector != null && selector.length() > 0 ) { |
166 | 0 | messenger.removeListener( destination, selector, listener ); |
167 | |
} |
168 | |
else { |
169 | 0 | messenger.removeListener( destination, listener ); |
170 | |
} |
171 | |
} |
172 | 0 | catch (JMSException e) { |
173 | 0 | log.error( "Could not unsubscribe to destination:" + destination + " for subscription: " + subscription, e ); |
174 | 0 | } |
175 | |
} |
176 | |
} |
177 | |
|
178 | |
|
179 | 0 | if ( listener instanceof MessageDrivenObject ) { |
180 | 0 | MessageDrivenObject mdo = (MessageDrivenObject) listener; |
181 | 0 | mdo.destroy(); |
182 | |
} |
183 | 0 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public MessengerManager getMessengerManager() throws JMSException { |
190 | 0 | return manager; |
191 | |
} |
192 | |
|
193 | |
public void setMessengerManager(MessengerManager manager) { |
194 | 0 | this.manager = manager; |
195 | 0 | } |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
public SubscriptionList getSubscriptionList() { |
202 | 0 | return subscriptionList; |
203 | |
} |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
public void setSubscriptionList(SubscriptionList subscriptionList) { |
210 | 0 | this.subscriptionList = subscriptionList; |
211 | 0 | } |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
public ServletContext getServletContext() { |
218 | 0 | return servletContext; |
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public void setServletContext(ServletContext servletContext) { |
226 | 0 | this.servletContext = servletContext; |
227 | 0 | } |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
protected MessageListener wrapInStopWatch( MessageListener listener ) { |
235 | 0 | if ( useStopWatch ) { |
236 | 0 | return new StopWatchMessageListener( listener ); |
237 | |
} |
238 | 0 | return listener; |
239 | |
} |
240 | |
|
241 | |
protected Messenger getMessenger(String name) throws JMSException { |
242 | 0 | return getMessengerManager().getMessenger( name ); |
243 | |
} |
244 | |
|
245 | |
} |