Search in sources :

Example 6 with QueueInfo

use of com.rabbitmq.http.client.domain.QueueInfo in project spring-amqp by spring-projects.

the class FixedReplyQueueDeadLetterTests method testQueueArgs3.

@Test
void testQueueArgs3() throws MalformedURLException, URISyntaxException, InterruptedException {
    Client client = new Client(brokerRunning.getAdminUri(), brokerRunning.getAdminUser(), brokerRunning.getAdminPassword());
    QueueInfo queue = await().until(() -> client.getQueue("/", "all.args.3"), que -> que != null);
    Map<String, Object> arguments = queue.getArguments();
    assertThat(arguments.get("x-message-ttl")).isEqualTo(1000);
    assertThat(arguments.get("x-expires")).isEqualTo(200_000);
    assertThat(arguments.get("x-max-length")).isEqualTo(42);
    assertThat(arguments.get("x-max-length-bytes")).isEqualTo(10_000);
    assertThat(arguments.get("x-overflow")).isEqualTo("reject-publish");
    assertThat(arguments.get("x-dead-letter-exchange")).isEqualTo("reply.dlx");
    assertThat(arguments.get("x-dead-letter-routing-key")).isEqualTo("reply.dlrk");
    assertThat(arguments.get("x-max-priority")).isEqualTo(4);
    assertThat(arguments.get("x-queue-mode")).isEqualTo("lazy");
    assertThat(arguments.get(Queue.X_QUEUE_LEADER_LOCATOR)).isEqualTo(LeaderLocator.random.getValue());
    ExchangeInfo exchange = client.getExchange("/", "dlx.test.requestEx");
    assertThat(exchange.getArguments().get("alternate-exchange")).isEqualTo("alternate");
}
Also used : QueueInfo(com.rabbitmq.http.client.domain.QueueInfo) Client(com.rabbitmq.http.client.Client) ExchangeInfo(com.rabbitmq.http.client.domain.ExchangeInfo) Test(org.junit.jupiter.api.Test)

Example 7 with QueueInfo

use of com.rabbitmq.http.client.domain.QueueInfo in project spring-amqp by spring-projects.

the class EnableRabbitIntegrationTests method deadLetterOnDefaultExchange.

@Test
public void deadLetterOnDefaultExchange() {
    this.rabbitTemplate.convertAndSend("amqp656", "foo");
    assertThat(this.rabbitTemplate.receiveAndConvert("amqp656dlq", 10000)).isEqualTo("foo");
    try {
        Client rabbitRestClient = new Client("http://localhost:15672/api/", "guest", "guest");
        QueueInfo amqp656 = rabbitRestClient.getQueue("/", "amqp656");
        if (amqp656 != null) {
            assertThat(amqp656.getArguments().get("test-empty")).isEqualTo("");
            assertThat(amqp656.getArguments().get("test-null")).isEqualTo("undefined");
        }
    } catch (Exception e) {
    // empty
    }
}
Also used : QueueInfo(com.rabbitmq.http.client.domain.QueueInfo) Client(com.rabbitmq.http.client.Client) MethodArgumentNotValidException(org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException) AmqpRejectAndDontRequeueException(org.springframework.amqp.AmqpRejectAndDontRequeueException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) ListenerExecutionFailedException(org.springframework.amqp.rabbit.support.ListenerExecutionFailedException) Test(org.junit.jupiter.api.Test)

Example 8 with QueueInfo

use of com.rabbitmq.http.client.domain.QueueInfo in project spring-cloud-stream by spring-cloud.

the class RabbitBinderTests method testAutoBindDLQManualAcks.

