KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > jms > queues > synchRequest > Responder


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is lital kasif.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  *
46  * Created on Mar 23, 2005
47  *
48  * Coridan LTD
49  */

50 package sample.jms.queues.synchRequest;
51
52 import java.util.Random JavaDoc;
53
54 import javax.jms.Connection JavaDoc;
55 import javax.jms.JMSException JavaDoc;
56 import javax.jms.Message JavaDoc;
57 import javax.jms.MessageConsumer JavaDoc;
58 import javax.jms.MessageProducer JavaDoc;
59 import javax.jms.Queue JavaDoc;
60 import javax.jms.Session JavaDoc;
61 import javax.jms.TextMessage JavaDoc;
62 import javax.jms.Topic JavaDoc;
63
64
65 import org.mr.api.jms.MantaConnectionFactory;
66 import org.mr.core.util.ExceptionMonitor;
67 import org.mr.core.util.SystemTime;
68
69
70 /*=======================================================================================
71   For instructions on how to run this sample please refer to the file
72   sample\jms\queues\synchRequest\Readme.txt under the MantaRay installation directory.
73 =======================================================================================*/

74
75
76 /**
77  * The Responder is part of the RequestSample. It is set to be a message listener on the Destination
78  * inserted in the QueueRequestSample/TopicRequestSample, and receives message requests up on it.
79  * For any incoming request it sends a suitalble response message.
80  * A suitable response would be a response message holding the same JMSCorellationID as the
81  * request message, which would be sent to the temporary queue that is set as the JMSReplyTo value
82  * of the request message.
83  *
84  * @author lital kasif
85  */

86 public class Responder implements javax.jms.MessageListener JavaDoc{
87     final String JavaDoc QUEUE = "queue";
88     final String JavaDoc TOPIC = "topic";
89     String JavaDoc destType, destName;
90     MantaConnectionFactory factory;
91     Queue JavaDoc queue;
92     Topic JavaDoc topic;
93     Connection JavaDoc prodConn, consConn;
94     Session JavaDoc prodSess, consSess;
95     MessageProducer JavaDoc producer;
96     MessageConsumer JavaDoc consumer;
97     private Random JavaDoc random;
98
99 /**
100  * Builds a Responder. Creates a JMS infrastructure, including connections and sessions.
101  * @param destinationName the name of the destination the Responder should get requests on.
102  * @param destinationType the type of the destination, either a Queue or a Topic.
103  */

104     public Responder(String JavaDoc destinationName, String JavaDoc destinationType){
105         factory = new MantaConnectionFactory();
106         try {
107             if(destinationType.equalsIgnoreCase("queue")){
108                 destType=QUEUE;
109             }else{
110                 if(destinationType.equalsIgnoreCase("topic")){
111                     destType=TOPIC;
112                 }else{
113                     try {
114                         ExceptionMonitor.getInstance().shout(24, "There is no destination type "+destType);
115                     } catch (JMSException JavaDoc e) {
116                         e.printStackTrace();
117                     }
118                 }//else
119
}//else
120
this.destName = destinationName;
121             prodConn = factory.createConnection();
122             prodSess = prodConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
123             consConn = factory.createConnection();
124             consSess = consConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
125             if (destType.equals(QUEUE)){
126                 queue = prodSess.createQueue(destName);
127                 producer = prodSess.createProducer(null);
128                 consumer = consSess.createConsumer(queue);
129             }else{
130                 topic = prodSess.createTopic(destName);
131                 producer = prodSess.createProducer(null);
132                 consumer = consSess.createConsumer(topic);
133             }
134             consumer.setMessageListener(this);
135             prodConn.start();
136             consConn.start();
137
138         } catch (JMSException JavaDoc e) {
139             e.printStackTrace();
140         }
141         random = new Random JavaDoc(SystemTime.currentTimeMillis());
142     }//constractor
143

144     /**
145      * receives a request JMS message, and create a suitable respondes to them.
146      */

147     public void onMessage(Message JavaDoc message)
148     {
149         try {
150             String JavaDoc text;
151             int i= random.nextInt(4);
152             switch (i) {
153                 case 0 : text="I agree.";
154                          break;
155                 case 1 : text="Yes, this is true.";
156                          break;
157                 case 2 : text="Have a nice day.";
158                          break;
159                 case 3 : text="Interesting, "+((TextMessage JavaDoc)message).getText();
160                          break;
161                 default : text ="Yep.";
162             }
163             TextMessage JavaDoc newMessage;
164             newMessage = prodSess.createTextMessage();
165             newMessage.setText(text);
166             newMessage.setJMSCorrelationID(message.getJMSCorrelationID());
167             producer.send(message.getJMSReplyTo(),newMessage);
168         } catch (JMSException JavaDoc e) {
169                 e.printStackTrace();
170         }
171     }//onMessage
172

173     public void close(){
174         try {
175             prodConn.close();
176             consConn.close();
177         } catch (JMSException JavaDoc e) {
178             e.printStackTrace();
179         }
180     }
181 }//Responder
182
Popular Tags