/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.jms.example;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

public class Drain
{
    private static final String USER = "guest";
    private static final String PASSWORD = "guest";
    private static final int DEFAULT_PORT = 5672;
    private static final String DEFAULT_HOST = "localhost";
    private static final int DEFAULT_COUNT = 10000;

    private String _hostname;
    private int _port;
    private int _count;

    public Drain(int count, String hostname, int port)
    {
        _count = count;
        _hostname = hostname;
        _port = port;
    }

    public void runExample()
    {
        try
        {
            // JNDI information can be configured by including an file named jndi.properties
            // on the classpath, containing the "java.naming.factory.initial" configuration
            // and properties configuring required ConnectionFactory and Destination objects.
            // The below is an alternative approach being used only for the examples.
            Properties env = new Properties();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jms.jndi.JmsInitialContextFactory");
            env.put(Context.PROVIDER_URL, ClassLoader.getSystemResource("org/apache/qpid/jms/example/example-jndi.properties").toExternalForm());

            Context context = new InitialContext(env);

            ConnectionFactory factory = (ConnectionFactory) context.lookup("myFactoryLookup");
            Destination queue = (Destination) context.lookup("myQueueLookup");

            Connection connection = factory.createConnection(USER, PASSWORD);
            connection.start();

            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            MessageConsumer messageConsumer = session.createConsumer(queue);

            long start = System.currentTimeMillis();

            int actualCount = 0;
            boolean deductTimeout = false;
            int timeout = 1000;
            for(int i = 1; i <= _count; i++, actualCount++)
            {
                TextMessage message = (TextMessage)messageConsumer.receive(timeout);
                if(message == null)
                {
                    System.out.println("Message not received, stopping");
                    deductTimeout = true;
                    break;
                }
                if(i % 100 == 0)
                {
                    System.out.println("Got message " + i + ":" + message.getText());
                }
            }

            long finish = System.currentTimeMillis();
            long taken = finish - start;
            if(deductTimeout)
            {
                taken -= timeout;
            }
            System.out.println("Received " + actualCount +" messages in " + taken + "ms");

            connection.close();
        }
        catch (Exception exp)
        {
            exp.printStackTrace();
            System.exit(1);
        }
    }

    public static void main(String[] argv) throws Exception
    {
        List<String> switches = new ArrayList<String>();
        List<String> args = new ArrayList<String>();
        for (String s : argv)
        {
            if (s.startsWith("-"))
            {
                switches.add(s);
            }
            else
            {
                args.add(s);
            }
        }

        int count = args.isEmpty() ? DEFAULT_COUNT : Integer.parseInt(args.remove(0));
        String hostname = args.isEmpty() ? DEFAULT_HOST : args.remove(0);
        int port = args.isEmpty() ? DEFAULT_PORT : Integer.parseInt(args.remove(0));

        new Drain(count, hostname, port).runExample();
    }
}
@@ -849,59 +849,8 @@
 e;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A
 impo
@@ -1288,390 +1288,529 @@
 ULT_
-PORT = 5672;%0A    private static final String DEFAULT_HOST = %22localhost%22;%0A    private static final int DEFAULT_COUNT = 10000;%0A%0A    private String _hostname;%0A    private int _port;%0A    private int _count;%0A%0A    public Drain(int count, String hostname, int port)%0A    %7B%0A        _count = count;%0A        _hostname = hostname;%0A        _port = port
+COUNT = 10;%0A%0A    public static void main(String%5B%5D args) throws Exception%0A    %7B%0A        int count = DEFAULT_COUNT;%0A        if( args.length == 0 )%0A        %7B%0A            System.out.println(%22Consuming up to %22 + count + %22 messages.%22);%0A            System.out.println(%22Specify a message count as the program argument if you wish to consume a different amount.%22);%0A        %7D%0A        else%0A        %7B%0A            count = Integer.parseInt(args%5B0%5D)
 ;%0A    
+ 
-%7D%0A%0A
+ 
     
-public void runExample()%0A    %7B
+  System.out.println(%22Consuming up to %22 + count + %22 messages.%22);%0A        %7D%0A
 %0A   
@@ -3198,17 +3198,16 @@
 1; i %3C= 
-_
 count; i
@@ -3425,16 +3425,26 @@
 %22Message
+ %22 + i + %22
  not rec
@@ -3448,16 +3448,31 @@
 received
+ within timeout
 , stoppi
@@ -3473,16 +3473,17 @@
 stopping
+.
 %22);%0A    
@@ -4112,728 +4112,139 @@
     
-exp.printStackTrace();%0A            System.exit(1);%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D argv) throws Exception%0A    %7B%0A        List%3CString%3E switches = new ArrayList%3CString%3E();%0A        List%3CString%3E args = new ArrayList%3CString%3E();%0A        for (String s : argv)%0A        %7B%0A            if (s.startsWith(%22-%22))%0A            %7B%0A                switches.add(s);%0A            %7D%0A            else%0A            %7B%0A                args.add(s);%0A            %7D%0A        %7D%0A%0A        int count = args.isEmpty() ? DEFAULT_COUNT : Integer.parseInt(args.remove(0));%0A        String hostname = args.isEmpty() ? DEFAULT_HOST : args.remove(0);%0A        int port = args.isEmpty() ? DEFAULT_PORT : Integer.parseInt(args.remove(0)
+System.out.println(%22Caught exception, exiting.%22);%0A            exp.printStackTrace(System.out);%0A            System.exit(1
 );%0A
-%0A
         
 new 
@@ -4243,54 +4243,9 @@
     
-new Drain(count, hostname, port).runExample();
+%7D
 %0A   

tidy up and simplify somewhat