activemq-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dain Sundstrom <d...@iq80.com>
Subject Re: Reply to message not being recieved...
Date Sat, 03 Mar 2007 07:25:24 GMT
Cleaned up the code... here is the Test and onMessage of the MDB.   I  
don't care about efficiency right now, but just want it to work  
(every time).

TIA,

-dain


public class MdbConnectionFactoryTests extends BasicMdbTestClient {
     protected ConnectionFactory connectionFactory;


     protected void setUp() throws Exception {
         connectionFactory = TestManager.getJms().getConnectionFactory 
();
     }

     public void testRpc() throws Exception {
         sendRequest();
         String returnValue = getResponse();
         assertEquals("test-cheese", returnValue);
     }

     private void sendRequest() throws JMSException {
         Connection connection = null;
         Session session = null;
         MessageProducer producer = null;
         try {
             connection = connectionFactory.createConnection();

             // create request
             Map<String, Object> request = new TreeMap<String, Object> 
();
             request.put("args", new Object[]{"cheese"});

             // initialize session
             session = connection.createSession(false,  
Session.AUTO_ACKNOWLEDGE);
             Destination requestQueue = session.createQueue("request");
             Destination responseQueue = session.createQueue 
("response");

             // Create a request messages
             ObjectMessage requestMessage =  
session.createObjectMessage();
             requestMessage.setJMSReplyTo(responseQueue);
             requestMessage.setObject((Serializable) request);

             // Send the request message
             producer = session.createProducer(requestQueue);
             producer.send(requestMessage);

             System.out.println("\n" +  
"***************************************\n" +
                     "Sent request message: " + requestMessage + "\n" +
                     "            to queue: " + requestQueue + "\n" +
                     "***************************************\n\n");
         } finally {
             MdbUtil.close(producer);
             MdbUtil.close(session);
             MdbUtil.close(connection);
         }
     }

     private String getResponse() throws JMSException {
         // wait for the response message
         MessageConsumer consumer = null;

         Connection connection = null;
         Session session = null;
         try {
             connection = connectionFactory.createConnection();

             // initialize session
             session = connection.createSession(false,  
Session.AUTO_ACKNOWLEDGE);
             Destination responseQueue = session.createQueue 
("response");

             // create consumer
             consumer = session.createConsumer(responseQueue);
             System.out.println("\n" +  
"***************************************\n" +
                     "Listening for response at : " + responseQueue +  
"\n" +
                     "***************************************\n\n");

             // wait for mesage
             Message message = consumer.receive(10000);

             // verify message
             assertNotNull("Did not get a response message", message);
             assertTrue("Response message is not an ObjectMessage",  
message instanceof ObjectMessage);
             ObjectMessage responseMessage = (ObjectMessage) message;
             Serializable object = responseMessage.getObject();
             assertNotNull("Response ObjectMessage contains a null  
object");
             assertTrue("Response ObjectMessage does not contain an  
instance of Map", object instanceof Map);
             Map response = (Map) object;

             // process results
             String returnValue = (String) response.get("return");
             return returnValue;
         } finally {
             MdbUtil.close(consumer);
             MdbUtil.close(session);
             MdbUtil.close(connection);
         }
     }
}




     public void onMessage(Message message) {
         System.out.println("\n" +
                 "***************************************\n" +
                 "Got message: " + message + "\n" +
                 "***************************************\n\n");

         // if we got a dummy (non ObjectMessage) return
         if (!(message instanceof ObjectMessage)) return;

         Connection connection = null;
         Session session = null;
         MessageProducer producer = null;
         try {
             // process request
             ObjectMessage requestMessage = (ObjectMessage) message;
             Map request = (Map) requestMessage.getObject();
             Object[] args = (Object[]) request.get("args");
             String returnValue = "test-" + args[0];

             // create response map
             Map<String, Object> response = new TreeMap<String,  
Object>();
             response.put("return", returnValue);

             // create connection
             ConnectionFactory connectionFactory = new  
ActiveMQConnectionFactory("tcp://localhost:61616");
             connection = connectionFactory.createConnection();

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

             // create response message
             ObjectMessage responseMessage =  
session.createObjectMessage();
             responseMessage.setObject((Serializable) response);

             // send response message
             Destination responseQueue = session.createQueue 
("response");
             producer = session.createProducer(responseQueue);
             producer.send(responseMessage);

             System.out.println("\n" +
                     "***************************************\n" +
                     "Sent response message: " + responseMessage +  
"\n" +
                     "             to queue: " + responseQueue + "\n" +
                     "***************************************\n\n");

         } catch (Throwable e) {
             e.printStackTrace();
         } finally {
             MdbUtil.close(producer);
             MdbUtil.close(session);
             MdbUtil.close(connection);
         }
     }






Mime
View raw message