import java.util.*;
import javax.ejb.*;
import javax.jms.*;
import javax.naming.*;

public
class rpTestMdb
    implements MessageDrivenBean, MessageListener
{
    private QueueConnection         m_qc    = null;
    private QueueSession            m_qs    = null;
    private QueueSender             m_snd   = null;

    private MessageDrivenContext    m_ctx   = null;


    public rpTestMdb()
    {
    }

    public void setMessageDrivenContext(MessageDrivenContext ctx)
    {
        m_ctx = ctx;
    }

    public void ejbCreate()
    {
    }

    public void ejbRemove()
    {
    }

    public void onMessage(Message msg)
    {
        try
        {
	    /*
	     * Get Connection
	     */
            Context ctx = new InitialContext();
            QueueConnectionFactory qcf = (QueueConnectionFactory)
                ctx.lookup("java:comp/env/jms/QueueConnectionFactory");
            ctx.close();
            m_qc = qcf.createQueueConnection();
            m_qs = m_qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            m_snd = m_qs.createSender(null);

            String txt = ("mdb rcv: " + msg.getJMSMessageID());
            System.out.println(txt + " redel="
                + msg.getJMSRedelivered() + " cnt="
                + msg.getIntProperty("JMSXDeliveryCount"));

            /*
             * Send a response to the message received.
             *
             * Copy the original message ID into new msg's Correlation ID
             * for tracking purposes
             *
             * Set string property "RECIPIENT" to "CLIENT", client only
             * looks for these messages
             */
            Message rmsg = m_qs.createMessage();
            rmsg.setStringProperty("RECIPIENT", "CLIENT");
            rmsg.setIntProperty("count",
                msg.getIntProperty("JMSXDeliveryCount"));
            rmsg.setJMSCorrelationID(msg.getJMSMessageID());

            Destination d = msg.getJMSReplyTo();
            m_snd.send((Queue) d, rmsg);

            System.out.println(txt + " snd: " + rmsg.getJMSMessageID());

	    // Close connection
            m_qc.close();
        }
        catch (Throwable ex)
        {
            ex.printStackTrace();
        }
    }
}
