Package openjms.examples.client.console

Source Code of openjms.examples.client.console.SimplePublisher

/**
* 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: SimplePublisher.java,v 1.40 2002/10/20 03:06:57 jalateras Exp $
*
* Date         Author  Changes
* 04/25/2000   jima    Created
*/
package openjms.examples.client.console;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.PrintStream;
import java.util.Hashtable;
import java.util.Date;
import java.util.Random;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.jms.Session;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.TopicSession;
import javax.jms.TopicPublisher;
import javax.jms.Topic;
import javax.jms.TextMessage;
import javax.jms.DeliveryMode;
import javax.jms.TopicConnectionFactory;
import org.exolab.core.logger.LoggerFactory;
import org.exolab.core.logger.LoggerIfc;
import org.exolab.core.logger.LogEventType;
import org.exolab.jms.util.CommandLine;
import org.exolab.jms.client.JmsTopicConnectionFactory;
import org.exolab.jms.jndi.JndiConstants;
import org.exolab.jms.jndi.rmi.RmiJndiInitialContextFactory;

/**
* The simple publisher application simply grabs a connection and a session.
* It then creates a topic and begins to publish messages. The application
* expects a topic name to be specified on the command line. If one is not
* specified then an exception is raised.
*/
public class SimplePublisher {
    static public void main(String[] args) {
        try {
            CommandLine cmdline = new CommandLine(args);

            if (cmdline.exists("help")) {
                // the help option has been specified, print the usage
                // information
                usage();
            } else if (cmdline.exists("topic")) {
                // enable debugging - unbdocumented option
                if (cmdline.exists("debug")) {
                    LoggerIfc logger = LoggerFactory.getLogger();
                    logger.setLogLevel(LogEventType.Debug);
                }

                // connect to the JNDI server and get a reference to
                // root context
                Hashtable props = new Hashtable();
                String topic_name = cmdline.value("topic");
                String host = "localhost";
                String port = null;
                String jndiname = "JndiServer";

                String mode = null;
                if (cmdline.exists("mode")) {
                    mode = cmdline.value("mode");
                } else {
                    mode = "rmi";
                }

                String modeType =
                    RmiJndiInitialContextFactory.class.getName();

                if (cmdline.exists("jndiport")) {
                    port = cmdline.value("jndiport");
                }

                if (cmdline.exists("jndihost")) {
                    host = cmdline.value("jndihost");
                }

                // override the default server name if specified on the
                // command line.
                if (cmdline.exists("jndiname")) {
                    jndiname = cmdline.value("jndiname");
                }

                if (mode.equals("ipc") || mode.equals("tcp")) {
                    if (port == null) {
                        port = "3035";
                    }

                    props.put(Context.PROVIDER_URL,
                        "tcp://" + host + ":" + port + "/");
                    modeType =
                        "org.exolab.jms.jndi.mipc.IpcJndiInitialContextFactory";
                } else if (mode.equals("http")) {
                    System.out.println("Using HTTP");

                    /* CONNECTING BY WAY OF PROXY 
                       Client needs to set these system properties if
                       it requires to go through a proxy for HTTP.
                       System.setProperty("http.proxyHost", host);
                       System.setProperty("http.proxyPort", port);
                    */

                    /* CONNECTING FOR SSL TRANSACTIONS.
                       Client needs to set these system properties
                       if it requires to go through a proxy for HTTPS.

                       System.setProperty("https.proxyHost", "host");
                       System.setProperty("https.proxyPort", "port");


                       Client needs to pass in "-mode http -secure"
                       to use https
                       Client needs -Djavax.net.ssl.trustStore=cacerts
                       passed in. JSSE is required to make this work.
                    */

                    String type = "http://";
                    if (cmdline.exists("secure")) {
                        if (port == null) {
                            port = "8443";
                        }
                        type = "https://";
                        modeType =
                            "org.exolab.jms.jndi.http.SslHttpJndiInitialContextFactory";
                    } else {
                        if (port == null) {
                            port = "8080";
                        }
                        modeType =
                            "org.exolab.jms.jndi.http.HttpJndiInitialContextFactory";
                    }

                    props.put(Context.PROVIDER_URL,
                        type + host + ":" + port + "/" +
                        "openjms/servlet/OpenJMSJndi");

                    String url = cmdline.value("url");
                    if (url == null) {
                        url = type + "localhost:8080";
                    }

                    // Client URL to allow server to send messages
                    // to registered consumers/receivers
                    System.getProperties().setProperty
                        (JndiConstants.HTTP_CLIENT_URL_PROPERTY, url);

                } else {
                    if (port == null) {
                        port = "1099";
                    }

                    props.put(Context.PROVIDER_URL,
                        "rmi://" + host + ":" + port + "/" + jndiname);
                }

                System.err.println("Using provider url " + props.get(Context.PROVIDER_URL));
                props.put(Context.INITIAL_CONTEXT_FACTORY, modeType);
                Context context = new InitialContext(props);

                // 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");
                }

                TopicConnection connection =
                    factory.createTopicConnection();
                connection.start();

                TopicSession session = connection.createTopicSession(
                    false, Session.CLIENT_ACKNOWLEDGE);
                Topic topic = null;
                topic = session.createTopic(topic_name);

                if (topic == null) {
                    System.err.println("Failed to get administered object"
                        + " exiting.....");
                    System.exit(-1);
                }

                TopicPublisher publisher = session.createPublisher(topic);

                // check the number of iterations that have been specified
                // on the command line. If it has not yet been specified
                // then default to 10.
                int count = 10;
                if (cmdline.exists("count")) {
                    try {
                        count = Integer.parseInt(cmdline.value("count"));
                    } catch (Exception exception) {
                        System.err.println("Illegal count value");
                        System.exit(-1);
                    }
                }

                // check if a sleep has been specified
                int sleep = 0;
                if (cmdline.exists("sleep")) {
                    try {
                        sleep = Integer.parseInt(cmdline.value("sleep"));
                    } catch (Exception exception) {
                        System.err.println("Illegal sleep value");
                        System.exit(-1);
                    }
                }

                // determine if the delivery mode should be persistent or
                // non persistent
                int delivery_mode = DeliveryMode.NON_PERSISTENT;
                if (cmdline.exists("persistent")) {
                    delivery_mode = DeliveryMode.PERSISTENT;
                }

                // create a text message
                Random rand = new Random((new Date()).getTime());
                for (int index = 0; index < count; index++) {
                    TextMessage message = session.createTextMessage(
                        "message " + index);
                    int priority = Math.abs(rand.nextInt() % 10);
                    publisher.publish(topic, message, delivery_mode,
                        priority, 0);

                    if (sleep > 0) {
                        try {
                            Thread.sleep(sleep);
                        } catch (Exception exception) {
                        }
                    }
                }

                System.out.println("The publisher published " + count +
                                   " messages on topic " + topic_name);

                if (mode.equals("ipc") || mode.equals("tcp") || mode.equals("http")) {
                    Thread.sleep(1000);
                }

                publisher.close();
                session.close();
                connection.close();                
            } else {
                System.err.println("Cannot publish messages under null " +
                    "topic");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        System.exit(0);
    }

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

        out.println("usage: java " + SimplePublisher.class.getName() +
            " [options]\n");
        out.println("options:");
        out.println("  -topic <name>      topic to publisher under.\n");
        out.println("  -persistent        " +
            "specifies persistent delivery mode.\n");
        out.println("  -mode <ipc | rmi | http>  " +
            "connect using ipc, http mode or rmi mode. " +
            "Defaults to 'rmi'.\n");
        out.println("  -secure only used for http mode. Connect using https.\n"
            "   requires JSSE jars, https enabled Web server etc.\n");
        out.println("  -jndiport <num>    port where the jndi server runs.\n");
        out.println("  -jndihost <host>   host where jndi server runs.\n");
        out.println("  -jndiname <name>   name of the jndi server\n");
        out.println("  -count <num>       number of messages to publish.\n");
        out.println("  -help              displays this screen.\n");
    }
}
TOP

Related Classes of openjms.examples.client.console.SimplePublisher

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.