Receiving a message:-
Synchronous Reception-
While JMS is typically associated with asynchronous processing, it is possible
to consume messages synchronously. The overloaded receive(..) methods provide
this functionality. During a synchronous receive, the calling thread blocks
until a message becomes available.This can be a dangerous operation since the
calling thread can potentially be blocked indefinitely. The property
receiveTimeout specifies how long the receiver should wait before giving up
waiting for a message.
Asynchronous Reception - Message-Driven POJOs-
Message-Driven Bean (MDB) in the EJB world, the Message-Driven POJO (MDP)
acts as a receiver for JMS messages. The one restriction on an MDP is that it
must implement the javax.jms.MessageListener interface.
package r4r.in;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import org.springframework.jms.core.JmsTemplate;
public class jmsReceiver
{
private JmsTemplate jmsTemplate;
private Destination destination;
public MessageReceiver() {}
public void setJmsTemplate(JmsTemplate jmsTemplate)
{
this.jmsTemplate = jmsTemplate;
}
public void setDestination(Destination destination)
{
this.destination = destination;
}
public void receiveMessage()
{
Message message = jmsTemplate.receive();
TextMessage textMessage = null;
if (message instanceof TextMessage)
{
textMessage = (TextMessage)message;
try
{
System.out.println(textMessage.getStringProperty("text"));
}
catch (JMSException e)
{
e.printStackTrace();
}
}
}
}