@Test
public void testAutoBindDLQManualAcks() throws Exception {
    RabbitTestBinder binder = getBinder();
    ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
    consumerProperties.getExtension().setPrefix(TEST_PREFIX);
    consumerProperties.getExtension().setAutoBindDlq(true);
    consumerProperties.setMaxAttempts(2);
    consumerProperties.getExtension().setDurableSubscription(true);
    consumerProperties.getExtension().setAcknowledgeMode(AcknowledgeMode.MANUAL);
    BindingProperties bindingProperties = createConsumerBindingProperties(consumerProperties);
    DirectChannel moduleInputChannel = createBindableChannel("input", bindingProperties);
    moduleInputChannel.setBeanName("dlqTestManual");
    Client client = new Client(adminUri());
    moduleInputChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            // Wait until the unacked state is reflected in the admin
            QueueInfo info = client.getQueue("/", TEST_PREFIX + "dlqTestManual.default");
            int n = 0;
            while (n++ < 100 && info.getMessagesUnacknowledged() < 1L) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                info = client.getQueue("/", TEST_PREFIX + "dlqTestManual.default");
            }
            throw new RuntimeException("foo");
        }
    });
    Binding<MessageChannel> consumerBinding = binder.bindConsumer("dlqTestManual", "default", moduleInputChannel, consumerProperties);
    RabbitTemplate template = new RabbitTemplate(this.rabbitTestSupport.getResource());
    template.convertAndSend("", TEST_PREFIX + "dlqTestManual.default", "foo");
    int n = 0;
    while (n++ < 100) {
        Object deadLetter = template.receiveAndConvert(TEST_PREFIX + "dlqTestManual.default.dlq");
        if (deadLetter != null) {
            assertThat(deadLetter).isEqualTo("foo");
            break;
        }
        Thread.sleep(100);
    }
    assertThat(n).isLessThan(100);
    n = 0;
    QueueInfo info = client.getQueue("/", TEST_PREFIX + "dlqTestManual.default");
    while (n++ < 100 && info.getMessagesUnacknowledged() > 0L) {
        Thread.sleep(100);
        info = client.getQueue("/", TEST_PREFIX + "dlqTestManual.default");
    }
    assertThat(info.getMessagesUnacknowledged()).isEqualTo(0L);
    consumerBinding.unbind();
    ApplicationContext context = TestUtils.getPropertyValue(binder, "binder.provisioningProvider.autoDeclareContext", ApplicationContext.class);
    assertThat(context.containsBean(TEST_PREFIX + "dlqTestManual.default.binding")).isFalse();
    assertThat(context.containsBean(TEST_PREFIX + "dlqTestManual.default")).isFalse();
    assertThat(context.containsBean(TEST_PREFIX + "dlqTestManual.default.dlq.binding")).isFalse();
    assertThat(context.containsBean(TEST_PREFIX + "dlqTestManual.default.dlq")).isFalse();
    verifyAutoDeclareContextClear(binder);
}
Also used : QueueInfo(com.rabbitmq.http.client.domain.QueueInfo) RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) RabbitConsumerProperties(org.springframework.cloud.stream.binder.rabbit.properties.RabbitConsumerProperties) MessageHandler(org.springframework.messaging.MessageHandler) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) Client(com.rabbitmq.http.client.Client) Test(org.junit.jupiter.api.Test)

Example 9 with QueueInfo

use of com.rabbitmq.http.client.domain.QueueInfo in project spring-cloud-stream by spring-cloud.

the class RabbitBinderCleanerTests method testCleanStream.

