Sending a Message Using JMS API by R4R Team

Sending a Message:-
The JmsTemplate contains many convenience methods to send a message. There are send methods that specify the destination using a javax.jms.Destination object and those that specify the destination using a string for use in a JNDI lookup.

package r4r.in;

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.Session;

import javax.jms.TextMessage;

import org.springframework.jms.core.JmsTemplate;

import org.springframework.jms.core.MessageCreator;

public class jmsSender

{

private Destination destination;

private JmsTemplate jmsTemplate;

public jmsSender() {}

public void setJmsTemplate(JmsTemplate jmsTemplate)

{

this.jmsTemplate = jmsTemplate;

}

public void setDestination(Destination destination)

{

this.destination = destination;

}

public void sendMessage()

{

MessageCreator creator = new MessageCreator()

{

public Message createMessage(Session session)

{

TextMessage message = null;

try

{

message = session.createTextMessage();

message.setStringProperty("text", "Hello Spring JMS");

}

catch (JMSException e)

{

e.printStackTrace();

}

return message;

}

};

jmsTemplate.send(destination, creator);

}

}


The jmsSender class needs to be aware of two things, one is the destination object where the message has to be sent and the other being the JMSTemplate object that simplifies the process of sending the message. The sending of the message is defined by the method JMSTemplate.send() that takes 2 arguments. The first argument is the message destination itself and the second argument is the message creator object that knows how to create a message that can be sent later.
  

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!