Java Reference
In-Depth Information
Next, you have to create a connection, session, and message producer before you can send your
message. There are several types of messages defined in the JMS API, including TextMessage , MapMessage ,
BytesMessage , ObjectMessage , and StreamMessage . MapMessage contains message content in key/value
pairs like a map. All of them are interfaces, whose super class is simply Message . In the meantime, you
have to handle JMSException , which may be thrown by the JMS API. Finally, you must remember to close
the session and connection to release system resources. Every time a JMS connection is closed, all its
opened sessions will be closed automatically. So you only have to ensure that the JMS connection is
closed properly in the finally block.
On the other hand, the following BackOfficeImpl class receives JMS messages with the JMS API
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
public class BackOfficeImpl implements BackOffice {
public Mail receiveMail() {
ConnectionFactory cf =
new ActiveMQConnectionFactory(" tcp://localhost:61616 ");
Destination destination = new ActiveMQQueue("mail.queue");
Connection conn = null;
try {
conn = cf.createConnection();
Session session =
conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(destination);
MapMessage message = (MapMessage) consumer.receive();
Mail mail = new Mail();
return mail;
Search WWH ::

Custom Search