user373201
user373201

Reputation: 11435

spring jms request response with a queue

I have a simple spring jms tutorial.A lender is acting as receiver. A borrower is action as sender. borrower sends salary and loan amount, and waits for response. The lender runs a decision and sends back "accept" or "decline".

on the borrower side, i am doing the following.

Message sentMessage = (Message)jmsTemplate.execute(new MyProducerCallBack(messageMap, "lenderResponseQueue"));

String filter = "JMSCorrelationID = '" + sentMessage.getJMSMessageID() + "'";
TextMessage tmsg = (TextMessage) jmsTemplate.receiveSelected("lenderResponseQueue", filter);

This is the code in MyProducerCallBack

public Object doInJms(Session session, MessageProducer producer) throws JMSException {
    System.out.println("inside doInJms");

    //create queue to receive accept or decline from lender
    Queue queue = session.createQueue(lenderResponseQueue);

    MapMessage jmsMapMsg = session.createMapMessage();
    populateJMSMapMsg(jmsMapMsg, mapMessage);
    jmsMapMsg.setJMSReplyTo(queue);

    //send salary and loan amount to lender
    producer.send(jmsMapMsg);

    //get the accept or decline from lender
    Message msg = session.createConsumer(queue).receive();

    if(msg instanceof TextMessage){
        System.out.println(((TextMessage)msg).getText());
    }

    return msg;
}

When Message msg = session.createConsumer(queue).receive(); is encountered The logs throw the following exception

2011-10-19 20:21:03,458 WARN [org.apache.activemq.ActiveMQSessionExecutor] - Received a message on a connection which is not yet started. 
Have you forgotten to call Connection.start()? Connection: ActiveMQConnection {id=ID:Reverb0253-PC-54217-1319070060323-0:1,clientId=ID:Reverb0253-PC-54217-1319070060323-1:1,started=false} 
Received: MessageDispatch {commandId = 0, responseRequired = false, consumerId = ID:Reverb0253-PC-54217-1319070060323-0:1:1:1, destination = queue://lenderResponseQueue, message = 
ActiveMQTextMessage {commandId = 7, responseRequired = true, messageId = ID:Reverb0253-PC-52978-1319058441747-0:1:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:Reverb0253-PC-52978-1319058441747-0:1:1:1, destination = queue://lenderResponseQueue, transactionId = null, expiration = 0, timestamp = 1319058485680, arrival = 0, brokerInTime = 1319058485706, brokerOutTime = 1319070063443, correlationId = ID:Reverb0253-PC-53001-1319058485443-0:1:1:1:1, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 5, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, text = Accepted!}, redeliveryCounter = 5}

The receiving queue is lenderResponseQueue is not present in the spring config file. Do I need to specify it there. This is my jmsTemplate from config file

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="queueConnectionFactory"/>
    <property name="destinationResolver" ref="destinationResolver"/>
    <!-- name of destination -->
    <property name="defaultDestinationName" value="lenderRequestQueue"/>
    <!-- amount of time to wait before getting a message -->
    <property name="receiveTimeout" value="0"/>
    <!-- indicate weather queue or topic is used. false = queue or p2p -->
    <property name="pubSubDomain" value="false"/>
</bean>

Upvotes: 2

Views: 8286

Answers (1)

tolitius
tolitius

Reputation: 22509

Is there any specific reason you have both send and receive in the execute callback right after one another:

//send salary and loan amount to lender
producer.send(jmsMapMsg);

//get the accept or decline from lender
Message msg = session.createConsumer(queue).receive();

You would usually receive in a separate JmsTemplate.receive() call ( not within a callback ). That would decouple / unsync your callback, as well as most likely would solve your:

Received a message on a connection which is not yet started.

Upvotes: 1

Related Questions