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 queue that the pool has registered to receive.
 *
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class QueuePool
{
  private final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
  private final static String JMS_FACTORY="javax.jms.QueueConnectionFactory";
  private final static String QUEUE="javax.jms.exampleQueue";
  private final static String SESSION_POOL_FACTORY="weblogic.jms.ServerSessionPoolFactory";
  private final static String MESSAGE_LISTENER="examples.jms.sessionpool.MsgListener";

  private QueueConnectionFactory qconFactory;
  private QueueConnection qcon;
  private QueueSession qsession;
  private QueueSender qsender;
  private Queue queue;
  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 queueName)
       throws NamingException, JMSException
  {
    qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
    qcon = qconFactory.createQueueConnection();
    qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    try {
      queue = (Queue) ctx.lookup(queueName);
    } catch (NamingException ne) {
      queue = qsession.createQueue(queueName);
      ctx.bind(queueName, queue);
    }
    qsender = qsession.createSender(queue);
    msg = qsession.createTextMessage();
    qcon.start();
    sessionPoolFactory = (ServerSessionPoolFactory) ctx.lookup(SESSION_POOL_FACTORY);
    sessionPool = 
      sessionPoolFactory.getServerSessionPool(qcon, 5, false, 
                                              Session.AUTO_ACKNOWLEDGE, 
                                              MESSAGE_LISTENER);
    consumer = qcon.createConnectionConsumer(queue, "TRUE", sessionPool, 10);
  }

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

    msg.setText(message);
    qsender.send(msg);
  }

  /**
   * Close JMS objects.
   */
  public void close()
       throws JMSException
  {
    consumer.close();
    qsender.close();
    qsession.close();
    qcon.close();
  }


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

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

    readAndSend(qp);
    qp.close();
  }

  protected static void readAndSend(QueuePool qp) 
       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) {
        qp.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);
  }

}
