Java Code Examples for javax.jms.ObjectMessage#setJMSReplyTo()

The following examples show how to use javax.jms.ObjectMessage#setJMSReplyTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ForeignMessageTest.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testSendForeignMessage() throws Exception
{
    final Destination replyTo = createQueue(getTestName() + "_replyTo");
    final Queue queue = createQueue(getTestName());
    final Connection connection = getConnection();
    try
    {
        final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final String jmsType = "TestJmsType";
        final String correlationId = "testCorrelationId";
        final ObjectMessage message = session.createObjectMessage();
        final ObjectMessage foreignMessage =
                Reflection.newProxy(ObjectMessage.class, new AbstractInvocationHandler()
                {
                    @Override
                    protected Object handleInvocation(final Object proxy, final Method method, final Object[] args)
                            throws Throwable
                    {
                        return method.invoke(message, args);
                    }
                });

        foreignMessage.setJMSCorrelationID(correlationId);
        foreignMessage.setJMSType(jmsType);
        foreignMessage.setJMSReplyTo(replyTo);
        Serializable payload = UUID.randomUUID();
        foreignMessage.setObject(payload);

        final MessageConsumer consumer = session.createConsumer(queue);
        final MessageProducer producer = session.createProducer(queue);
        producer.send(foreignMessage);

        connection.start();

        Message receivedMessage = consumer.receive(getReceiveTimeout());
        assertTrue("ObjectMessage was not received ", receivedMessage instanceof ObjectMessage);
        assertEquals("JMSCorrelationID mismatch",
                     foreignMessage.getJMSCorrelationID(),
                     receivedMessage.getJMSCorrelationID());
        assertEquals("JMSType mismatch", foreignMessage.getJMSType(), receivedMessage.getJMSType());
        assertEquals("JMSReply To mismatch", foreignMessage.getJMSReplyTo(), receivedMessage.getJMSReplyTo());
        assertEquals("JMSMessageID mismatch", foreignMessage.getJMSMessageID(), receivedMessage.getJMSMessageID());
        assertEquals("JMS Default priority should be default",
                     Message.DEFAULT_PRIORITY,
                     receivedMessage.getJMSPriority());
        assertEquals("Message payload not as expected", payload, ((ObjectMessage) receivedMessage).getObject());
    }
    finally
    {
        connection.close();
    }
}
 
Example 2
Source File: MdbConnectionFactoryTests.java    From tomee with Apache License 2.0 4 votes vote down vote up
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);
        }
    }
 
Example 3
Source File: MdbProxy.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    final Session tempSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    if (tempSession == null) throw new IllegalStateException("Proxy has been destroyed");

    // create request
    final Map<String, Object> request = new TreeMap<String, Object>();
    final String signature = MdbUtil.getSignature(method);
    request.put("method", signature);
    request.put("args", args);

    // create a new temp response queue and consumer
    // this is very inefficient, but eliminates a whole class of errors
    final String correlationId = UUID.randomUUID().toString();

    //final Destination responseQueue = tempSession.createTemporaryQueue();
    final Destination responseQueue = tempSession.createQueue(correlationId);

    // create a producer and consumers used to send requests and receive response
    MessageConsumer consumer = null;
    try {

        // Wait for a message
        // Again this is quite inefficient
        consumer = tempSession.createConsumer(responseQueue);

        // Create a message
        final ObjectMessage requestMessage = tempSession.createObjectMessage();
        requestMessage.setJMSReplyTo(responseQueue);
        requestMessage.setJMSCorrelationID(correlationId);
        requestMessage.setObject((Serializable) request);

        // Send the request
        getProducer().send(requestMessage);

        // wait for the message
        final Message message = consumer.receive(MdbProxy.MdbInvocationHandler.MAX_RESPONSE_WAIT);

        // verify message
        if (message == null)
            throw new NullPointerException("Did not get a response within " + MdbProxy.MdbInvocationHandler.MAX_RESPONSE_WAIT + "ms");
        if (!correlationId.equals(message.getJMSCorrelationID())) {
            throw new IllegalStateException("Received a response message with the wrong correlation id");
        }
        if (!(message instanceof ObjectMessage))
            throw new IllegalArgumentException("Expected a ObjectMessage response but got a " + message.getClass().getName());
        final ObjectMessage resMessage = (ObjectMessage) message;
        final Serializable object = resMessage.getObject();
        if (object == null) throw new NullPointerException("object in ObjectMessage is null");
        if (!(object instanceof Map)) {
            if (ObjectMessage.class.isInstance(message))
                throw new IllegalArgumentException("Expected a Map contained in the ObjectMessage response but got a " + object.getClass().getName());
        }
        final Map response = (Map) object;

        // process results
        final boolean exception = response.containsKey("exception");
        final Object returnValue = response.get("return");
        if (exception) {
            throw (Throwable) returnValue;
        }
        return returnValue;
    } finally {
        MdbUtil.close(consumer);
        MdbUtil.close(tempSession);
    }
}
 
