Java Reference
In-Depth Information
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
public class FrontDeskImpl implements FrontDesk {
public void sendMail(Mail mail) {
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);
MessageProducer producer = session.createProducer(destination);
MapMessage message = session.createMapMessage();
message.setString("mailId", mail.getMailId());
message.setString("country", mail.getCountry());
message.setDouble("weight", mail.getWeight());
producer.send(message);
session.close();
} catch (JMSException e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (JMSException e) {
}
}
}
}
}
In the preceding sendMail() method, you first create JMS-specific ConnectionFactory and
Destination objects with the classes provided by ActiveMQ. The message broker URL is the default for
ActiveMQ if you run it on localhost. In JMS, there are two types of destinations: queue and topic. As
explained before, a queue is for the point-to-point communication model, while topic is for the publish-
subscribe communication model. Because you are sending JMS messages point to point from front desk
to back office, you should use a message queue. You can easily create a topic as a destination using the
ActiveMQTopic class.
Search WWH ::




Custom Search