O'Reilly logo

Building Java Enterprise Applications by Brett McLaughlin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Messaging on the Client

At this point, handling messaging from the client perspective is a piece of cake. It’s simply a matter of connecting to the messaging service (through your application server’s messaging middleware) and listening to a specific Topic or Queue. To ensure that you can get some sample code up and running to see how this works, I’ve included Example 9-5, a simple listener class.

Example 9-5. The JMSTester Class

package com.forethought.client; import java.util.Date; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; import javax.jms.Session; import javax.jms.TextMessage; import javax.jms.Topic; import javax.jms.TopicConnection; import javax.jms.TopicConnectionFactory; import javax.jms.TopicSession; import javax.jms.TopicSubscriber; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; // Event bean import com.forethought.ejb.event.EventInfo; // User bean import com.forethought.ejb.user.UserInfo; public class JMSTester implements MessageListener { public JMSTester(String factoryName, String topicName) throws JMSException, NamingException { Context context = getInitialContext( ); // Get topic factory TopicConnectionFactory factory = (TopicConnectionFactory)context.lookup(factoryName); Topic topic = (Topic)context.lookup(topicName); // Connect to topic TopicConnection connection = factory.createTopicConnection( ); // Send off notification of this ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required