Java Reference
In-Depth Information
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.jms.Topic;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.Session;
import javax.jms.MessageListener;
import javax.jms.JMSException;
public class DebugSubscriber implements MessageListener
{
private TopicConnection topicConnection=null;
private TopicSession topicSession=null;
private TopicSubscriber topicSubscriber=null;
private Topic topic=null;
private TopicConnectionFactory topicFactory=null;
private int count_=0;
private Context context=null;
public DebugSubscriber() throws JMSException, NamingException
{
String factoryJNDI="TopicConnectionFactory";
String topicJNDI="topic/deviceMessages";
// Get the initial context
System.out.println("Getting Initial Context:");
context = new InitialContext();
System.out.println("Got Initial Context:"+context);
// Get the connection factory
System.out.println("Getting Topic Factory:");
topicFactory=(TopicConnectionFactory)
context.lookup(factoryJNDI);
System.out.println("Got Topic Factory:"+topicFactory);
// Create the connection
topicConnection = topicFactory.createTopicConnection();
// Create the session
topicSession=topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
// Look up the destination
topic = (Topic)context.lookup(topicJNDI);
// Create a subscriber
topicSubscriber = topicSession.createSubscriber(topic);
// Set the message listener,
// which is this class since we implement
// the MessageListener interface
topicSubscriber.setMessageListener(this);
System.out.println("DeviceSubscriber subscribed to topic: "
+ topicJNDI);
Search WWH ::




Custom Search