package examples.jms.startup;

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

/**
 *
 * This example shows how to establish a JMS message consumer
 * from a WebLogic startup class.  Startup classes are loaded
 * and executed when the server is first started.
 * The  <font face="Courier New" size = -1>PoolReceive</font> example shows how to establish 
 *a pool of message listeners (<font face="Courier New" size = -1>MsgListener</font> class) 
 *from a startup class. As messages are 
 * received from the tropic they are displayed in shell running the WebLogic Server. 
 * You can use the <font face="Courier New" size = -1>
 *<a href=../topic/Package-examples.jms.topic.html>TopicSend</a></font> client 
 * to send messages to the Topic.
 * <p>
 * Configure the server:
 * <p>
 * <ol>
 * <li>
 * Add the following properties to your <tt>weblogic.properties</tt> file:
 * <pre>
 * weblogic.system.startupClass.poolReceive=examples.jms.startup.PoolReceive
 * weblogic.system.startupArgs.poolReceive=\
 *    connectionFactory=javax.jms.TopicConnectionFactory,\
 *    topic=javax.jms.exampleTopic</pre>
 * </ol>
 * Build the example:
 * <ol>
 * <li>
 *  Set up your development shell, as described in 
 * <a href="../../../techstart/environment.html">Setting up 
 * your environment</a>.
 * <li>
 * Compile the files in this directory as shown in this examples for Windows NT:
 * <pre>  $<font face="Courier New"><b> javac -d %SERVER_CLASSES% MsgListener.java PoolReceive.java</b></font></pre>
 * </ol>
 * Run the example:
 * <ol>
 * <li>
 * Start the WebLogic Server in a new command shell.
 * <li>
 * Start the <font face="Courier New" size = -1>
 *<a href=../topic/Package-examples.jms.topic.html>TopicSend</a></font> client in your 
 *development shell and send messages to the topic.
 *The messages are displayed in the shell running 
 *WebLogic Server as they are 
 * received from the Topic.  Send messages at less than 1 second intervals 
 * to see the pool in action.
 * </ol>
 *
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class PoolReceive
  implements T3StartupDef
{
  private final static String SESSION_POOL_FACTORY = 
  "weblogic.jms.ServerSessionPoolFactory";

  private T3ServicesDef services;
  private TopicConnectionFactory tconFactory;
  private TopicConnection tcon;
  private TopicSession tsession;
  private Topic topic;
  private ServerSessionPoolFactory sessionPoolFactory;
  private ServerSessionPool sessionPool;
  private ConnectionConsumer consumer;

  /**
   * The <tt>setServices()</tt> method passes to the
   * startup class the handle of an object that can be used 
   * to access WebLogic services. This makes another reference 
   * to the services object so it can be used later.
   *
   * @param services          Services stub
   */
  public void setServices(T3ServicesDef services) {
    this.services = services;
  }

  public String startup(String name, Hashtable args) 
       throws Exception 
  {
    String connectionFactory = (String)args.get("connectionFactory");
    String topicName = (String)args.get("topic");

    if (connectionFactory == null || topicName == null) {
      throw new IllegalArgumentException("connectionFactory="+connectionFactory+
                                         ", topicName="+topicName);
    }

    Context ctx = new InitialContext();
    tconFactory = (TopicConnectionFactory) ctx.lookup(connectionFactory);
    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);
    }
    tcon.start();
    sessionPoolFactory = (ServerSessionPoolFactory) 
      ctx.lookup(SESSION_POOL_FACTORY);
    sessionPool = sessionPoolFactory.
      getServerSessionPool(tcon, 5, false, 
                           Session.AUTO_ACKNOWLEDGE, 
                           "examples.jms.startup.MsgListener");
    consumer = tcon.createConnectionConsumer(topic, "TRUE", sessionPool, 10);

    return "Ready To Receive Messages";
  }

}






