/* $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.*;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
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;
import javax.naming.InitialContext;
import javax.naming.NamingException;

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

    private static String                 myname = TopicAsynchConsumer.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!!" );
            textListener = new TextListener();
            topicSubscriber.setMessageListener( textListener );
            topicConnection.start();
            System.out.println( "Subscriber ready!!" );

            synchronized( textListener ) {
                try {
                    textListener.wait();
                } catch( Exception e ) {}
            }
        } 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;
    }

    public static class TextListener implements MessageListener
    {
        private boolean        bExit;

        public void onMessage( Message message )
        {
            if( message instanceof TextMessage ) {
                try {
                    System.out.println( "Received message : " + ((TextMessage)message).getText() );
                } catch( JMSException e ) {
                    e.printStackTrace();
                    bExit = true;
                }
            } else {
                bExit = true;
            }

            if( bExit ) {
                synchronized( this ) {
                    try {
                        this.notify();
                    } catch( Exception e ) {}
                }
            }
        }
    }
}

