KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001, 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.transport.jms;
57
58 import org.jboss.axis.server.AxisServer;
59 import org.jboss.axis.utils.Messages;
60 import org.jboss.axis.utils.Options;
61 import org.jboss.logging.Logger;
62
63 import javax.jms.BytesMessage JavaDoc;
64 import javax.jms.MessageListener JavaDoc;
65 import java.io.BufferedInputStream JavaDoc;
66 import java.io.FileInputStream JavaDoc;
67 import java.io.IOException JavaDoc;
68 import java.util.HashMap JavaDoc;
69 import java.util.Properties JavaDoc;
70
71
72 /**
73  * SimpleJMSListener implements the javax.jms.MessageListener interface. Its
74  * basic purpose is listen asynchronously for messages and to pass them off
75  * to SimpleJMSWorker for processing.
76  *
77  * Note: This is a simple JMS listener that does not pool worker threads and
78  * is not otherwise tuned for performance. As such, its intended use is not
79  * for production code, but for demos, debugging, and performance profiling.
80  *
81  * @author Jaime Meritt (jmeritt@sonicsoftware.com)
82  * @author Richard Chung (rchung@sonicsoftware.com)
83  * @author Dave Chappell (chappell@sonicsoftware.com)
84  */

85 public class SimpleJMSListener implements MessageListener JavaDoc
86 {
87    protected static Logger log = Logger.getLogger(SimpleJMSListener.class);
88
89    // Do we use (multiple) threads to process incoming messages?
90
private static boolean doThreads;
91
92    private JMSConnector connector;
93    private JMSEndpoint endpoint;
94    private AxisServer server;
95
96    public SimpleJMSListener(HashMap JavaDoc connectorMap, HashMap JavaDoc cfMap,
97                             String JavaDoc destination, String JavaDoc username,
98                             String JavaDoc password, boolean doThreads)
99            throws Exception JavaDoc
100    {
101       this.doThreads = doThreads;
102
103       try
104       {
105          connector = JMSConnectorFactory.
106                  createServerConnector(connectorMap,
107                          cfMap,
108                          username,
109                          password);
110       }
111       catch (Exception JavaDoc e)
112       {
113          log.error(Messages.getMessage("exception00"), e);
114          throw e;
115       }
116
117       // create the appropriate endpoint for the indicated destination
118
endpoint = connector.createEndpoint(destination);
119    }
120
121    // Axis server (shared between instances)
122
private static AxisServer myAxisServer = new AxisServer();
123
124    protected static AxisServer getAxisServer()
125    {
126       return myAxisServer;
127    }
128
129    protected JMSConnector getConnector()
130    {
131       return connector;
132    }
133
134    /**
135     * This method is called asynchronously whenever a message arrives.
136     * @param message
137     */

138    public void onMessage(javax.jms.Message JavaDoc message)
139    {
140       try
141       {
142          // pass off the message to a worker as a BytesMessage
143
SimpleJMSWorker worker = new SimpleJMSWorker(this, (BytesMessage JavaDoc)message);
144
145          //should pool
146

147          // do we allow multi-threaded workers?
148
if (doThreads)
149          {
150             Thread JavaDoc t = new Thread JavaDoc(worker);
151             t.start();
152          }
153          else
154          {
155             worker.run();
156          }
157       }
158       catch (ClassCastException JavaDoc cce)
159       {
160          log.error(Messages.getMessage("exception00"), cce);
161          cce.printStackTrace();
162          return;
163       }
164    }
165
166    public void start()
167            throws Exception JavaDoc
168    {
169       endpoint.registerListener(this);
170       connector.start();
171    }
172
173    public void shutdown()
174            throws Exception JavaDoc
175    {
176       endpoint.unregisterListener(this);
177       connector.stop();
178       connector.shutdown();
179    }
180
181    public static final HashMap JavaDoc createConnectorMap(Options options)
182    {
183       HashMap JavaDoc connectorMap = new HashMap JavaDoc();
184       if (options.isFlagSet('t') > 0)
185       {
186          //queue is default so only setup map if topic domain is required
187
connectorMap.put(JMSConstants.DOMAIN, JMSConstants.DOMAIN_TOPIC);
188       }
189       return connectorMap;
190    }
191
192    public static final HashMap JavaDoc createCFMap(Options options)
193            throws IOException JavaDoc
194    {
195       String JavaDoc cfFile = options.isValueSet('c');
196       if (cfFile == null)
197          return null;
198
199       Properties JavaDoc cfProps = new Properties JavaDoc();
200       cfProps.load(new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(cfFile)));
201       HashMap JavaDoc cfMap = new HashMap JavaDoc(cfProps);
202       return cfMap;
203    }
204
205    public static void main(String JavaDoc[] args) throws Exception JavaDoc
206    {
207       Options options = new Options(args);
208
209       // first check if we should print usage
210
if ((options.isFlagSet('?') > 0) || (options.isFlagSet('h') > 0))
211          printUsage();
212
213       SimpleJMSListener listener = new SimpleJMSListener(createConnectorMap(options),
214               createCFMap(options),
215               options.isValueSet('d'),
216               options.getUser(),
217               options.getPassword(),
218               options.isFlagSet('s') > 0);
219    }
220
221    public static void printUsage()
222    {
223       System.out.println("Usage: SimpleJMSListener [options]");
224       System.out.println(" Opts: -? this message");
225       System.out.println();
226       System.out.println(" -c connection factory properties filename");
227       System.out.println(" -d destination");
228       System.out.println(" -t topic [absence of -t indicates queue]");
229       System.out.println();
230       System.out.println(" -u username");
231       System.out.println(" -w password");
232       System.out.println();
233       System.out.println(" -s single-threaded listener");
234       System.out.println(" [absence of option => multithreaded]");
235
236       System.exit(1);
237    }
238 }
239
Popular Tags