/* $Id$ */
// =========================================================
// Copyright (C) NEC Corporation 2004
// NEC CONFIDENTIAL AND PROPRIETARY
// All rights reserved by NEC Corporation.
// This program must be used solely for the purpose for which
// it was furnished by NEC Corporation. No part of this
// program may be reproduced or disclosed to others, in any
// form, without the prior written permission of NEC
// Corporation. Use of copyright notice does not evidence
// publication of the program.
// =========================================================
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;

public class TopicPublisherExample
{
    private static TopicConnectionFactory topicConnectionFactory;
    private static TopicConnection        topicConnection;
    private static TopicSession           topicSession;
    private static TopicPublisher         topicPublisher;
    private static Topic                  topic;
    private static String                 TOPICCONFACT = "TopicConnectionFactory";

    private static String                 myname = TopicPublisherExample.class.getName();
    private static InitialContext         context;


    public static void main( String[] args )
    {
        String          topicName = null;
        int             num_msgs = 10;
        String          msg_text = "Sample message #";
        TextMessage     message = null;

        if( args.length < 2 ) {
            System.out.println( "usage: java " + myname + " <topic_name> <factory_name> [<number_of_messages>]" );
            System.exit(1);
        }
        topicName = args[0];
        TOPICCONFACT = args[1];
        if( args.length >= 3 ) {
            num_msgs = Integer.parseInt(args[2]);
        }

        try {
            System.out.print( "looking up " + TOPICCONFACT + " ..." );
            topicConnectionFactory =
                    (TopicConnectionFactory)lookupJNDI( TOPICCONFACT );
            System.out.println( "done!!" );
            System.out.print( "creating TopicConnection ..." );
            topicConnection =
                    topicConnectionFactory.createTopicConnection();
            System.out.println( "done!!" );
            System.out.print( "creating TopicSession ..." );
            topicSession = topicConnection.createTopicSession( false,
                    Session.AUTO_ACKNOWLEDGE );
            System.out.println( "done!!" );
            System.out.print( "looking up " + topicName + " ..." );
            topic = (Topic)lookupJNDI( topicName );
            System.out.println( "done!!" );
        } catch( Exception e ) {
            System.out.println( e );
            e.printStackTrace();
            if( topicConnection != null ) {
                try {
                    topicConnection.close();
                } catch( JMSException ee ) {}
            }
        }

        try {
            System.out.print( "creating Publisher ..." );
            topicPublisher = topicSession.createPublisher( topic );
            System.out.println( "done!!" );
            message = topicSession.createTextMessage();
            for( int i=0; i<num_msgs; i++ ) {
                message.setText( msg_text + (i+1) );
                System.out.println( "Sending message : " + message.getText() );
                topicPublisher.publish( message );
            }

            // send a non-text message to finish the Consumer
            topicPublisher.publish( topicSession.createMessage() );
        } catch( Exception e ) {
            System.out.println( e );
            e.printStackTrace();
        } finally {
            if( topicConnection != null ) {
                try {
                    topicConnection.close();
                } catch( JMSException ee ) {}
            }
        }
        return;
    }


    private static Object lookupJNDI( String name ) throws Exception
    {
        Object        obj=null;

        if( context == null ) {
            try {
                context = new InitialContext();
            } catch( NamingException e ) {
                System.err.println( "lookupJNDI creatng InitialContext failed." );
                e.printStackTrace();
                throw e;
            }
        }
        try {
            obj = context.lookup( name );
        } catch( NamingException e ) {
            System.err.println( "lookupJNDI looking up object failed." );
            e.printStackTrace();
            throw e;
        }
        return obj;
    }
}

