Book Image

EJB 3 Developer Guide

By : Michael Sikora
Book Image

EJB 3 Developer Guide

By: Michael Sikora

Overview of this book

Table of Contents (18 chapters)
EJB 3 Developer Guide
Credits
About the Author
About the Reviewers
Preface
Annotations and Their Corresponding Packages

Topic Producer and Consumer Examples


We will start with a Java application, TopicProducer.java, acting as a JMS topic producer. Recall we use a topic in publish/subscribe messaging. Publish/subscribe is used for one-to-many delivery of messages. Typically this mode is used for broadcasting information to a large number of consumers who may not even be known to the topic producer. An example in the banking industry would be the notification of an imminent interest rate cut. TopicProducer.java just sends a message "Savings account interest rate cut by 0.5 % "

The code is very similar to QueueProducer.

public class TopicProducer {
public static void main(String[] args) {
ConnectionFactory cf = null;
Connection conn = null;
Topic topic = null;
try {
InitialContext ctx = new InitialContext();
cf = (ConnectionFactory)
ctx.lookup("BankServiceConnectionFactory");
topic = (Topic) ctx.lookup("BankServiceJMSTopic");
} catch (NamingException e) {
e.printStackTrace();
}
try {
conn = cf.createConnection...