QueueRequestor Example
The following code shows how to use the QueueRequestor.
The QueueRequestor is actually quite lame. Internally it does
QueueRecevier.receive()
which will wait forever if there is nobody listening to your requests. Better would to use a timeout,
QueueRecevier.recieve(timeout);
Code
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueRequestor;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
public class Client implements MessageListener
{
public static void main(String[] args) throws Exception
{
log.info("Creating jndi context - alternatively use a jndi.properties");
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
properties.put(Context.PROVIDER_URL, "localhost");
InitialContext ctx = new InitialContext(properties);
log.info("Looking up queue");
Queue queue = (Queue) ctx.lookup("queue/testQueue");
log.info("Looking up connection factory");
QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup("UIL2ConnectionFactory");
log.info("Creating connection");
QueueConnection qc = qcf.createQueueConnection();
try
{
log.info("Creating session");
QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
log.info("Creating receiver");
QueueReceiver receiver = qs.createReceiver(queue);
log.info("Set the message listener");
receiver.setMessageListener(new Client(qs));
log.info("You have to start the connection before receiving messages");
qc.start();
log.info("Creating queue requestor session");
QueueSession qrs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
log.info("Creating queue requestor");
QueueRequestor qr = new QueueRequestor(qrs, queue);
log.info("Creating message");
TextMessage hello = qs.createTextMessage("hello");
log.info("Sending request");
Message response = qr.request(hello);
log.info("Close the queue requestor");
qr.close();
log.info("Got response " + response);
}
finally
{
qc.close();
}
}
QueueSession session;
public Client(QueueSession session)
{
this.session = session;
}
public void onMessage(Message message)
{
log.info("Processing message: " + message);
try
{
Queue replyQueue = (Queue) message.getJMSReplyTo();
log.info("Reply queue is: " + replyQueue);
log.info("Creating sender");
QueueSender sender = session.createSender(replyQueue);
log.info("Creating reply");
TextMessage reply = session.createTextMessage("Reply");
log.info("Sending reply");
sender.send(reply);
log.info("Tidyup sender - you could be processing many requests");
sender.close();
}
catch (JMSException e)
{
log.error("Got unexpected error", e);
System.exit(0);
}
}
public static class log
{
public static void info(String message)
{
System.out.println(message);
}
public static void error(String message, Throwable t)
{
System.err.println(message);
t.printStackTrace();
}
}
}
Instructions
To compile it: javac -classpath JBOSS_HOME/client/jbossall-client.jar Client.java or windows javac -classpath JBOSS_HOME\client\jbossall-client.jar Client.java To run it: java -classpath .:JBOSS_HOME/client/jbossall-client.jar Client or windows java -classpath .;JBOSS_HOME\client\jbossall-client.jar Client
Output
Creating jndi context - alternatively use a jndi.properties
Looking up queue
Looking up connection factory
Creating connection
Creating session
Creating receiver
Set the message listener
You have to start the connection before receiving messages
Creating queue requestor session
Creating queue requestor
Creating message
Sending request
Processing message: SpyTextMessage {
Header {
jmsDestination : QUEUE.testQueue
jmsDeliveryMode : 2
jmsExpiration : 0
jmsPriority : 4
jmsMessageID : ID:1-10825512815071
jmsTimeStamp : 1082551281507
jmsCorrelationID: null
jmsReplyTo : QUEUE.JMS_TQ1
jmsType : null
jmsRedelivered : false
jmsProperties : {}
jmsPropReadWrite: false
msgReadOnly : true
producerClientId: ID:1
}
Body {
text :hello
}
}
Reply queue is: QUEUE.JMS_TQ1
Creating sender
Creating reply
Sending reply
Tidyup sender - you could be processing many requests
Close the queue requestor
Got response SpyTextMessage {
Header {
jmsDestination : QUEUE.JMS_TQ1
jmsDeliveryMode : 1
jmsExpiration : 0
jmsPriority : 4
jmsMessageID : ID:1-10825512820892
jmsTimeStamp : 1082551282089
jmsCorrelationID: null
jmsReplyTo : null
jmsType : null
jmsRedelivered : false
jmsProperties : {}
jmsPropReadWrite: false
msgReadOnly : true
producerClientId: ID:1
}
Body {
text :Reply
}
}
Comments