Search in sources :

Example 11 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class SimpleAmqpQueueMessageIT method testSendFromAmqpAndReceiveBytesMessage.

@Test
public void testSendFromAmqpAndReceiveBytesMessage() 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);
    hdrs.put("JMSType", USER_JMS_TYPE_SETTING);
    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, BYTE_ARRAY);
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    // read-only AMQP-mapped queue
    Queue queue = new RMQDestination(QUEUE_NAME_NON_EXCLUSIVE, null, null, QUEUE_NAME_NON_EXCLUSIVE);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    BytesMessage message = (BytesMessage) queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
    assertNotNull(message, "No message received");
    byte[] bytes = new byte[BYTE_ARRAY.length + 2];
    int bytesIn = message.readBytes(bytes);
    assertEquals(BYTE_ARRAY.length, bytesIn, "Message payload not correct size");
    byte[] bytesTrunc = new byte[bytesIn];
    System.arraycopy(bytes, 0, bytesTrunc, 0, bytesIn);
    assertArrayEquals(BYTE_ARRAY, bytesTrunc, "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(USER_JMS_TYPE_SETTING, 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");
    assertThat(message.getJMSTimestamp()).isZero();
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) HashMap(java.util.HashMap) BytesMessage(javax.jms.BytesMessage) AMQP(com.rabbitmq.client.AMQP) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 12 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class SimpleAmqpQueueMessageIT method testSendToAmqpAndReceiveTextMessage.

@Test
public void testSendToAmqpAndReceiveTextMessage() throws Exception {
    channel.queueDeclare(QUEUE_NAME, // durable
    false, // exclusive
    true, // autoDelete
    true, // options
    null);
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    // write-only AMQP-mapped queue
    Queue queue = new RMQDestination(QUEUE_NAME, "", QUEUE_NAME, null);
    QueueSender queueSender = queueSession.createSender(queue);
    queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    queueSender.setPriority(9);
    TextMessage message = queueSession.createTextMessage(MESSAGE);
    message.setStringProperty(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
    queueSender.send(message);
    queueConn.close();
    GetResponse response = channel.basicGet(QUEUE_NAME, false);
    assertNotNull(response, "basicGet failed to retrieve a response");
    byte[] body = response.getBody();
    assertNotNull(body, "body of response is null");
    {
        Map<String, Object> hdrs = response.getProps().getHeaders();
        assertEquals(new HashSet<>(Arrays.asList("JMSMessageID", "JMSDeliveryMode", "JMSPriority", "JMSTimestamp", USER_STRING_PROPERTY_NAME)), hdrs.keySet(), "Some keys missing");
        assertEquals(9, hdrs.get("JMSPriority"), "Priority wrong");
        // toString is a bit wiffy
        assertEquals("NON_PERSISTENT", hdrs.get("JMSDeliveryMode").toString(), "Delivery mode wrong");
        assertEquals(STRING_PROP_VALUE, hdrs.get(USER_STRING_PROPERTY_NAME).toString(), "String property wrong");
    }
    assertEquals(MESSAGE, new String(body, "UTF-8"), "Message received not identical to message sent");
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) QueueSender(javax.jms.QueueSender) Queue(javax.jms.Queue) GetResponse(com.rabbitmq.client.GetResponse) HashMap(java.util.HashMap) Map(java.util.Map) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 13 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class NackMessageOnRollbackIT method nackParameterTrueRollbackMessageShouldBeInDlxQueue.

@ParameterizedTest
@MethodSource("messageProviderArguments")
public void nackParameterTrueRollbackMessageShouldBeInDlxQueue(MessageProvider messageProvider) throws Exception {
    sendMessage();
    QueueConnection connection = null;
    try {
        connection = connection();
        QueueSession queueSession = connection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
        RMQDestination queue = new RMQDestination();
        queue.setAmqp(true);
        queue.setAmqpQueueName(QUEUE_NAME);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        Message message = messageProvider.message(queueReceiver);
        assertNotNull(message);
        queueSession.rollback();
        // the message has been consumed, no longer in the queue
        queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueReceiver = queueSession.createReceiver(queue);
        message = queueReceiver.receive(1000L);
        assertNull(message);
        // the message has been redelivered to the dead letter queue
        queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queue = new RMQDestination();
        queue.setAmqp(true);
        queue.setAmqpQueueName(QUEUE_DLX_NAME);
        queueReceiver = queueSession.createReceiver(queue);
        message = queueReceiver.receive(1000L);
        assertNotNull(message);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 14 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class NackMessageOnRollbackIT method sendMessage.

private void sendMessage() throws Exception {
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
        RMQDestination queue = new RMQDestination();
        queue.setAmqp(true);
        queue.setAmqpQueueName(QUEUE_NAME);
        queue.setAmqpExchangeName(EXCHANGE_NAME);
        queue.setAmqpRoutingKey(ROUTING_KEY);
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        TextMessage message = queueSession.createTextMessage(MESSAGE);
        queueSender.send(message);
    } finally {
        reconnect();
    }
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination)

Example 15 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class RMQSession method createDurableSubscriber.

/**
 * {@inheritDoc}
 */
@Override
public TopicSubscriber createDurableSubscriber(Topic topic, String name, String messageSelector, boolean noLocal) throws JMSException {
    illegalStateExceptionIfClosed();
    RMQDestination topicDest = (RMQDestination) topic;
    RMQMessageConsumer previousConsumer = this.subscriptions.get(name);
    if (previousConsumer != null) {
        // we are changing subscription, or not, if called with the same topic
        RMQDestination prevDest = previousConsumer.getDestination();
        if (prevDest.equals(topicDest)) {
            if (previousConsumer.isClosed()) {
                // They called TopicSubscriber.close but didn't unsubscribe
                // and they are simply resubscribing with a new one
                logger.warn("Re-subscribing to topic '{}' with name '{}'", topicDest, name);
            } else {
                logger.error("Subscription with name '{}' for topic '{}' already exists", name, topicDest);
                throw new JMSException(String.format("Subscription with name [%s] and topic [%s] already exists", name, topicDest));
            }
        } else {
            logger.warn("Previous subscription with name '{}' was for topic '{}' and is replaced by one for topic '{}'", name, prevDest, topicDest);
            unsubscribe(name);
        }
    }
    // Create a new subscription
    RMQMessageConsumer consumer = (RMQMessageConsumer) createConsumerInternal(topicDest, name, true, messageSelector);
    consumer.setDurable(true);
    consumer.setNoLocal(noLocal);
    this.subscriptions.put(name, consumer);
    return consumer;
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) JMSException(javax.jms.JMSException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException)

Aggregations

RMQDestination (com.rabbitmq.jms.admin.RMQDestination)18 Test (org.junit.jupiter.api.Test)8 Queue (javax.jms.Queue)7 QueueSession (javax.jms.QueueSession)7 TextMessage (javax.jms.TextMessage)6 HashMap (java.util.HashMap)5 QueueSender (javax.jms.QueueSender)5 HashSet (java.util.HashSet)4 AMQP (com.rabbitmq.client.AMQP)3 GetResponse (com.rabbitmq.client.GetResponse)3 MessageConsumer (javax.jms.MessageConsumer)3 QueueReceiver (javax.jms.QueueReceiver)3 Map (java.util.Map)2 BytesMessage (javax.jms.BytesMessage)2 JMSException (javax.jms.JMSException)2 Session (javax.jms.Session)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 Channel (com.rabbitmq.client.Channel)1 RMQDestinationTest (com.rabbitmq.jms.admin.RMQDestinationTest)1