public static void main(String[] args) {
        String destName = null;
        Context jndiContext = null;
        TopicConnectionFactory connectionFactory = null;
        TopicConnection connection = null;
        TopicSession session = null;
        Topic topic = null;
        TopicSubscriber consumer = null;
        TextListener listener = null;
        TextMessage message = null;
        InputStreamReader inputStreamReader = null;
        char answer = '\0';

        if (args.length != 1) {
            System.out.println("Program takes one argument: <dest_name>");
            System.exit(1);
        }

        destName = new String(args[0]);
        System.out.println("Destination name is " + destName);

        /*
         * Create a JNDI API InitialContext object if none exists
         * yet.
         */
        try {
            jndiContext = new InitialContext();
        } catch (NamingException e) {
            System.out.println("Could not create JNDI API context: " +
                e.toString());
            System.exit(1);
        }

        /*
         * Look up connection factory and destination.  If either
         * does not exist, exit.  If you look up a
         * TopicConnectionFactory or a QueueConnectionFactory,
         * program behavior is the same.
         */
        try {
            connectionFactory = (TopicConnectionFactory) jndiContext.lookup(
                    "ConnectionFactory");
            topic = (Topic) jndiContext.lookup(destName);
        } catch (Exception e) {
            System.out.println("JNDI API lookup failed: " + e.toString());
            System.exit(1);
        }

        /*
         * Create connection.
         * Create session from connection; false means session is
         * not transacted.
         * Create consumer.
         * Register message listener (TextListener).
         * Receive text messages from destination.
         * When all messages have been received, type Q to quit.
         * Close connection.
         */
        try {
            connection = connectionFactory.createTopicConnection();
            connection.setClientID("myClientId");
            session = connection.createTopicSession(false, 
                        Session.AUTO_ACKNOWLEDGE);
            
            consumer = session.createDurableSubscriber(topic, "mySubscription");
            // consumer = session.createSubscriber(topic);
            listener = new TextListener();
            consumer.setMessageListener(listener);
            connection.start();
            System.out.println("To end program, type Q or q, " +
                "then <return>");
            inputStreamReader = new InputStreamReader(System.in);

            while (!((answer == 'q') || (answer == 'Q'))) {
                try {
                    answer = (char) inputStreamReader.read();
                } catch (IOException e) {
                    System.out.println("I/O exception: " + e.toString());
                }
            }
        } catch (Exception e) {
            System.out.println("Exception occurred: " + e.toString());
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                }
            }
        }
    }