Search in sources :

Example 1 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class SimpleAmqpQueueMessageIT method testSendFromAmqpAndReceiveTextMessage.

@Test
public void testSendFromAmqpAndReceiveTextMessage() throws Exception {
    channel.queueDeclare(QUEUE_NAME_NON_EXCLUSIVE, // durable
    false, // non-exclusive
    false, // autoDelete
    true, // options
    null);
    Map<String, Object> hdrs = new HashMap<String, Object>();
    hdrs.put(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
    // To signal a JMS TextMessage
    hdrs.put("JMSType", "TextMessage");
    hdrs.put("JMSPriority", 21);
    hdrs.put("JMSDeliveryMode", 2);
    hdrs.put("DummyProp", 42);
    hdrs.put("rmq.jms.silly", "silly attempt");
    AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().deliveryMode(1).priority(6).headers(hdrs).build();
    channel.basicPublish("", QUEUE_NAME_NON_EXCLUSIVE, props, MESSAGE.getBytes("UTF-8"));
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    // read-only AMQP-mapped queue
    Queue queue = (Queue) new RMQDestination(QUEUE_NAME_NON_EXCLUSIVE, null, null, QUEUE_NAME_NON_EXCLUSIVE);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    TextMessage message = (TextMessage) queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
    assertNotNull(message, "No message received");
    assertEquals(MESSAGE, message.getText(), "Payload doesn't match");
    // override should work
    assertEquals(21, message.getJMSPriority(), "Priority incorrect");
    // override should fail
    assertEquals(1, message.getJMSDeliveryMode(), "Delivery mode incorrect");
    // override should work
    assertEquals("TextMessage", message.getJMSType(), "JMSType not set correctly");
    Enumeration<?> propNames = message.getPropertyNames();
    Set<String> propNameSet = new HashSet<String>();
    while (propNames.hasMoreElements()) {
        propNameSet.add((String) propNames.nextElement());
    }
    assertEquals(new HashSet<>(Arrays.asList(USER_STRING_PROPERTY_NAME, "DummyProp")), propNameSet, "Headers not set correctly");
    assertEquals(STRING_PROP_VALUE, message.getStringProperty(USER_STRING_PROPERTY_NAME), "String property not transferred");
    assertEquals("42", message.getStringProperty("DummyProp"), "Numeric property not transferred");
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) HashMap(java.util.HashMap) AMQP(com.rabbitmq.client.AMQP) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 2 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class SimpleBrowseQueueMessageIT method messageTestBase.

private void messageTestBase(MessageTestType mtt, String selector, int numExpected) throws Exception {
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        drainQueue(queueSession, queue);
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        queueSender.send(mtt.gen(queueSession, (Serializable) queue));
    } finally {
        reconnect();
    }
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    {
        // Browse queue before receiving message
        QueueBrowser queueBrowser = queueSession.createBrowser(queue, selector);
        Enumeration<?> e = queueBrowser.getEnumeration();
        int numE = 0;
        Message msg = null;
        while (e.hasMoreElements()) {
            ++numE;
            msg = (Message) e.nextElement();
            mtt.check(msg, (Serializable) queue);
        }
        assertEquals(numExpected, numE, "Wrong number of messages on browse queue");
    }
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    mtt.check(queueReceiver.receive(TEST_RECEIVE_TIMEOUT), (Serializable) queue);
}
Also used : Serializable(java.io.Serializable) Enumeration(java.util.Enumeration) Message(javax.jms.Message) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) QueueBrowser(javax.jms.QueueBrowser)

Example 3 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class SimpleQueueMessageIT method messageTestBase.

private void messageTestBase(MessageTestType mtt) throws Exception {
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        drainQueue(queueSession, queue);
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        queueSender.send(mtt.gen(queueSession, (Serializable) queue));
    } finally {
        reconnect();
    }
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    mtt.check(queueReceiver.receive(TEST_RECEIVE_TIMEOUT), (Serializable) queue);
}
Also used : Serializable(java.io.Serializable) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession)

Example 4 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class MixedConsumerCheckIT method testAsyncThenSyncReceive.

/**
 * Listener with a Consumer. Uses message listener (asynchronous receive).
 * Try to receive() from different queue on same session.
 * @throws javax.jms.IllegalStateException if test succeeds.
 */
@Test
public void testAsyncThenSyncReceive() throws Exception {
    assertThrows(javax.jms.IllegalStateException.class, () -> {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        Queue queueTwo = queueSession.createQueue(QUEUE_NAME_TWO);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        QueueReceiver queueReceiverTwo = queueSession.createReceiver(queueTwo);
        TestMessageListener listener = new TestMessageListener();
        queueReceiver.setMessageListener(listener);
        // should throw an exception
        queueReceiverTwo.receiveNoWait();
    });
}
Also used : QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) Test(org.junit.jupiter.api.Test)

Example 5 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class ReceiveNoWaitIT method messageTestBase.

private void messageTestBase(MessageTestType mtt) throws Exception {
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        drainQueue(queueSession, queue);
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        queueSender.send(mtt.gen(queueSession, (Serializable) queue));
    } finally {
        reconnect();
    }
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    mtt.check(queueReceiver.receiveNoWait(), (Serializable) queue);
}
Also used : Serializable(java.io.Serializable) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession)

Aggregations

QueueReceiver (javax.jms.QueueReceiver)49 QueueSession (javax.jms.QueueSession)41 Queue (javax.jms.Queue)34 TextMessage (javax.jms.TextMessage)24 Message (javax.jms.Message)22 QueueSender (javax.jms.QueueSender)22 Test (org.junit.jupiter.api.Test)13 Serializable (java.io.Serializable)10 QueueConnection (javax.jms.QueueConnection)10 Test (org.junit.Test)10 JMSException (javax.jms.JMSException)9 QueueConnectionFactory (javax.jms.QueueConnectionFactory)6 BytesMessage (javax.jms.BytesMessage)5 InitialContext (javax.naming.InitialContext)5 Destination (javax.jms.Destination)4 MapMessage (javax.jms.MapMessage)4 MessageProducer (javax.jms.MessageProducer)4 ObjectMessage (javax.jms.ObjectMessage)4 NamingException (javax.naming.NamingException)4 IfsaException (nl.nn.adapterframework.extensions.ifsa.IfsaException)4