Package client.jms1_0_2

Source Code of client.jms1_0_2.TopicPublisherExample

package client.jms1_0_2;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.jms.ConnectionFactory;
import javax.jms.TopicConnectionFactory;
import javax.jms.Destination;
import javax.jms.Topic;
import javax.jms.Session;
import javax.jms.Connection;
import javax.jms.TopicConnection;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import javax.jms.TopicSession;
import org.apache.log4j.Logger;
import javax.jms.JMSException;
import javax.jms.TopicPublisher;
import client.common.ServerDependent;
import javax.naming.Context;

/**
* A simple JMS client that publishes messages to a topic. It uses the older JMS 1.0.2 interfaces.
* Functionally, is similar with client.common.CommonInterfacePublisher.
*
* @author Ovidiu Feodorov <ovidiu@feodorov.com>
* @version $Revision: 1.2 $ $Date: 2004/03/11 05:13:49 $
**/
public class TopicPublisherExample {

    private static final Logger log = Logger.getLogger(TopicPublisherExample.class);

    private static final int DEFAULT_NUMBER_OF_MESSAGES = 10;

    /**
     * @param args - if "-appclient" is present here, no CORBA environment setup is attempted.
     **/
    public static void main(String[] args) throws Exception {

        args = ServerDependent.init(args);

        Context initialContext = new InitialContext();

        TopicConnectionFactory connectionFactory =
            (TopicConnectionFactory)initialContext.lookup("ConnectionFactory");

        Topic topic = (Topic)initialContext.lookup("Topic1");

        TopicConnection connection = connectionFactory.createTopicConnection();

        TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
       
        TopicPublisher publisher = session.createPublisher(topic);

        Thread.sleep(1000);

        int numberOfMessages = getNumberOfMessages(args);
        log.info("Sending "+numberOfMessages+" text messages ...");

        for(int i = 0; i < numberOfMessages; i++) {
            TextMessage message = session.createTextMessage();
            message.setText("This is message "+i);
            publisher.send(message);
            log.debug("sent message "+i);
        }

        TextMessage message = session.createTextMessage();
        message.setText("");
        publisher.send(message);
        log.debug("sent end-of-communication message");

        log.info("Finished sending messages");

        // TO_DO: If I immediately close the producer after sending the messages, sometimes the
        // view change arrives before the messages, which are then discared by NACKACK.
//         Thread.sleep(1000);
//         connection.close();
//         log.info("Successfully closed the connection");
//         System.exit(0);
    }


    private static int getNumberOfMessages(String[] args) {
       
        int result = DEFAULT_NUMBER_OF_MESSAGES;

        if (args.length > 0) {
            try {
                result = Integer.parseInt(args[0]);
            }
            catch(Exception e) {
                log.warn("Invalid number of messages: "+args[0]);
            }
        }

        return result;
    }


}


TOP

Related Classes of client.jms1_0_2.TopicPublisherExample

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.