Menu Search

Drain.java

package org.apache.qpid.example;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.qpid.client.AMQAnyDestination;

public class Drain extends OptionParser
{
       
    static final Option FOREVER = new Option("f",
            "forever",
            "ignore timeout and wait forever",
            null,
            null,
            Boolean.class);

    static final Option COUNT = new Option ("c",
            "count",
            "read c messages, then exit",
            "COUNT",
            "0",
            Integer.class);
                                                

    static 
    {        
        addOption(BROKER);
        addOption(HELP);
        addOption(TIMEOUT);
        addOption(FOREVER);
        addOption(COUNT);
        addOption(CON_OPTIONS);
        addOption(BROKER_OPTIONS);
    }
    
    public Drain(String[] args, String usage, String desc) throws Exception
    {   
        super(args, usage, desc);        
        
        Connection con = createConnection();
        con.start();
        Session ssn = con.createSession(false,Session.AUTO_ACKNOWLEDGE);     
        Destination dest = new AMQAnyDestination(getAddress());
        MessageConsumer consumer = ssn.createConsumer(dest);
        Message msg;
        
        long timeout = -1;        
        int count = 0;
        int i = 0;
        
        if (containsOp(TIMEOUT)) { timeout = Integer.parseInt(getOp(TIMEOUT))*1000; }
        if (containsOp(FOREVER)) { timeout = 0; }
        if (containsOp(COUNT)) { count = Integer.parseInt(getOp(COUNT)); }
        
        while ((msg = consumer.receive(timeout)) != null)
        {
            System.out.println("\n------------- Msg -------------");
            System.out.println(msg);
            System.out.println("-------------------------------\n");

            if (count > 0) {
                if (++i == count) {
                    break;                    
                }               
            }            
        }
        
        ssn.close();
        con.close();
    }
   
    public static void main(String[] args) throws Exception
    {
        String u = "Usage: drain [OPTIONS] 'ADDRESS'";
        String d = "Drains messages from the specified address."; 
            
        new Drain(args,u,d);        
    }
}

Download this file