@Test
public void testCleanStream() {
    final RabbitBindingCleaner cleaner = new RabbitBindingCleaner();
    final String stream1 = new Base64UrlNamingStrategy("foo").generateName();
    String stream2 = stream1 + "-1";
    String firstQueue = null;
    CachingConnectionFactory connectionFactory = rabbitTestSupport.getResource();
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
    for (int i = 0; i < 5; i++) {
        String queue1Name = AbstractBinder.applyPrefix(BINDER_PREFIX, stream1 + ".default." + i);
        String queue2Name = AbstractBinder.applyPrefix(BINDER_PREFIX, stream2 + ".default." + i);
        if (firstQueue == null) {
            firstQueue = queue1Name;
        }
        rabbitAdmin.declareQueue(new Queue(queue1Name, true, false, false));
        rabbitAdmin.declareQueue(new Queue(queue2Name, true, false, false));
        rabbitAdmin.declareQueue(new Queue(AbstractBinder.constructDLQName(queue1Name), true, false, false));
        TopicExchange exchange = new TopicExchange(queue1Name);
        rabbitAdmin.declareExchange(exchange);
        rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue(queue1Name)).to(exchange).with(queue1Name));
        exchange = new TopicExchange(queue2Name);
        rabbitAdmin.declareExchange(exchange);
        rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue(queue2Name)).to(exchange).with(queue2Name));
    }
    final TopicExchange topic1 = new TopicExchange(AbstractBinder.applyPrefix(BINDER_PREFIX, stream1 + ".foo.bar"));
    rabbitAdmin.declareExchange(topic1);
    rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue(firstQueue)).to(topic1).with("#"));
    String foreignQueue = UUID.randomUUID().toString();
    rabbitAdmin.declareQueue(new Queue(foreignQueue));
    rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue(foreignQueue)).to(topic1).with("#"));
    final TopicExchange topic2 = new TopicExchange(AbstractBinder.applyPrefix(BINDER_PREFIX, stream2 + ".foo.bar"));
    rabbitAdmin.declareExchange(topic2);
    rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue(firstQueue)).to(topic2).with("#"));
    new RabbitTemplate(connectionFactory).execute(new ChannelCallback<Void>() {

        @Override
        public Void doInRabbit(Channel channel) throws Exception {
            String queueName = AbstractBinder.applyPrefix(BINDER_PREFIX, stream1 + ".default." + 4);
            String consumerTag = channel.basicConsume(queueName, new DefaultConsumer(channel));
            try {
                waitForConsumerStateNot(queueName, 0);
                doClean(cleaner, stream1, false);
                fail("Expected exception");
            } catch (RabbitAdminException e) {
                assertThat(e).hasMessageContaining("Queue " + queueName + " is in use");
            }
            channel.basicCancel(consumerTag);
            waitForConsumerStateNot(queueName, 1);
            try {
                doClean(cleaner, stream1, false);
                fail("Expected exception");
            } catch (RabbitAdminException e) {
                assertThat(e).hasMessageContaining("Cannot delete exchange ");
                assertThat(e).hasMessageContaining("; it has bindings:");
            }
            return null;
        }

        private void waitForConsumerStateNot(String queueName, long state) throws InterruptedException {
            int n = 0;
            QueueInfo queue = client.getQueue("/", queueName);
            while (n++ < 100 && (queue == null || queue.getConsumerCount() == state)) {
                Thread.sleep(100);
                queue = client.getQueue("/", queueName);
            }
            assertThat(n).withFailMessage("Consumer state remained at " + state + " after 10 seconds").isLessThan(100);
        }
    });
    // easier than deleting the binding
    rabbitAdmin.deleteExchange(topic1.getName());
    rabbitAdmin.declareExchange(topic1);
    rabbitAdmin.deleteQueue(foreignQueue);
    connectionFactory.destroy();
    Map<String, List<String>> cleanedMap = doClean(cleaner, stream1, false);
    assertThat(cleanedMap).hasSize(2);
    List<String> cleanedQueues = cleanedMap.get("queues");
    // should *not* clean stream2
    assertThat(cleanedQueues).hasSize(10);
    for (int i = 0; i < 5; i++) {
        assertThat(cleanedQueues.get(i * 2)).isEqualTo(BINDER_PREFIX + stream1 + ".default." + i);
        assertThat(cleanedQueues.get(i * 2 + 1)).isEqualTo(BINDER_PREFIX + stream1 + ".default." + i + ".dlq");
    }
    List<String> cleanedExchanges = cleanedMap.get("exchanges");
    assertThat(cleanedExchanges).hasSize(6);
    // wild card *should* clean stream2
    cleanedMap = doClean(cleaner, stream1 + "*", false);
    assertThat(cleanedMap).hasSize(2);
    cleanedQueues = cleanedMap.get("queues");
    assertThat(cleanedQueues).hasSize(5);
    for (int i = 0; i < 5; i++) {
        assertThat(cleanedQueues.get(i)).isEqualTo(BINDER_PREFIX + stream2 + ".default." + i);
    }
    cleanedExchanges = cleanedMap.get("exchanges");
    assertThat(cleanedExchanges).hasSize(6);
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) QueueInfo(com.rabbitmq.http.client.domain.QueueInfo) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Channel(com.rabbitmq.client.Channel) RabbitAdmin(org.springframework.amqp.rabbit.core.RabbitAdmin) RabbitBindingCleaner(org.springframework.cloud.stream.binder.rabbit.admin.RabbitBindingCleaner) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) RabbitAdminException(org.springframework.cloud.stream.binder.rabbit.admin.RabbitAdminException) TopicExchange(org.springframework.amqp.core.TopicExchange) Base64UrlNamingStrategy(org.springframework.amqp.core.Base64UrlNamingStrategy) CachingConnectionFactory(org.springframework.amqp.rabbit.connection.CachingConnectionFactory) List(java.util.List) Queue(org.springframework.amqp.core.Queue) RabbitAdminException(org.springframework.cloud.stream.binder.rabbit.admin.RabbitAdminException) Test(org.junit.jupiter.api.Test)

