best way of acheiving request response format
rohit_mca2000 Nov 28, 2005 1:39 AMhello all ,
can anyone please tell me whats the best way to achieve request response format between two application.
i have one it this way, please correct me if i am wrong.
once i get a request , i create a message and i create a temporary queue for every request and send that temp queue destination along with my request to some appication who is listening , the listeneer then extracts my destination ans responds to that destination (temp queue) which i had created and to which i am listening.
but what i am worried about it that for every request i am creating a temp queue. so will it work in real time application where lets say 20-30 requests come at once, so my app will ccreate 20-30 temp queue. I am using Jboss 4.0.
and one more thing. are these temp queue destroyed automatically .if yes then i guess my approach should work i guess. i am pasting my code as well .
please do help me . thanks
package test.jms;
import java.util.HashMap;
import java.util.Properties;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TemporaryQueue;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class PrototypeSenderReceiver {
String queueName = null;
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
Queue replyQueue=null;
QueueSender queueSender= null;
TextMessage requestMessage = null;
QueueReceiver queueReceiver=null;
TemporaryQueue tempQueue=null;
public static void main(String[] args) {
}
public String sendMessage(HashMap messageMap) {
PrototypeSenderReceiver m = new PrototypeSenderReceiver();
try {
m.establishConnectionAndSend(messageMap);
} catch (Exception e) {
e.printStackTrace();
}
return messageMap.get("q").toString();
}
private void establishConnectionAndSend(HashMap messageMap){
queueName = "queue/testQueue";
try {
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
jndiContext = new InitialContext();
queueConnectionFactory = (QueueConnectionFactory)jndiContext.lookup("QueueConnectionFactory");
queue = (Queue) jndiContext.lookup(queueName);
}catch (NamingException e) {
System.out.println("JNDI API lookup failed: " + e.toString());
System.exit(1);
}
try {
queueConnection = queueConnectionFactory.createQueueConnection();
queueSession = queueConnection.createQueueSession (false, Session.AUTO_ACKNOWLEDGE);
queueSender = queueSession.createSender(queue);
requestMessage = queueSession.createTextMessage();
//TODO create XML MESSAGE out of HASHMAP and set it in requestMessage
requestMessage.setText("new value");
tempQueue = queueSession.createTemporaryQueue();
Destination replyDestination = tempQueue;
System.out.println("there");
requestMessage.setJMSReplyTo(replyDestination);
queueSender.send(requestMessage);
System.out.println("******************************");
QueueReceiver qr = queueSession.createReceiver(tempQueue);
queueConnection.start();
TextMessage replyMessage = (TextMessage)qr.receive(30000);
if(replyMessage ==null){
System.out.println("3");
}else{
System.out.println("done with this " + replyMessage.getText());
}
} catch (Exception e) {
System.out.println("stacktrace");
e.printStackTrace();
System.out.println("Exception occurred: " + e.toString());
}
finally {
if (queueConnection != null) {
try {
queueConnection.close();
}catch (JMSException e) {
}
}
}
}
}