KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > transport > jms > SimpleJMSListener


1 /*
2  * Copyright 2001, 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.transport.jms;
18
19 import org.apache.axis.components.jms.JMSVendorAdapter;
20 import org.apache.axis.components.jms.JMSVendorAdapterFactory;
21 import org.apache.axis.components.logger.LogFactory;
22 import org.apache.axis.server.AxisServer;
23 import org.apache.axis.utils.Messages;
24 import org.apache.axis.utils.Options;
25 import org.apache.commons.logging.Log;
26
27 import javax.jms.BytesMessage JavaDoc;
28 import javax.jms.MessageListener JavaDoc;
29 import java.io.BufferedInputStream JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Properties JavaDoc;
34
35
36 /**
37  * SimpleJMSListener implements the javax.jms.MessageListener interface. Its
38  * basic purpose is listen asynchronously for messages and to pass them off
39  * to SimpleJMSWorker for processing.
40  *
41  * Note: This is a simple JMS listener that does not pool worker threads and
42  * is not otherwise tuned for performance. As such, its intended use is not
43  * for production code, but for demos, debugging, and performance profiling.
44  *
45  * @author Jaime Meritt (jmeritt@sonicsoftware.com)
46  * @author Richard Chung (rchung@sonicsoftware.com)
47  * @author Dave Chappell (chappell@sonicsoftware.com)
48  */

49 public class SimpleJMSListener implements MessageListener JavaDoc
50 {
51     protected static Log log =
52             LogFactory.getLog(SimpleJMSListener.class.getName());
53
54     // Do we use (multiple) threads to process incoming messages?
55
private static boolean doThreads;
56
57     private JMSConnector connector;
58     private JMSEndpoint endpoint;
59     private AxisServer server;
60     private HashMap JavaDoc connectorProps;
61
62     public SimpleJMSListener(HashMap JavaDoc connectorMap, HashMap JavaDoc cfMap,
63                              String JavaDoc destination, String JavaDoc username,
64                              String JavaDoc password, boolean doThreads)
65         throws Exception JavaDoc
66     {
67         SimpleJMSListener.doThreads = doThreads;
68
69         try {
70             // create a JMS connector using the default vendor adapter
71
JMSVendorAdapter adapter = JMSVendorAdapterFactory.getJMSVendorAdapter();
72             connector = JMSConnectorFactory.createServerConnector(connectorMap,
73                                                                   cfMap,
74                                                                   username,
75                                                                   password,
76                                                                   adapter);
77             connectorProps = connectorMap;
78         } catch (Exception JavaDoc e) {
79             log.error(Messages.getMessage("exception00"), e);
80             throw e;
81         }
82
83         // create the appropriate endpoint for the indicated destination
84
endpoint = connector.createEndpoint(destination);
85     }
86
87     // Axis server (shared between instances)
88
private static AxisServer myAxisServer = new AxisServer();
89
90     protected static AxisServer getAxisServer()
91     {
92         return myAxisServer;
93     }
94
95     protected JMSConnector getConnector()
96     {
97         return connector;
98     }
99
100     /**
101      * This method is called asynchronously whenever a message arrives.
102      * @param message
103      */

104     public void onMessage(javax.jms.Message JavaDoc message)
105     {
106         try
107         {
108             // pass off the message to a worker as a BytesMessage
109
SimpleJMSWorker worker = new SimpleJMSWorker(this, (BytesMessage JavaDoc)message);
110
111             // do we allow multi-threaded workers?
112
if (doThreads) {
113                 Thread JavaDoc t = new Thread JavaDoc(worker);
114                 t.start();
115             } else {
116                 worker.run();
117             }
118         }
119         catch(ClassCastException JavaDoc cce)
120         {
121             log.error(Messages.getMessage("exception00"), cce);
122             cce.printStackTrace();
123             return;
124         }
125     }
126
127     public void start()
128         throws Exception JavaDoc
129     {
130         endpoint.registerListener(this, connectorProps);
131         connector.start();
132     }
133
134     public void shutdown()
135         throws Exception JavaDoc
136     {
137         endpoint.unregisterListener(this);
138         connector.stop();
139         connector.shutdown();
140     }
141
142     public static final HashMap JavaDoc createConnectorMap(Options options)
143     {
144         HashMap JavaDoc connectorMap = new HashMap JavaDoc();
145         if (options.isFlagSet('t') > 0)
146         {
147             //queue is default so only setup map if topic domain is required
148
connectorMap.put(JMSConstants.DOMAIN, JMSConstants.DOMAIN_TOPIC);
149         }
150         return connectorMap;
151     }
152
153     public static final HashMap JavaDoc createCFMap(Options options)
154         throws IOException JavaDoc
155     {
156         String JavaDoc cfFile = options.isValueSet('c');
157         if(cfFile == null)
158             return null;
159
160         Properties JavaDoc cfProps = new Properties JavaDoc();
161         cfProps.load(new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(cfFile)));
162         HashMap JavaDoc cfMap = new HashMap JavaDoc(cfProps);
163         return cfMap;
164     }
165
166     public static void main(String JavaDoc[] args) throws Exception JavaDoc
167     {
168         Options options = new Options(args);
169
170         // first check if we should print usage
171
if ((options.isFlagSet('?') > 0) || (options.isFlagSet('h') > 0))
172             printUsage();
173
174         SimpleJMSListener listener = new SimpleJMSListener(createConnectorMap(options),
175                                                            createCFMap(options),
176                                                            options.isValueSet('d'),
177                                                            options.getUser(),
178                                                            options.getPassword(),
179                                                            options.isFlagSet('s') > 0);
180         listener.start();
181     }
182
183     public static void printUsage()
184     {
185         System.out.println("Usage: SimpleJMSListener [options]");
186         System.out.println(" Opts: -? this message");
187         System.out.println();
188         System.out.println(" -c connection factory properties filename");
189         System.out.println(" -d destination");
190         System.out.println(" -t topic [absence of -t indicates queue]");
191         System.out.println();
192         System.out.println(" -u username");
193         System.out.println(" -w password");
194         System.out.println();
195         System.out.println(" -s single-threaded listener");
196         System.out.println(" [absence of option => multithreaded]");
197
198         System.exit(1);
199     }
200 }
201
Popular Tags