Menu

Message.getJMSReplyTo() always null

2010-03-17
2013-04-15
  • Nobody/Anonymous

    Hi,

    I am using the JMS functionality in MockRunner.  I'm running a simple test.  Send a message on a queue, MDBean looks for getJMSReplyTo(), and then responds with a status on the destination in getJMSReplyTo().  However,  even though I'm setting jmsReplyTo in the Message object before sending it to the queue, it is always null in the MDB's onMessage operation. 

    Not sure what I'm doing wrong?  Here is my sample Message Driven Bean:

    /****************************************************************************
    
     * TD AMERITRADE CONFIDENTIAL CONTROLLED.  DO NOT COPY OR DISTRIBUTE FURTHER.
     * (c) 2006 Think Tech, Inc.  All rights reserved.
     ****************************************************************************/
    package com.ameritrade.jasom.clientnotification.service.mdb.mockSamples;
    import javax.ejb.EJBException;
    import javax.ejb.MessageDrivenBean;
    import javax.ejb.MessageDrivenContext;
    import javax.jms.Message;
    import javax.jms.MessageListener;
    public class SimpleMDBean implements MessageDrivenBean, MessageListener
    {
        //~ Methods ------------------------------------------------------------------------------------
        public void onMessage(Message message)
        {
            try
            {
                System.out.println("I am a message driven bean, and message reply dest is "
                    + message.getJMSReplyTo());
            }
            catch (Exception exception)
            {
                exception.printStackTrace(System.err);
            }
        }
        public void ejbCreate()
        {
            // do nothing
        }
        
        public void ejbRemove() throws EJBException
        {
            // do nothing
        }
        public void setMessageDrivenContext(MessageDrivenContext arg0) throws EJBException
        {
            // do nothing
        }
    }
    

    And here is my sample Test Case:

    public class TestSimpleMDBean extends TestCase
    {
        //~ Static fields/initializers -----------------------------------------------------------------
        private static final String RESPONSE_QUEUE_NAME = "ResponseQueue";
        private static final String REQUEST_QUEUE_NAME = "RequestQueue";
        private static final String QUEUE_CONNECTION_FACTORY = "SampleConnectionFactory";
        //~ Instance fields ----------------------------------------------------------------------------
        // JNDI context and MockContainer instance used by all tests in this class
        private MockContainer mockContainer;
        //~ Methods ------------------------------------------------------------------------------------
        /**
         * Sets up our mock container, JNDI context and deploy the beans that we need.
         */
        /**
         * Deploys MDB and uses session bean to send a message to MDB.
         */
        public void testMessageBeanWithWrongObjectType() throws Exception
        {
            // MockContextFactory becomes the primary JNDI provider
            MockContextFactory.setAsInitial();
            Context context = new InitialContext();
            
            // create the response queue
            context.rebind( RESPONSE_QUEUE_NAME,  new MockQueue( RESPONSE_QUEUE_NAME) );
                    mockContainer = new MockContainer(context);
            
            
            // We can bind the MDB to the mock REQUEST queue so that
            // it will 'listen' on the queue
            mockContainer.deploy(new MDBDescriptor(
                    QUEUE_CONNECTION_FACTORY,
                    REQUEST_QUEUE_NAME,
                    new SimpleMDBean()));     
            InitialContext ctx = new InitialContext();
            // get references to request and response queues
            Queue requestQueue = (Queue) ctx.lookup(REQUEST_QUEUE_NAME);
            Queue responseQueue = null; 
                responseQueue = (Queue) ctx.lookup(RESPONSE_QUEUE_NAME);
            System.out.println("response queue is " + responseQueue);
            // get message producer so we can send the message
            QueueConnectionFactory queueConnectionFactory =
                (QueueConnectionFactory) ctx.lookup(QUEUE_CONNECTION_FACTORY);
            QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
            QueueSession queueSession =
                queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);       
            MessageProducer producer = queueSession.createProducer(requestQueue);
            // create the message
            ObjectMessage objectMessage = queueSession.createObjectMessage();
            objectMessage.setObject("Test Message!");
            objectMessage.setJMSReplyTo(responseQueue);
            System.out.println("Object message's JMSReplyTo before sending on the mock queue is " + objectMessage.getJMSReplyTo());
            // post the message to the queue
            producer.send(objectMessage);
        }
    }
    
     
  • Alwin Ibba

    Alwin Ibba - 2010-03-17

    It seems that you do not even use Mockrunner or one of Mockrunners JMS mock implementations. You do not show the imports of your test but do you even import a com.mockrunner.* class? This looks like pure MockEJB code (http://mockejb.sourceforge.net/). So you do not use the JMS functionality of Mockrunner but the JMS functionality of MockEJB. JMSReplyTo should work with Mockrunner. Please have a look at the Mockrunner examples.

     
  • Nobody/Anonymous

    Thank you for taking the time to reply.  I do import some com.mockrunner classes, but it is probably not the right ones.

    I appreciate the pointer, and so far as I've been able to figure it out, LOVE this tool.  =)  Thank you for making the world of unit testing a better place. 

     

Log in to post a comment.

MongoDB Logo MongoDB