package examples.jms.topic;

import java.io.*;
import java.util.*;
import javax.jts.*;
import javax.naming.*;
import javax.jms.*;

/**
 *
 * The classes in this package work together to show how to establish a connection to a JMS
 * Topic and receive messages from a Topic. This class is used to receive messages from 
 * the Topic.
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class TopicReceive
  implements MessageListener
{
  public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
  public final static String JMS_FACTORY="javax.jms.TopicConnectionFactory";
  public final static String TOPIC="javax.jms.exampleTopic";

  private TopicConnectionFactory tconFactory;
  private TopicConnection tcon;
  private TopicSession tsession;
  private TopicSubscriber tsubscriber;
  private Topic topic;
  private boolean quit = false;

  // MessageListener interface
  public void onMessage(Message msg)
  {
    try {
      String msgText;

      if (msg instanceof TextMessage) {
        msgText = ((TextMessage)msg).getText();
      } else {
        msgText = msg.toString();
      }

      System.out.println("JMS Message Received: "+ msgText );
      
      if (msgText.equalsIgnoreCase("quit")) {
        synchronized(this) {
          quit = true;
          this.notifyAll(); // Notify main thread to quit
        }
      }
    } catch (JMSException jmse) {
      jmse.printStackTrace();
    }
  }

  /**
   * Create all the necessary objects for sending
   * messages to a JMS queue.
   */
  public void init(Context ctx, String topicName)
       throws NamingException, JMSException
  {
    tconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);
    tcon = tconFactory.createTopicConnection();
    tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    try {
      topic = (Topic) ctx.lookup(topicName);
    } catch (NamingException ne) {
      topic = tsession.createTopic(topicName);
      ctx.bind(topicName, topic);
    }
    tsubscriber = tsession.createSubscriber(topic);
    tsubscriber.setMessageListener(this);
    tcon.start();
  }

  /**
   * Close JMS objects.
   */
  public void close()
       throws JMSException
  {
    tsubscriber.close();
    tsession.close();
    tcon.close();
  }


  public static void main(String[] args)
       throws Exception 
  {
    if (args.length != 1) {
      System.out.println("Usage: java examples.jms.topic.TopicReceive WebLogicURL");
      return;
    }
    InitialContext ic = getInitialContext(args[0]);
    TopicReceive tr = new TopicReceive();
    tr.init(ic, TOPIC);

    System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");

    // Wait until a "quit" message has been received.
    synchronized(tr) {
      while (! tr.quit) {
        try {
          tr.wait();
        } catch (InterruptedException ie) {}
      }
    }
    tr.close();
  }

  private static InitialContext getInitialContext(String url)
       throws NamingException
  {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
    env.put(Context.PROVIDER_URL, url);
    env.put("weblogic.jndi.createIntermediateContexts", "true");
    return new InitialContext(env);
  }

}




