Hi,

I am trying to achieve request-response with ActiveMQ. This is how my
listener looks like. We are using JMSTemplate to send the messages. This is
the source code of producer who sends the messages and then wait for
specified time. 

I just want to make sure whether we should use "send" call and "receive"
call (for response) in the same tread or we should use different thread ?

Public class Publisher
 {
 
    private final Log            LOG             =
LogFactory.getLog(this.getClass());
     public JmsTemplate           myJmsTemplate;
     public Destination           sendDestination;
     public Destination           receiveDestination;
     
     private static final Integer DEFAULT_TIMEOUT = 30_000;
     private static final int     ackMode         =
Session.AUTO_ACKNOWLEDGE;
     private static final boolean transacted      = false;
     
     public boolean publish()
     {
     String message = "check_transaction_capture_service";
     boolean responseReceived = false;
     
     Connection connection = null;
     Session session = null;
     
     String correlationId = JmsUtil.createCorrelationId();
     
     try
     {
     connection = myJmsTemplate.getConnectionFactory().createConnection();
     session    = connection.createSession(transacted, ackMode);
     
     String messageSelector = "JMSCorrelationID='" + correlationId + "'";
     MessageConsumer responseConsumer =
session.createConsumer(receiveDestination, messageSelector);
     connection.start();
     
     LOG.debug("JMSTemplate.isExplicitQosEnabled: " +
myJmsTemplate.isExplicitQosEnabled() +
     ", JMSTemplate.deliveryMode: " + myJmsTemplate.getDeliveryMode());
     
     // send a text message to broker
     myJmsTemplate.send(sendDestination, new SimpleTextMessageCreator(
     message, receiveDestination, correlationId));
     
     LOG.info("Waiting for message with " + messageSelector +
     " in " + receiveDestination + " for " + DEFAULT_TIMEOUT + " ms");
     
     // check for response from broker
     TextMessage responseMessage = (TextMessage)
responseConsumer.receive(DEFAULT_TIMEOUT);
     if (responseMessage != null)
     {
     if (!responseMessage.getJMSCorrelationID().equals(correlationId)) {
     String errorMsg =
     "Invalid correlation id in response message!!! " +
     "Expected : " + correlationId +
     " but received : " + responseMessage.getJMSCorrelationID();
     
     LOG.error(errorMsg);
     responseReceived = false;
     }
     else {
     LOG.info("Received the response back: " + responseMessage.getText() + "
with correlationId: " + responseMessage.getJMSCorrelationID());
     responseReceived = true;
     }
     }
     else {
     LOG.warn("No heart-beat response received for correlationID: " +
correlationId +
     ", within the timeout period: " + DEFAULT_TIMEOUT + "ms.");
     responseReceived = false;
     }
     }
    catch (Throwable t)
    {
    LOG.warn("Publish encountered unknown exception, while handling
heartbeat for correlationID=" + correlationId + ": " + t, t);
    }
    finally {
    JmsUtil.closeConnection(connection, session, this.getClass().getName());
    }
    
    return responseReceived;
    }
 }   



--
View this message in context: 
http://activemq.2283324.n4.nabble.com/Request-response-with-ActiveMQ-tp4695963.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Reply via email to