Example 10 with QueueInfo

use of com.rabbitmq.http.client.domain.QueueInfo in project spring-amqp by spring-projects.

the class RabbitRestApiTests method testSpecificQueue.

@Test
public void testSpecificQueue() throws Exception {
    RabbitAdmin admin = new RabbitAdmin(connectionFactory);
    Map<String, Object> args = Collections.<String, Object>singletonMap("foo", "bar");
    Queue queue1 = QueueBuilder.nonDurable(UUID.randomUUID().toString()).autoDelete().withArguments(args).build();
    admin.declareQueue(queue1);
    Queue queue2 = QueueBuilder.durable(UUID.randomUUID().toString()).withArguments(args).build();
    admin.declareQueue(queue2);
    Channel channel = this.connectionFactory.createConnection().createChannel(false);
    String consumer = channel.basicConsume(queue1.getName(), false, "", false, true, null, new DefaultConsumer(channel));
    QueueInfo qi = await().until(() -> this.rabbitRestClient.getQueue("/", queue1.getName()), info -> info.getExclusiveConsumerTag() != null && !"".equals(info.getExclusiveConsumerTag()));
    QueueInfo queueOut = this.rabbitRestClient.getQueue("/", queue1.getName());
    assertThat(queueOut.isDurable()).isFalse();
    assertThat(queueOut.isExclusive()).isFalse();
    assertThat(queueOut.isAutoDelete()).isTrue();
    assertThat(queueOut.getName()).isEqualTo(queue1.getName());
    assertThat(queueOut.getArguments()).isEqualTo(args);
    assertThat(qi.getExclusiveConsumerTag()).isEqualTo(consumer);
    channel.basicCancel(consumer);
    channel.close();
    queueOut = this.rabbitRestClient.getQueue("/", queue2.getName());
    assertThat(queueOut.isDurable()).isTrue();
    assertThat(queueOut.isExclusive()).isFalse();
    assertThat(queueOut.isAutoDelete()).isFalse();
    assertThat(queueOut.getName()).isEqualTo(queue2.getName());
    assertThat(queueOut.getArguments()).isEqualTo(args);
    admin.deleteQueue(queue1.getName());
    admin.deleteQueue(queue2.getName());
}
Also used : QueueInfo(com.rabbitmq.http.client.domain.QueueInfo) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Channel(com.rabbitmq.client.Channel) Queue(org.springframework.amqp.core.Queue) Test(org.junit.jupiter.api.Test)

Aggregations

QueueInfo (com.rabbitmq.http.client.domain.QueueInfo)15 Client (com.rabbitmq.http.client.Client)13 Test (org.junit.jupiter.api.Test)12 Channel (com.rabbitmq.client.Channel)3 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)3 ExchangeInfo (com.rabbitmq.http.client.domain.ExchangeInfo)3 Queue (org.springframework.amqp.core.Queue)3 BindingInfo (com.rabbitmq.http.client.domain.BindingInfo)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 List (java.util.List)2 CachingConnectionFactory (org.springframework.amqp.rabbit.connection.CachingConnectionFactory)2 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)2 ApplicationContext (org.springframework.context.ApplicationContext)2 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 AmqpOutboundEndpoint (org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint)2 DeclareOk (com.rabbitmq.client.AMQP.Queue.DeclareOk)1 LongString (com.rabbitmq.client.LongString)1 ClientParameters (com.rabbitmq.http.client.ClientParameters)1 IOException (java.io.IOException)1