Package openjms.examples.appserver

Source Code of openjms.examples.appserver.ExoLabApplicationServer

/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
*    statements and notices.  Redistributions must also contain a
*    copy of this document.
*
* 2. Redistributions in binary form must reproduce the
*    above copyright notice, this list of conditions and the
*    following disclaimer in the documentation and/or other
*    materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
*    products derived from this Software without prior written
*    permission of Exoffice Technologies.  For written permission,
*    please contact info@exolab.org.
*
* 4. Products derived from this Software may not be called "Exolab"
*    nor may "Exolab" appear in their names without prior written
*    permission of Exoffice Technologies. Exolab is a registered
*    trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
*    (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: ExoLabApplicationServer.java,v 1.3 2001/09/24 08:40:32 jima Exp $
*
* Date         Author  Changes
* 11/02/2000   jima    Created
*/
package openjms.examples.appserver;

// java util
import java.util.Hashtable;

// java io
import java.io.PrintStream;

// naming
import javax.naming.Context;
import javax.naming.InitialContext;

// exolab core
import org.exolab.jms.util.CommandLine;

// jms package
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ConnectionConsumer;
import javax.jms.JMSException;
import javax.jms.Topic;

// jms specific
import org.exolab.jms.client.JmsServerSessionPool;
import org.exolab.jms.client.JmsTopic;

// logger
import org.exolab.core.logger.LoggerIfc;
import org.exolab.core.logger.LoggerFactory;

/**
* This class provides a context to test the application server
*/
public class ExoLabApplicationServer
    implements Runnable
{
    /**
     * The mainline will instantiate an application server and start it.
     */
    public static void main (String[] args)
    {
        try
        {
            // create the mock app server
            ExoLabApplicationServer server = new ExoLabApplicationServer(
                new CommandLine(args));

            // start it up.
            (new Thread(server)).start();
        }
        catch (Exception exception)
        {
            getLogger().logError("Failed to init the application server " +
                    exception);
            exception.printStackTrace();
        }
    }

    /**
     * The constructor will instantiate a connection to the server, initialise the
     * connection and connection consumer and then start the connection.
     * <p>
     * It uses the command line to retrieve the particular options.
     */ 
    private ExoLabApplicationServer(CommandLine cmdline)
        throws Exception
    {
        String topic_name = cmdline.value("topic");

        if (cmdline.exists("help") )
        {
            usage();
        }
        else if (topic_name != null)
        {
            // connect to the JNDI server and get a reference to
            // root context
            Hashtable props = new Hashtable();
            String mode = cmdline.value("mode");
            String modeType =
                "org.exolab.jms.jndi.rmi.RmiJndiInitialContextFactory";

            if (mode != null)
            {
                if (mode.equals("ipc"))
                {
                    System.out.println("Using IPC mode");
                    modeType =
                        "org.exolab.jms.jndi.mipc.IpcJndiInitialContextFactory";
                }
            }

            props.put(Context.INITIAL_CONTEXT_FACTORY, modeType);
            Context context = new InitialContext(props);

            // if we can't get the root context then exit with an exception
            if (context == null)
            {
                throw new RuntimeException("Failed to get the root context");
            }

            // lookup the connection factory from the context
            TopicConnectionFactory factory =
                (TopicConnectionFactory)context.lookup("JmsTopicConnectionFactory");

            // if we can't find the factory then throw an exception
            if (factory == null)
            {
                throw new RuntimeException("Failed to locate connection factory");
            }

            // create the topic. In reality the topic should be registered with JNDI
            // but this will do for now.
            Topic topic = new JmsTopic(topic_name);

            // get a topic connection to the factory.
            connection_ = factory.createTopicConnection();
           
            // set up the server session pool size to 15 and a message listener
            JmsServerSessionPool.init(15,
                new MessageListener ()
                {
                    /**
                     * Simply log the message to the screen
                     *
                     * @param       message         message it receives
                     */
                    public void onMessage(Message message)
                    {
                        ExoLabApplicationServer.getLogger().logInfo(
                            "[" + Thread.currentThread().hashCode() +
                            "] Processing message " + message);

                        // sleep for awhile
                        try
                        {
                            Thread.currentThread().sleep(2000);
                        }
                        catch (InterruptedException exception)
                        {
                        }
                    }
                }
            );

            // create a connection consumer
            connectionConsumer_ = connection_.createConnectionConsumer(
                topic, null, JmsServerSessionPool.instance(), 1);

            // log a trace method
            getLogger().logInfo("ExoLabApplicationServer is ready.");
        }
        else
        {
            usage();
        }
    }

    /**
     * Run the application server. This will start the connection, which will
     * enable it to receive messages
     */
    public void run()
    {
        try
        {
            connection_.start();
        }
        catch (JMSException exception)
        {
            exception.printStackTrace();
        }
    }

    /**
     * Print out information on running this sevice
     */
    static protected void usage()
    {
    PrintStream out = System.out;

    out.println("\n\n");
    out.println("========================================================================");
    out.println("Usage information for openjms.examples.appserver.ExoLabApplicationServer");
    out.println("========================================================================");
    out.println("\nopenjms.examples.appserver.ExoLabApplicationserve");
    out.println("    [-help | -topic <topic name> | -mode <rmi/ipc> \n");
    out.println("\t-help   displays this screen\n");
    out.println("\t-topic   topic name to subscriber under.\n");
    out.println("\t-mode    connect in either ipc mode or rmi mode.\n");

        // force exit
        System.exit(0);
    }


    /**
     * Return a reference ot the logger
     */
    static LoggerIfc getLogger()
    {
        return LoggerFactory.getLogger();
    }


    /**
     * This is the reference to the topic connection, used by this appserver
     */
    private TopicConnection connection_ = null;

    /**
     * This is a reference to the connection consumer
     */
    private ConnectionConsumer connectionConsumer_ = null;

}

TOP

Related Classes of openjms.examples.appserver.ExoLabApplicationServer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.