Receiving a message using JMS API by R4R Team

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();

}

}

}

}

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!