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.JMSException; |
15 | |
import javax.servlet.ServletContext; |
16 | |
|
17 | |
import org.apache.commons.logging.Log; |
18 | |
import org.apache.commons.logging.LogFactory; |
19 | |
import org.apache.commons.messagelet.model.SubscriptionDigester; |
20 | |
import org.apache.commons.messagelet.model.SubscriptionList; |
21 | |
import org.apache.commons.messenger.Messenger; |
22 | |
import org.apache.commons.messenger.MessengerManager; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class Main { |
33 | |
|
34 | |
|
35 | 0 | private static final Log log = LogFactory.getLog(Main.class); |
36 | |
|
37 | |
|
38 | |
private MessengerManager manager; |
39 | |
|
40 | |
|
41 | |
private SubscriptionList subscriptionList; |
42 | |
|
43 | |
|
44 | 0 | private String connectionsConfig = "Messenger.xml"; |
45 | |
|
46 | |
|
47 | 0 | private String subscriptionsConfig = "subscriptions.xml"; |
48 | |
|
49 | |
|
50 | 0 | private boolean useStopWatch = false; |
51 | |
|
52 | |
|
53 | |
public static void main(String[] args) { |
54 | 0 | Main main = new Main(); |
55 | 0 | if ( args.length <= 0 ) { |
56 | 0 | System.out.println( "Usage <subscriptionConfigFile> [<connectionsConfigFile>]" ); |
57 | 0 | return; |
58 | |
} |
59 | 0 | if ( args.length > 0 ) { |
60 | 0 | main.setSubscriptionsConfig( args[0] ); |
61 | |
} |
62 | 0 | if ( args.length > 1 ) { |
63 | 0 | main.setConnectionsConfig( args[1] ); |
64 | |
} |
65 | |
|
66 | |
try { |
67 | 0 | main.run(); |
68 | |
} |
69 | 0 | catch (Exception e) { |
70 | 0 | log.error( "Caught: " + e, e ); |
71 | 0 | } |
72 | 0 | } |
73 | |
|
74 | 0 | public Main() { |
75 | 0 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public void run() throws Exception { |
83 | |
|
84 | |
|
85 | 0 | SubscriptionManager subscriber = new SubscriptionManager(); |
86 | 0 | subscriber.setMessengerManager( getMessengerManager() ); |
87 | 0 | subscriber.setSubscriptionList( createSubscriptionList() ); |
88 | 0 | subscriber.setServletContext( getServletContext() ); |
89 | |
|
90 | 0 | subscriber.subscribe(); |
91 | |
|
92 | |
|
93 | 0 | for (Iterator iter = manager.getMessengerNames(); iter.hasNext(); ) { |
94 | 0 | String name = (String) iter.next(); |
95 | 0 | Messenger messenger = manager.getMessenger( name ); |
96 | |
try { |
97 | 0 | messenger.getConnection().start(); |
98 | |
} |
99 | 0 | catch (JMSException e) { |
100 | 0 | log.error( "Caught exception trying to start messenger: " + name + ". Exception: " + e, e ); |
101 | 0 | } |
102 | 0 | } |
103 | |
|
104 | |
|
105 | 0 | } |
106 | |
|
107 | |
|
108 | |
public Messenger getMessenger(String name) throws JMSException { |
109 | 0 | return getMessengerManager().getMessenger( name ); |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public String getConnectionsConfig() { |
117 | 0 | return connectionsConfig; |
118 | |
} |
119 | |
|
120 | |
public void setConnectionsConfig(String connectionsConfig) { |
121 | 0 | this.connectionsConfig = connectionsConfig; |
122 | 0 | } |
123 | |
|
124 | |
public String getSubscriptionsConfig() { |
125 | 0 | return subscriptionsConfig; |
126 | |
} |
127 | |
|
128 | |
public void setSubscriptionsConfig(String subscriptionsConfig) { |
129 | 0 | this.subscriptionsConfig = subscriptionsConfig; |
130 | 0 | } |
131 | |
|
132 | |
public MessengerManager getMessengerManager() throws JMSException { |
133 | 0 | if ( manager == null ) { |
134 | 0 | manager = createMessengerManager(); |
135 | 0 | MessengerManager.setInstance( manager ); |
136 | |
} |
137 | 0 | return manager; |
138 | |
} |
139 | |
|
140 | |
public void setMessengerManager(MessengerManager manager) { |
141 | 0 | this.manager = manager; |
142 | 0 | } |
143 | |
|
144 | |
|
145 | |
|
146 | |
protected MessengerManager createMessengerManager() throws JMSException { |
147 | 0 | String config = connectionsConfig; |
148 | |
|
149 | 0 | log.info( "Creating the JMS connections from the file: " + config ); |
150 | |
|
151 | |
try { |
152 | 0 | return MessengerManager.load( config ); |
153 | |
} |
154 | 0 | catch (JMSException e) { |
155 | 0 | log.error( "Could not parse Messenger connection XML deployment document for URL: " + config, e ); |
156 | |
|
157 | 0 | throw new JMSException( |
158 | |
"Could not parse Messenger connection XML deployment document for URL: " + config |
159 | |
+ " reason: " + e |
160 | |
); |
161 | |
} |
162 | |
} |
163 | |
|
164 | |
protected SubscriptionList createSubscriptionList() throws JMSException { |
165 | 0 | String config = subscriptionsConfig; |
166 | |
|
167 | 0 | log.info( "Loading the JMS subscriptions from: " + config ); |
168 | |
|
169 | |
try { |
170 | 0 | SubscriptionDigester digester = new SubscriptionDigester(); |
171 | 0 | return (SubscriptionList) digester.parse( config ); |
172 | |
} |
173 | 0 | catch (Exception e) { |
174 | 0 | log.error( "Could not parse Messenger subscription XML deployment document for URL: " + config, e ); |
175 | |
|
176 | 0 | throw new JMSException( |
177 | |
"Could not parse Messenger subscription XML deployment document for URL: " + config |
178 | |
+ " reason: " + e |
179 | |
); |
180 | |
} |
181 | |
} |
182 | |
|
183 | |
protected ServletContext getServletContext() { |
184 | 0 | return null; |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
protected void waitForever() { |
191 | 0 | Object lock = new Object(); |
192 | 0 | synchronized (lock) { |
193 | |
while (true) { |
194 | |
try { |
195 | 0 | lock.wait(); |
196 | |
} |
197 | 0 | catch (Exception e) { |
198 | 0 | log.warn( "Main thread interupted: " + e, e ); |
199 | 0 | } |
200 | |
} |
201 | 0 | } |
202 | |
} |
203 | |
} |