/* $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.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;

public class TopicSynchSubscriber
{
    private static TopicConnectionFactory	topicConnectionFactory;
    private static TopicConnection        topicConnection;
    private static TopicSession           topicSession;
    private static TopicSubscriber        topicSubscriber;
    private static Topic                  topic;
    private static String                 TOPICCONFACT = "TopicConnectionFactory";

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


    public static void main( String[] args )
    {
        String            topicName = null;
        TextMessage        message = null;

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

        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 Subscriber ..." );
            topicSubscriber = topicSession.createSubscriber( topic );
            System.out.println( "done!!" );
            topicConnection.start();
            System.out.println( "Subscriber ready!!" );
            while( true ) {
                Message msg = topicSubscriber.receive();
                if( msg instanceof TextMessage ) {
                    message = (TextMessage)msg;
                    System.out.println( "Received message : " + message.getText() );
                } else {
                    // non-text message is end of messages.
                    break;
                }
            }
        } 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;
    }
}

