Search in sources :

Example 81 with MessageConsumer

use of javax.jms.MessageConsumer in project tomee by apache.

the class MdbTest method createSender.

private void createSender() throws JMSException {
    Connection connection = null;
    Session session = null;
    MessageProducer producer = null;
    MessageConsumer consumer = null;
    try {
        connection = connectionFactory.createConnection();
        connection.start();
        // create request
        final Map<String, Object> request = new TreeMap<String, Object>();
        request.put("args", new Object[] { "cheese" });
        // create a new temp response queue
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final Destination responseQueue = session.createTemporaryQueue();
        // Create a request messages
        final ObjectMessage requestMessage = session.createObjectMessage();
        requestMessage.setJMSReplyTo(responseQueue);
        requestMessage.setObject((Serializable) request);
        // Send the request message
        producer = session.createProducer(session.createQueue(REQUEST_QUEUE_NAME));
        producer.send(requestMessage);
        // wait for the response message
        consumer = session.createConsumer(responseQueue);
        final Message message = consumer.receive(30000);
        // verify message
        assertNotNull("Did not get a response message", message);
        assertTrue("Response message is not an ObjectMessage", message instanceof ObjectMessage);
        final ObjectMessage responseMessage = (ObjectMessage) message;
        final Serializable object = responseMessage.getObject();
        assertNotNull("Response ObjectMessage contains a null object");
        assertTrue("Response ObjectMessage does not contain an instance of Map", object instanceof Map);
        final Map<String, String> response = (Map<String, String>) object;
        // process results
        final String returnValue = (String) response.get("return");
        assertEquals("test-cheese", returnValue);
    } finally {
        MdbUtil.close(consumer);
        MdbUtil.close(producer);
        MdbUtil.close(session);
        MdbUtil.close(connection);
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Serializable(java.io.Serializable) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) TreeMap(java.util.TreeMap) ObjectMessage(javax.jms.ObjectMessage) MessageProducer(javax.jms.MessageProducer) TreeMap(java.util.TreeMap) Map(java.util.Map) Session(javax.jms.Session)

Example 82 with MessageConsumer

use of javax.jms.MessageConsumer in project tomee by apache.

the class MdbConnectionFactoryTests method test02_directRpc.

public void test02_directRpc() throws Exception {
    final Connection connection = createConnection();
    Session session = null;
    MessageProducer producer = null;
    MessageConsumer consumer = null;
    try {
        // create request
        final Map<String, Object> request = new TreeMap<String, Object>();
        request.put("method", "businessMethod(java.lang.String)");
        request.put("args", new Object[] { "cheese" });
        // initialize session
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final Destination requestQueue = session.createQueue("BasicMdb");
        final Destination responseQueue = session.createTemporaryQueue();
        // Create a request messages
        final 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" +
        //                    "         request map: " + request + "\n" +
        //                    "            to queue: " + requestQueue + "\n" +
        //                    "***************************************\n\n");
        // create consumer
        consumer = session.createConsumer(responseQueue);
        //            System.out.println("\n" + "***************************************\n" +
        //                    "Listening for response at : " + responseQueue + "\n" +
        //                    "***************************************\n\n");
        // wait for response mesage
        final Message message = consumer.receive(1000);
        // verify message
        assertNotNull("Did not get a response message", message);
        assertTrue("Response message is not an ObjectMessage", message instanceof ObjectMessage);
        final ObjectMessage responseMessage = (ObjectMessage) message;
        final Serializable object = responseMessage.getObject();
        assertNotNull("Response ObjectMessage contains a null object");
        assertTrue("Response ObjectMessage does not contain an instance of Map", object instanceof Map);
        final Map response = (Map) object;
        // process results
        if (response.containsKey("exception")) {
            throw (Exception) response.get("return");
        }
        final String returnValue = (String) response.get("return");
        assertEquals("eseehc", returnValue);
    } finally {
        MdbUtil.close(producer);
        MdbUtil.close(session);
        MdbUtil.close(connection);
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Serializable(java.io.Serializable) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) TreeMap(java.util.TreeMap) ObjectMessage(javax.jms.ObjectMessage) MessageProducer(javax.jms.MessageProducer) TreeMap(java.util.TreeMap) Map(java.util.Map) Session(javax.jms.Session)

Aggregations

MessageConsumer (javax.jms.MessageConsumer)82 Session (javax.jms.Session)53 Message (javax.jms.Message)40 MessageProducer (javax.jms.MessageProducer)40 Test (org.junit.Test)39 Connection (javax.jms.Connection)38 TextMessage (javax.jms.TextMessage)38 ConnectionFactory (javax.jms.ConnectionFactory)14 JMSException (javax.jms.JMSException)14 Destination (javax.jms.Destination)12 Queue (javax.jms.Queue)8 TemporaryQueue (javax.jms.TemporaryQueue)8 ObjectMessage (javax.jms.ObjectMessage)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 Serializable (java.io.Serializable)4 Map (java.util.Map)4 TreeMap (java.util.TreeMap)4 MessageListener (javax.jms.MessageListener)4 HornetQMixIn (org.switchyard.component.test.mixins.hornetq.HornetQMixIn)4 ArrayList (java.util.ArrayList)3