Search in sources :

Example 26 with QueueReceiver

use of javax.jms.QueueReceiver in project activemq-artemis by apache.

the class NewQueueRequestorTest method testQueueRequestor.

@Test
public void testQueueRequestor() throws Exception {
    QueueConnection conn1 = null, conn2 = null;
    try {
        Queue queue1 = createQueue(true, "myQueue");
        conn1 = (QueueConnection) cf.createConnection();
        QueueSession sess1 = conn1.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueRequestor requestor = new QueueRequestor(sess1, queue1);
        conn1.start();
        // And the responder
        conn2 = (QueueConnection) cf.createConnection();
        QueueSession sess2 = conn2.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        TestMessageListener listener = new TestMessageListener(sess2);
        QueueReceiver receiver = sess2.createReceiver(queue1);
        receiver.setMessageListener(listener);
        conn2.start();
        Message m1 = sess1.createMessage();
        log.trace("Sending request message");
        TextMessage m2 = (TextMessage) requestor.request(m1);
        assertNotNull(m2);
        assertEquals("This is the response", m2.getText());
        requestor.close();
    } finally {
        conn1.close();
        conn2.close();
    }
}
Also used : QueueRequestor(javax.jms.QueueRequestor) QueueConnection(javax.jms.QueueConnection) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 27 with QueueReceiver

use of javax.jms.QueueReceiver in project wildfly by wildfly.

the class MDBEJBRuntimeNameTestsCase method testMDB.

@Test
@InSequence(1)
public void testMDB() throws Exception {
    final QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("java:/jms/RemoteConnectionFactory");
    final QueueConnection connection = factory.createQueueConnection("guest", "guest");
    try {
        connection.start();
        final QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        final Queue replyDestination = session.createTemporaryQueue();
        final String requestMessage = "test";
        final Message message = session.createTextMessage(requestMessage);
        message.setJMSReplyTo(replyDestination);
        final Destination destination = (Destination) context.lookup(Constants.QUEUE_JNDI_NAME);
        final MessageProducer producer = session.createProducer(destination);
        producer.send(message);
        producer.close();
        // wait for a reply
        final QueueReceiver receiver = session.createReceiver(replyDestination);
        final Message reply = receiver.receive(TimeoutUtil.adjust(1000));
        assertNotNull("Did not receive a reply on the reply queue. Perhaps the original (request) message didn't make it to the MDB?", reply);
        final String result = ((TextMessage) reply).getText();
        assertEquals("Unexpected reply messsage", Constants.REPLY_MESSAGE_PREFIX + requestMessage, result);
    } finally {
        if (connection != null) {
            // just closing the connection will close the session and other related resources (@see javax.jms.Connection)
            safeClose(connection);
        }
    }
}
Also used : Destination(javax.jms.Destination) QueueConnection(javax.jms.QueueConnection) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) QueueConnectionFactory(javax.jms.QueueConnectionFactory) QueueReceiver(javax.jms.QueueReceiver) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.Test) InSequence(org.jboss.arquillian.junit.InSequence)

Example 28 with QueueReceiver

use of javax.jms.QueueReceiver in project jmeter by apache.

the class JMSSampler method clearQueue.

private String clearQueue(Queue queue, SampleResult res) {
    String retVal = null;
    QueueReceiver consumer = null;
    String queueName = null;
    try {
        queueName = queue.getQueueName();
        consumer = session.createReceiver(queue);
        Message deletedMsg = null;
        long deletedMsgCount = 0;
        do {
            deletedMsg = consumer.receive(getTimeoutAsInt());
            if (deletedMsg != null) {
                deletedMsgCount++;
                deletedMsg.acknowledge();
            }
        } while (deletedMsg != null);
        retVal = deletedMsgCount + " message(s) removed using receive timeout:" + getTimeoutAsInt() + "ms";
        res.setResponseMessage(retVal);
    } catch (Exception ex) {
        res.setResponseMessage("Error clearing queue:" + queueName);
        LOGGER.error("Error clearing queue {}", queueName, ex);
        return "Error clearing queue " + queueName + ", message:" + ex.getMessage();
    } finally {
        Utils.close(consumer, LOGGER);
    }
    return retVal;
}
Also used : MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) QueueReceiver(javax.jms.QueueReceiver) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException)

Example 29 with QueueReceiver

use of javax.jms.QueueReceiver in project jmeter by apache.

the class JMSSampler method browseQueueForConsumption.

