import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.StreamMessage;


public final
class Qrecv
{
    public static void main(String[] argv)
    {
        try
        {
            if ((argv.length != 3)
                && (argv.length != 5))
            {
                System.err.println("Usage: Qrecv <qcfN> <qN>"
                    + " <nmsgs> [<user> <pass>]]");
                return;
            }

            int argc = 0;

            String qcfN  = argv[argc++];
            String qN    = argv[argc++];
            int    nmsgs = Integer.parseInt(argv[argc++]);

            String user  = null;
            String pass  = null;

            if (argv.length == 5)
            {
                user    = argv[argc++];
                pass    = argv[argc++];
            }

            doit(qcfN, qN, user, pass, nmsgs, false);
            //doit(qcfN, qN, user, pass, nmsgs, true);
            System.out.println("\n\n");
        }
        catch (Exception e)
        {
            e.printStackTrace();

        }

    }

    public static void doit(String qcfN, String qN,
        String user, String pass, int nmsgs, boolean transacted)
    {
        try
        {
            QueueConnectionFactory qcf =
                (QueueConnectionFactory) Oc4jjmsDemo.getQCF(qcfN);
            Queue q = (Queue) Oc4jjmsDemo.getQ(qN);

            do_receive(qcf, q, user, pass, nmsgs, transacted);
        }
        catch (JMSException e)
        {
            e.printStackTrace();
            if (e.getLinkedException() != null)
            {
                e.getLinkedException().printStackTrace();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private static void do_receive(QueueConnectionFactory qcf, Queue q,
        String user, String pass, int nmsgs, boolean transacted)
        throws Exception
    {
        System.out.println("starting do_receive");

        QueueConnection qc  = Oc4jjmsDemo.getQC(qcf, user, pass);
        QueueSession    qs  = qc.createQueueSession(transacted,
            Session.AUTO_ACKNOWLEDGE);
        QueueReceiver   rcv = qs.createReceiver(q);


        qc.start();

        int count = 0;
        while (true)
        {
            Message msg = rcv.receiveNoWait();
            if (msg == null)
            {
                break;
            }


            System.out.println("msg:"
                + " id=" + msg.getJMSMessageID());
            ++count;
        }

        if (transacted)
            qs.commit();

        if (nmsgs != count)
        {
            System.out.println("expected: " + nmsgs + " found: " + count);
        }

        rcv.close();
        qs.close();
        qc.close();

        System.out.println("ending do_receive");
    }
}