Example 4
Source File: MdbProxy.java    From tomee with Apache License 2.0 4 votes vote down vote up
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    final Session session = getSession();
    if (session == null) throw new IllegalStateException("Proxy has been destroyed");

    // create request
    final Map<String, Object> request = new TreeMap<>();
    final String signature = MdbUtil.getSignature(method);
    request.put("method", signature);
    request.put("args", args);

    // create a new temp response queue and consumer
    // this is very inefficient, but eliminates a whole class of errors
    final Destination responseQueue = session.createTemporaryQueue();

    // Create a messages
    final ObjectMessage reqMessage = session.createObjectMessage();
    reqMessage.setJMSReplyTo(responseQueue);
    final String correlationId = UUID.randomUUID().toString();
    reqMessage.setJMSCorrelationID(correlationId);
    reqMessage.setObject((Serializable) request);

    // Send the request
    getProducer().send(reqMessage);

    // Wait for a message
    // Again this is quite inefficient
    final MessageConsumer consumer = session.createConsumer(responseQueue);
    try {
        // wait for the message
        final Message message = consumer.receive(MAX_RESPONSE_WAIT);

        // verify message
        if (message == null) throw new NullPointerException("message is null");
        if (!correlationId.equals(message.getJMSCorrelationID())) {
            throw new IllegalStateException("Recieved a response message with the wrong correlation id");
        }
        if (!(message instanceof ObjectMessage))
            throw new IllegalArgumentException("Expected a ObjectMessage response but got a " + message.getClass().getName());
        final ObjectMessage resMessage = (ObjectMessage) message;
        final Serializable object = resMessage.getObject();
        if (object == null) throw new NullPointerException("object in ObjectMessage is null");
        if (!(object instanceof Map)) {
            if (message instanceof ObjectMessage)
                throw new IllegalArgumentException("Expected a Map contained in the ObjectMessage response but got a " + object.getClass().getName());
        }
        final Map response = (Map) object;

        // process results
        final boolean exception = response.containsKey("exception");
        final Object returnValue = response.get("return");
        if (exception) {
            throw (Throwable) returnValue;
        }
        return returnValue;
    } finally {
        MdbUtil.close(consumer);
    }
}
 
Example 5
Source File: MdbTest.java    From tomee with Apache License 2.0 4 votes vote down vote up
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<>();
        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);
    }
}
 
Example 6
Source File: JmsTest.java    From tomee with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private synchronized void createSender(final Connection connection, final Destination requestQueue) throws JMSException {
    Session session = null;
    MessageProducer producer = null;
    MessageConsumer consumer = null;
    try {
        // create request
        final Map<String, Object> request = new TreeMap<>();
        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(requestQueue);
        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 = response.get("return");
        assertEquals("test-cheese", returnValue);
    } finally {
        MdbUtil.close(consumer);
        MdbUtil.close(producer);
        MdbUtil.close(session);
    }
}