package examples.jms.sessionpool;

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

/**
 * This example shows how to establish a pool of server-side
 * message listeners that process incoming messages in parallel.
 * This class establishes the listener pool, and then sends messages
 * to the topic that the pool has registered to receive.
 *
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class TopicPool
{
  private final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
  private final static String JMS_FACTORY="javax.jms.TopicConnectionFactory";
  private final static String TOPIC="javax.jms.exampleTopic";
  private final static String SESSION_POOL_FACTORY="weblogic.jms.ServerSessionPoolFactory";

  private TopicConnectionFactory tconFactory;
  private TopicConnection tcon;
  private TopicSession tsession;
  private TopicPublisher tpublisher;
  private Topic topic;
  private TextMessage msg;
  private ServerSessionPoolFactory sessionPoolFactory;
  private ServerSessionPool sessionPool;
  private ConnectionConsumer consumer;

  /**
   * 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);
    }
    tpublisher = tsession.createPublisher(topic);
    msg = tsession.createTextMessage();
    tcon.start();
    sessionPoolFactory = (ServerSessionPoolFactory) ctx.lookup(SESSION_POOL_FACTORY);
    sessionPool = sessionPoolFactory.getServerSessionPool(tcon, 5, false, Session.AUTO_ACKNOWLEDGE, 
                                                          "examples.jms.sessionpool.MsgListener");
    consumer = tcon.createConnectionConsumer(topic, "TRUE", sessionPool, 10);
  }

  /**
   * Send a message to a JMS Topic.
   */
  public void send(String message)
       throws JMSException
  {

    msg.setText(message);
    tpublisher.publish(msg);
  }

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


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

    System.out.println("JMS Session Pool Established");

    readAndSend(tp);
    tp.close();
  }

  protected static void readAndSend(TopicPool lps) 
       throws IOException, JMSException
  {
    BufferedReader msgStream = new BufferedReader (new InputStreamReader(System.in));
    String line=null;
    do {
      System.out.print("Enter message (\"quit\" to quit): ");
      line = msgStream.readLine();
      if (line != null && line.trim().length() != 0) {
        lps.send(line);
        System.out.println("JMS Message Sent: "+line+"\n");
      }
    } while (line != null && ! line.equalsIgnoreCase("quit"));

  }

  protected 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);
  }

}
