/* $Id$ */
// =========================================================
// Copyright (C) NEC Corporation 2004,2007
// 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.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

public class QueueAsynchConsumer
{
    private static QueueConnectionFactory queueConnectionFactory;
    private static QueueConnection        queueConnection;
    private static QueueSession           queueSession;
    private static QueueReceiver          queueReceiver;
    private static Queue                  queue;
    private static TextListener           textListener;
    private static ExcpListener           excpListener;
    private static String                 QUEUECONFACT = "QueueConnectionFactory";
    private static String                 queueName;

    private static Object                 exitMonitor = new Object();

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


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

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

        try {
            System.out.print( "looking up " + QUEUECONFACT + " ..." );
            queueConnectionFactory =
                    (QueueConnectionFactory)lookupJNDI( QUEUECONFACT );
            System.out.println( "done!!" );
            System.out.print( "looking up " + queueName + " ..." );
            queue = (Queue)lookupJNDI( queueName );
            System.out.println( "done!!" );

            if( !connect( false ) ) return;
            queueConnection.start();
            System.out.println( "Receiver ready!!" );
        } catch( Exception e ) {
            System.out.println( e );
            e.printStackTrace();
        }

        try {
            synchronized( exitMonitor ) {
                try {
                    exitMonitor.wait();
                } catch( Exception e ) {}
            }
        } catch( Exception e ) {
            System.out.println( e );
            e.printStackTrace();
        } finally {
            if( queueConnection != null ) {
                try {
                    queueConnection.close();
                } catch( JMSException ee ) {}
            }
        }

        return;
    }

    private static boolean reconnect()
    {
        boolean bConnect = false;

        try {
            queueConnection.close();
        } catch( JMSException ee ) {}

        for( int cnt=1; !bConnect; cnt++ ) {
            try {
                Thread.sleep( 3000 );
            } catch( Exception e ) {}
            bConnect = connect( true );
        }
        try {
            queueConnection.start();
        } catch( JMSException ee ) {}
        System.out.println( "Receiver ready!!" );
        return bConnect;
    }

    private static boolean connect( boolean bReconnect )
    {
        try {
            System.out.print( "creating QueueConnection ..." );
            queueConnection =
                    queueConnectionFactory.createQueueConnection();
            System.out.println( "done!!" );
            System.out.print( "setting ExceptionListener ..." );
            excpListener = new ExcpListener();
            queueConnection.setExceptionListener( excpListener );
            System.out.println( "done!!" );
            System.out.print( "creating QueueSession ..." );
            queueSession = queueConnection.createQueueSession( false,
                    Session.AUTO_ACKNOWLEDGE );
            System.out.println( "done!!" );

            System.out.print( "creating Receiver ..." );
            queueReceiver = queueSession.createReceiver( queue );
            System.out.println( "done!!" );
            textListener = new TextListener();
            queueReceiver.setMessageListener( textListener );

            return true;
        } catch( Exception e ) {
            System.out.println();
            if( !bReconnect ) {
                System.out.println( e );
                e.printStackTrace();
            }
            if( queueConnection != null ) {
                try {
                    queueConnection.close();
                } catch( JMSException ee ) {}
            }
            return false;
        }
    }

    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( exitMonitor ) {
                    try {
                        exitMonitor.notify();
                    } catch( Exception e ) {}
                }
            }
        }
    }


    public static class ExcpListener implements ExceptionListener
    {
        public void        onException( JMSException excp )
        {
            System.out.println( "ExceptionListener called!!" );
            System.out.println( "Received JMSException : " + excp );
            try {
                QueueAsynchConsumer.reconnect();
            } catch( Exception e ) {
                e.printStackTrace();
            }
        }
    }
}
