If you want your receive-response to be synchronous (the sender can't send the second message till it receives the response message for the first one), use the same thread. If you want to be asynchronous (each message needs a response in within some timeout window, but you can send the second and third and fourth without waiting for the response to the first, then use two threads and some kind of Collection to hold the messages still awaiting a response. (You could implement synchronous request-response using two threads if you wanted, though you'll need a little more code to synchronize between the two.)
BTW, if you haven't looked at Camel for request-response you really should. It makes it pretty easy to set up, with none of the boilerplate code. It might not meet your needs, but it's at least worth understanding and considering. On Apr 30, 2015 10:15 PM, "khandelwalanuj" <[email protected]> wrote: > 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. >
