KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > jms > queues > requestReply > QueueReply


1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License as
4  * published by the Free Software Foundation; either version 2 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
15  * USA
16  *
17  * Created on Aug 11, 2005
18  *
19  */

20 package sample.jms.queues.requestReply;
21
22 import java.util.Random JavaDoc;
23
24 import javax.jms.Connection JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.Message JavaDoc;
27 import javax.jms.MessageConsumer JavaDoc;
28 import javax.jms.MessageProducer JavaDoc;
29 import javax.jms.Queue JavaDoc;
30 import javax.jms.Session JavaDoc;
31 import javax.jms.TextMessage JavaDoc;
32 import javax.jms.Topic JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.mr.api.jms.MantaConnectionFactory;
37 import org.mr.core.util.ExceptionMonitor;
38 import org.mr.core.util.SystemTime;
39
40 /**
41  * The Responder is part of the QueueReply. It is set to be a message listener on the Destination
42  * inserted in the QueueRequest, and receives message requests up on it.
43  * For any incoming request it sends a suitalble response message.
44  * A suitable response would be a response message holding the same JMSCorellationID as the
45  * request message, which would be sent to the temporary queue that is set as the JMSReplyTo value
46  * of the request message.
47  *
48  * @author Nambi Sankaran
49  */

50 public class QueueReply implements javax.jms.MessageListener JavaDoc{
51     final String JavaDoc QUEUE = "queue";
52     final String JavaDoc TOPIC = "topic";
53     String JavaDoc destType, destName;
54     MantaConnectionFactory factory;
55     Queue JavaDoc queue;
56     Topic JavaDoc topic;
57     Connection JavaDoc prodConn, consConn;
58     Session JavaDoc prodSess, consSess;
59     MessageProducer JavaDoc producer;
60     MessageConsumer JavaDoc consumer;
61     private static Log log;
62     private Random JavaDoc random;
63     
64 /**
65  * Builds a Responder. Creates a JMS infrastructure, including connections and sessions.
66  * @param destinationName the name of the destination the Responder should get requests on.
67  * @param destinationType the type of the destination, either a Queue or a Topic.
68  */

69     public QueueReply(String JavaDoc destinationName, String JavaDoc destinationType){
70         factory = new MantaConnectionFactory();
71         try {
72             if(destinationType.equalsIgnoreCase("queue")){
73                 destType=QUEUE;
74             }else{
75                 if(destinationType.equalsIgnoreCase("topic")){
76                     destType=TOPIC;
77                 }else{
78                     try {
79                         ExceptionMonitor.getInstance().shout(24, "There is no destination type "+destType);
80                     } catch (JMSException JavaDoc e) {
81                         e.printStackTrace();
82                     }
83                 }//else
84
}//else
85
this.destName = destinationName;
86             prodConn = factory.createConnection();
87             prodSess = prodConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
88             consConn = factory.createConnection();
89             consSess = consConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
90             if (destType.equals(QUEUE)){
91                 queue = prodSess.createQueue(destName);
92                 producer = prodSess.createProducer(null);
93                 consumer = consSess.createConsumer(queue);
94             }else{
95                 topic = prodSess.createTopic(destName);
96                 producer = prodSess.createProducer(null);
97                 consumer = consSess.createConsumer(topic);
98             }
99             consumer.setMessageListener(this);
100             prodConn.start();
101             consConn.start();
102
103         } catch (JMSException JavaDoc e) {
104             e.printStackTrace();
105         }
106         random = new Random JavaDoc(SystemTime.currentTimeMillis());
107     }//constractor
108

109     /**
110      * receives a request JMS message, and create a suitable respondes to them.
111      */

112     public void onMessage(Message JavaDoc message)
113     {
114         try {
115             String JavaDoc text;
116             int i= random.nextInt(4);
117             switch (i) {
118                 case 0 : text="i agree.";
119                          break;
120                 case 1 : text="yes, this is true.";
121                          break;
122                 case 2 : text="have a nice day.";
123                          break;
124                 case 3 : text="interesting, "+((TextMessage JavaDoc)message).getText();
125                          break;
126                 default : text ="yep.";
127             }
128             TextMessage JavaDoc newMessage;
129             newMessage = prodSess.createTextMessage();
130             newMessage.setText(text);
131             newMessage.setJMSCorrelationID(message.getJMSCorrelationID());
132             producer.send(message.getJMSReplyTo(),newMessage);
133         } catch (JMSException JavaDoc e) {
134                 e.printStackTrace();
135         }
136     }//onMessage
137

138     public static void main(String JavaDoc args[])
139     {
140
141         if( args.length != 1 )
142         {
143             printUsage();
144             System.exit(0);
145         }
146
147         // Qname read from command line parameters
148
String JavaDoc Qname = args[0];
149
150         if( Qname == null )
151         {
152             printUsage();
153             System.exit(0);
154         }
155         
156         QueueReply replier = new QueueReply(Qname, "queue");
157     }
158
159     /** prints the usage. */
160     private static void printUsage()
161     {
162         System.err.println("usage: QueueReply \nSpecify the name of queue to receive requests.\n");
163     }
164
165 }//QueueReply
166
Popular Tags