KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > jms > topics > 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.topics.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 import org.mr.api.jms.MantaConnectionFactory;
65 import org.mr.core.util.ExceptionMonitor;
66 import org.mr.core.util.SystemTime;
67
68 /**
69  * The Responder is part of the RequestSample. It is set to be a message listener on the Destination
70  * inserted in the QueueRequestSample/TopicRequestSample, and receives message requests up on it.
71  * For any incoming request it sends a suitalble response message.
72  * A suitable response would be a response message holding the same JMSCorellationID as the
73  * request message, which would be sent to the temporary queue that is set as the JMSReplyTo value
74  * of the request message.
75  *
76  * @author lital kasif
77  */

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

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

136     /**
137      * receives a request JMS message, and create a suitable respondes to them.
138      */

139     public void onMessage(Message JavaDoc message)
140     {
141         try {
142             String JavaDoc text;
143             int i= random.nextInt(4);
144             switch (i) {
145                 case 0 : text="i agree.";
146                          break;
147                 case 1 : text="yes, this is true.";
148                          break;
149                 case 2 : text="have a nice day.";
150                          break;
151                 case 3 : text="interesting, "+((TextMessage JavaDoc)message).getText();
152                          break;
153                 default : text ="yep.";
154             }
155             TextMessage JavaDoc newMessage;
156             newMessage = prodSess.createTextMessage();
157             newMessage.setText(text);
158             newMessage.setJMSCorrelationID(message.getJMSCorrelationID());
159             producer.send(message.getJMSReplyTo(),newMessage);
160         } catch (JMSException JavaDoc e) {
161                 e.printStackTrace();
162         }
163     }//onMessage
164

165     public void close(){
166         try {
167             prodConn.close();
168             consConn.close();
169         } catch (JMSException JavaDoc e) {
170             // TODO Auto-generated catch block
171
e.printStackTrace();
172         }
173     }
174
175 }//Responder
176
Popular Tags