If I use the following code to create a sender and receiver
qsender = qsession.createSender((Queue)msg.getJMSDestination());
qreceiver=qsession.createReceiver((Queue)msg.getJMSDestination());
and then do this
qsender.send(msg);
Does it just send the message to the queue and will it remain in the queue forever? Do I need to call the receive() method on the receiver or implement the MessageListener interface to get it to the receiver?
Edit: more info
qsender = qsession.createSender((Queue)msg.getJMSDestination());
qreceiver=qsession.createReceiver((Queue)msg.getJMSDestination());
temp1 = qsession.createTemporaryQueue();
responseConsumer = qsession.createConsumer(temp1);
msg.setJMSReplyTo(temp1);
responseConsumer.setMessageListener(responseListener);
msg.setJMSCorrelationID(msg.getJMSCorrelationID()+i);
qsender.send(msg);
In the above code, what is the temporary queue used for? Is it for receiving the messages? Is it the receiver? And if yes, whats the use of msg.setJMSReplyTo(temp1) ?
Yes, send(..) method will send message to the destination queue. And this message will remain in the queue until your program will receive it using receiver or until your message broker is running (as I know).
About your second question then the difference between two approaches consist in next:
receive(..) method is synchronous (and this is a downside of this approach). This means that the receiver must wait patiently for the message to arrive, as the receive() message will block until a message is available (or until a timeout condition occurs). From other side consuming messages with listener is asynchronous process. Your receiver will not wait. Listener will call your receiving method only when message will be put to the query.
UPDATE:
Temporary destination is used for sending a reply on a message by consumer. For example can be situation that your server get message from client and you need to send response to him. In such case you should use temporary destination. Server application will use this temporary destination (queue in your case) for sending a response message to client. Such queues have a scope limited to the connection that created it, and are removed on the server side as soon as the connection is closed.
More details you can find in this article and in officila java tutorial. In second article also describes how and when to use JMSCorrelationID.
Here is interesting part from official doc that describes how to send response message using temporary destination:
UPDATE 2:
I want to clarify my unswer about message expiration time in the queue (or topic). By default message will never expire. But you can set expiration time for message:
After this all messages produced by this MessageProducer will have specified expiration time.
Also you can specify expiration tyme for concrete message during it’s sending:
where 10000 means 10 seconds