private String browseQueueForConsumption(Queue queue, String jmsSelector, SampleResult res, StringBuilder buffer, StringBuilder propBuffer) {
    String retVal = null;
    QueueReceiver consumer = null;
    Message reply = null;
    String queueName = null;
    try {
        queueName = queue.getQueueName();
        consumer = session.createReceiver(queue, jmsSelector);
        reply = consumer.receive(getTimeoutAsInt());
        LOGGER.debug("Message: {}", reply);
        if (reply != null) {
            res.setResponseMessage("1 message received successfully");
            res.setResponseHeaders(reply.toString());
            TextMessage msg = (TextMessage) reply;
            retVal = msg.getText();
            extractContent(buffer, propBuffer, msg);
        } else {
            res.setResponseMessage("No message received");
        }
    } catch (Exception ex) {
        res.setResponseMessage("Error browsing queue '" + queueName + "' with selector '" + jmsSelector + "', timeout '" + getTimeout() + "', message:" + ex.getMessage());
        LOGGER.error("Error browsing queue {} with selector {} and configured timeout {}", queueName, jmsSelector, getTimeout(), ex);
    } finally {
        Utils.close(consumer, LOGGER);
    }
    return retVal;
}
Also used : MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) QueueReceiver(javax.jms.QueueReceiver) TextMessage(javax.jms.TextMessage) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException)

Example 30 with QueueReceiver

use of javax.jms.QueueReceiver in project iaf by ibissource.

the class IfsaRequesterSender method getRawReplyMessage.

/**
 * Retrieves a message with the specified correlationId from queue or other channel, but does no processing on it.
 */
private javax.jms.Message getRawReplyMessage(QueueSession session, IFSAQueue queue, TextMessage sentMessage) throws SenderException, TimeoutException {
    String selector = null;
    javax.jms.Message msg = null;
    QueueReceiver replyReceiver = null;
    try {
        replyReceiver = getReplyReceiver(session, sentMessage);
        selector = replyReceiver.getMessageSelector();
        long timeout = getExpiry(queue);
        log.debug(getLogPrefix() + "start waiting at most [" + timeout + "] ms for reply on message using selector [" + selector + "]");
        msg = replyReceiver.receive(timeout);
        if (msg == null) {
            log.info(getLogPrefix() + "received null reply");
        } else {
            log.info(getLogPrefix() + "received reply");
        }
    } catch (Exception e) {
        throw new SenderException(getLogPrefix() + "got exception retrieving reply", e);
    } finally {
        try {
            closeReplyReceiver(replyReceiver);
        } catch (IfsaException e) {
            log.error(getLogPrefix() + "error closing replyreceiver", e);
        }
    }
    if (msg == null) {
        throw new TimeoutException(getLogPrefix() + " timed out waiting for reply using selector [" + selector + "]");
    }
    if (msg instanceof IFSATimeOutMessage) {
        throw new TimeoutException(getLogPrefix() + "received IFSATimeOutMessage waiting for reply using selector [" + selector + "]");
    }
    return msg;
// try {
// TextMessage result = (TextMessage)msg;
// return result;
// } catch (Exception e) {
// throw new SenderException(getLogPrefix()+"reply received for message using selector ["+selector+"] cannot be cast to TextMessage ["+msg.getClass().getName()+"]",e);
// }
}
Also used : QueueReceiver(javax.jms.QueueReceiver) IFSATimeOutMessage(com.ing.ifsa.IFSATimeOutMessage) SenderException(nl.nn.adapterframework.core.SenderException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) JMSException(javax.jms.JMSException) SenderException(nl.nn.adapterframework.core.SenderException) ParameterException(nl.nn.adapterframework.core.ParameterException) TimeoutException(nl.nn.adapterframework.core.TimeoutException)

Aggregations

QueueReceiver (javax.jms.QueueReceiver)56 QueueSession (javax.jms.QueueSession)47 Queue (javax.jms.Queue)40 TextMessage (javax.jms.TextMessage)28 Message (javax.jms.Message)25 QueueSender (javax.jms.QueueSender)25 Test (org.junit.jupiter.api.Test)17 QueueConnection (javax.jms.QueueConnection)13 Serializable (java.io.Serializable)11 JMSException (javax.jms.JMSException)10 Test (org.junit.Test)10 QueueConnectionFactory (javax.jms.QueueConnectionFactory)6 BytesMessage (javax.jms.BytesMessage)5 InitialContext (javax.naming.InitialContext)5 Destination (javax.jms.Destination)4 MessageProducer (javax.jms.MessageProducer)4 NamingException (javax.naming.NamingException)4 RMQDestination (com.rabbitmq.jms.admin.RMQDestination)3 Enumeration (java.util.Enumeration)3 HashMap (java.util.HashMap)3