KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > util > jms > JMS2HTTPBridgeDestination


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 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 "WSIF" 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 and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.wsif.util.jms;
59
60 import java.util.ArrayList JavaDoc;
61 import java.util.Arrays JavaDoc;
62
63 import javax.jms.JMSException JavaDoc;
64 import javax.jms.Message JavaDoc;
65 import javax.jms.Queue JavaDoc;
66 import javax.jms.QueueReceiver JavaDoc;
67 import org.apache.wsif.WSIFException;
68 import org.apache.wsif.logging.Trc;
69
70 /**
71  * Server destination for the JMS2HTTPBridge
72  * @author Mark Whitlock <whitlock@apache.org>
73  */

74 public class JMS2HTTPBridgeDestination extends WSIFJMSDestination {
75     public static final String JavaDoc COLDSTART = "cold";
76     public static final String JavaDoc WARMSTART = "warm";
77
78     private boolean verbose;
79     private static final ArrayList JavaDoc allStarts =
80         new ArrayList JavaDoc(Arrays.asList(new Object JavaDoc[] { COLDSTART, WARMSTART }));
81
82     /**
83      * Public constructor.
84      * @param finder used to find JMS objects.
85      * @param altdestName is an alterative JMS provider destination name
86      * @param timeout is the maximum time to wait on a synchronous receive
87      * @param startType is WARMSTART or COLDSTART. Cold means wipe the read queue on startup.
88      */

89     public JMS2HTTPBridgeDestination(
90         WSIFJMSFinder finder,
91         String JavaDoc altDestName,
92         long timeout,
93         String JavaDoc startType,
94         boolean verbose)
95         throws WSIFException {
96         super(finder, altDestName, timeout);
97         Trc.entry(
98             this,
99             finder,
100             altDestName,
101             new Long JavaDoc(timeout),
102             startType,
103             new Boolean JavaDoc(verbose));
104
105         this.verbose = verbose;
106         
107         // Swap the queues because we're a server not a client.
108
readQ = writeQ;
109         writeQ = null;
110
111         if (!allStarts.contains(startType))
112             throw new WSIFException("StartType must either be warm or cold");
113
114         if (COLDSTART.equals(startType)) {
115             if (verbose)
116                 System.out.println("Wiping messages off the read queue");
117             Message JavaDoc msg = null;
118             try {
119                 QueueReceiver JavaDoc rec = session.createReceiver(readQ);
120                 for (;;) {
121                     msg = rec.receive(100);
122                     if (msg != null) {
123                         if (verbose)
124                             System.out.println("Removing an input message");
125                     } else
126                         break;
127                 }
128             } catch (Exception JavaDoc ignored) {
129                 Trc.exception(ignored);
130             }
131         }
132         Trc.exit();
133     }
134
135     /**
136      * Create a listener thread to listen for messages. This waits forever until it gets
137      * an InterruptedException. This listens on the read queue.
138      * @param listener is the JMS message and exception callback interface implementation
139      */

140     public void listen(WSIFJMSListener listener) throws WSIFException {
141         Trc.entry(this, listener);
142         listen(listener, readQ);
143         Trc.exit();
144     }
145
146     /**
147      * Create a listener thread to listen for messages. This waits forever until it gets
148      * an InterruptedException.
149      * @param listener is the JMS message and exception callback interface implementation
150      * @param queue to listen on
151      */

152     public void listen(WSIFJMSListener listener, Queue JavaDoc queue)
153         throws WSIFException {
154         Trc.entry(this, listener, queue);
155         areWeClosed();
156
157         try {
158             QueueReceiver JavaDoc qr = session.createReceiver(queue);
159             qr.setMessageListener(listener);
160             connection.setExceptionListener(listener);
161
162             connection.start();
163
164             for (int i = 1; !Thread.interrupted(); i++) {
165                 Thread.yield();
166                 Thread.sleep(5000);
167                 if (verbose)
168                     System.out.println("Waiting... " + i);
169             }
170         } catch (JMSException JavaDoc je) {
171             Trc.exception(je);
172             throw WSIFJMSConstants.ToWsifException(je);
173         } catch (InterruptedException JavaDoc ignored) {
174             Trc.exception(ignored);
175             System.out.println("Exitting");
176         }
177         Trc.exit();
178     }
179
180     // Stop anyone overwriting our readQ
181
public void setReplyToQueue() throws WSIFException {
182         Trc.entry(this);
183         Trc.exit();
184     }
185
186     public void setReplyToQueue(String JavaDoc replyTo) throws WSIFException {
187         Trc.entry(this, replyTo);
188         Trc.exit();
189     }
190
191     /**
192      * Set the replyTo queue. Special bridge version.
193      * @param replyTo queue.
194      */

195     public void setReplyToQueue(Queue JavaDoc replyTo) throws WSIFException {
196         Trc.entry(this, replyTo);
197         areWeClosed();
198
199         if (writeQ == null)
200             writeQ = replyTo;
201         else if (!writeQ.equals(replyTo)) {
202
203             if (sender != null) {
204                 try {
205                     sender.close();
206                 } catch (Exception JavaDoc e) {
207                     Trc.exception(e);
208                 }
209                 sender = null;
210             }
211             writeQ = replyTo;
212         }
213         Trc.exit();
214     }
215 }
Popular Tags