Search in sources :

Example 1 with Client

use of com.rabbitmq.http.client.Client in project tutorials by jhkim105.

the class RabbitClientTest method delete.

@Test
void delete() throws Exception {
    String apiUrl = "http://localhost:15672/api";
    String username = "guest";
    String password = "guest";
    Client client = new Client(new ClientParameters().url(apiUrl).username(username).password(password));
    List<QueueInfo> queueInfoList = client.getQueues();
    queueInfoList.forEach(qi -> client.deleteQueue("/", qi.getName()));
}
Also used : QueueInfo(com.rabbitmq.http.client.domain.QueueInfo) ClientParameters(com.rabbitmq.http.client.ClientParameters) Client(com.rabbitmq.http.client.Client) Test(org.junit.jupiter.api.Test)

Example 2 with Client

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

the class RabbitBinderTests method testConsumerPropertiesWithUserInfrastructureCustomQueueArgs.

@Test
public void testConsumerPropertiesWithUserInfrastructureCustomQueueArgs() throws Exception {
    RabbitTestBinder binder = getBinder();
    ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
    RabbitConsumerProperties extProps = properties.getExtension();
    extProps.setExchangeType(ExchangeTypes.DIRECT);
    extProps.setExchangeDurable(false);
    extProps.setExchangeAutoDelete(true);
    extProps.setBindingRoutingKey("foo");
    extProps.setExpires(30_000);
    extProps.setLazy(true);
    extProps.setMaxLength(10_000);
    extProps.setMaxLengthBytes(100_000);
    extProps.setMaxPriority(10);
    extProps.setOverflowBehavior("drop-head");
    extProps.setTtl(2_000);
    extProps.setAutoBindDlq(true);
    extProps.setDeadLetterQueueName("customDLQ");
    extProps.setDeadLetterExchange("customDLX");
    extProps.setDeadLetterExchangeType(ExchangeTypes.TOPIC);
    extProps.setDeadLetterRoutingKey("customDLRK");
    extProps.setDlqDeadLetterExchange("propsUser3");
    // GH-259 - if the next line was commented, the test failed.
    extProps.setDlqDeadLetterRoutingKey("propsUser3");
    extProps.setDlqExpires(60_000);
    extProps.setDlqLazy(true);
    extProps.setDlqMaxLength(20_000);
    extProps.setDlqMaxLengthBytes(40_000);
    extProps.setDlqOverflowBehavior("reject-publish");
    extProps.setDlqMaxPriority(8);
    extProps.setDlqTtl(1_000);
    extProps.setConsumerTagPrefix("testConsumerTag");
    extProps.setExclusive(true);
    Binding<MessageChannel> consumerBinding = binder.bindConsumer("propsUser3", "infra", createBindableChannel("input", new BindingProperties()), properties);
    Lifecycle endpoint = extractEndpoint(consumerBinding);
    SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint, "messageListenerContainer", SimpleMessageListenerContainer.class);
    assertThat(container.isRunning()).isTrue();
    Client client = new Client(adminUri());
    List<BindingInfo> bindings = client.getBindingsBySource("/", "propsUser3");
    int n = 0;
    while (n++ < 100 && bindings == null || bindings.size() < 1) {
        Thread.sleep(100);
        bindings = client.getBindingsBySource("/", "propsUser3");
    }
    assertThat(bindings.size()).isEqualTo(1);
    assertThat(bindings.get(0).getSource()).isEqualTo("propsUser3");
    assertThat(bindings.get(0).getDestination()).isEqualTo("propsUser3.infra");
    assertThat(bindings.get(0).getRoutingKey()).isEqualTo("foo");
    bindings = client.getBindingsBySource("/", "customDLX");
    n = 0;
    while (n++ < 100 && bindings == null || bindings.size() < 1) {
        Thread.sleep(100);
        bindings = client.getBindingsBySource("/", "customDLX");
    }
    // assertThat(bindings.size()).isEqualTo(1);
    assertThat(bindings.get(0).getSource()).isEqualTo("customDLX");
    assertThat(bindings.get(0).getDestination()).isEqualTo("customDLQ");
    assertThat(bindings.get(0).getRoutingKey()).isEqualTo("customDLRK");
    ExchangeInfo exchange = client.getExchange("/", "propsUser3");
    n = 0;
    while (n++ < 100 && exchange == null) {
        Thread.sleep(100);
        exchange = client.getExchange("/", "propsUser3");
    }
    assertThat(exchange.getType()).isEqualTo("direct");
    assertThat(exchange.isDurable()).isEqualTo(false);
    assertThat(exchange.isAutoDelete()).isEqualTo(true);
    exchange = client.getExchange("/", "customDLX");
    n = 0;
    while (n++ < 100 && exchange == null) {
        Thread.sleep(100);
        exchange = client.getExchange("/", "customDLX");
    }
    assertThat(exchange.getType()).isEqualTo("topic");
    assertThat(exchange.isDurable()).isEqualTo(true);
    assertThat(exchange.isAutoDelete()).isEqualTo(false);
    QueueInfo queue = client.getQueue("/", "propsUser3.infra");
    n = 0;
    while (n++ < 100 && queue == null || queue.getConsumerCount() == 0) {
        Thread.sleep(100);
        queue = client.getQueue("/", "propsUser3.infra");
    }
    assertThat(queue).isNotNull();
    Map<String, Object> args = queue.getArguments();
    assertThat(args.get("x-expires")).isEqualTo(30_000);
    assertThat(args.get("x-max-length")).isEqualTo(10_000);
    assertThat(args.get("x-max-length-bytes")).isEqualTo(100_000);
    assertThat(args.get("x-overflow")).isEqualTo("drop-head");
    assertThat(args.get("x-max-priority")).isEqualTo(10);
    assertThat(args.get("x-message-ttl")).isEqualTo(2_000);
    assertThat(args.get("x-dead-letter-exchange")).isEqualTo("customDLX");
    assertThat(args.get("x-dead-letter-routing-key")).isEqualTo("customDLRK");
    assertThat(args.get("x-queue-mode")).isEqualTo("lazy");
    assertThat(queue.getExclusiveConsumerTag()).isEqualTo("testConsumerTag#0");
    queue = client.getQueue("/", "customDLQ");
    n = 0;
    while (n++ < 100 && queue == null) {
        Thread.sleep(100);
        queue = client.getQueue("/", "customDLQ");
    }
    assertThat(queue).isNotNull();
    args = queue.getArguments();
    assertThat(args.get("x-expires")).isEqualTo(60_000);
    assertThat(args.get("x-max-length")).isEqualTo(20_000);
    assertThat(args.get("x-max-length-bytes")).isEqualTo(40_000);
    assertThat(args.get("x-overflow")).isEqualTo("reject-publish");
    assertThat(args.get("x-max-priority")).isEqualTo(8);
    assertThat(args.get("x-message-ttl")).isEqualTo(1_000);
    assertThat(args.get("x-dead-letter-exchange")).isEqualTo("propsUser3");
    assertThat(args.get("x-dead-letter-routing-key")).isEqualTo("propsUser3");
    assertThat(args.get("x-queue-mode")).isEqualTo("lazy");
    consumerBinding.unbind();
    assertThat(container.isRunning()).isFalse();
    verifyAutoDeclareContextClear(binder);
}
Also used : QueueInfo(com.rabbitmq.http.client.domain.QueueInfo) RabbitConsumerProperties(org.springframework.cloud.stream.binder.rabbit.properties.RabbitConsumerProperties) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) Lifecycle(org.springframework.context.Lifecycle) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) LongString(com.rabbitmq.client.LongString) ExchangeInfo(com.rabbitmq.http.client.domain.ExchangeInfo) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) MessageChannel(org.springframework.messaging.MessageChannel) BindingInfo(com.rabbitmq.http.client.domain.BindingInfo) Client(com.rabbitmq.http.client.Client) Test(org.junit.jupiter.api.Test)

Example 3 with Client

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

the class RabbitBinderTests method testConsumerPropertiesWithUserInfrastructureNoBind.

@Test
public void testConsumerPropertiesWithUserInfrastructureNoBind() throws Exception {
    RabbitAdmin admin = new RabbitAdmin(this.rabbitTestSupport.getResource());
    Queue queue = new Queue("propsUser1.infra");
    admin.declareQueue(queue);
    DirectExchange exchange = new DirectExchange("propsUser1");
    admin.declareExchange(exchange);
    admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("foo"));
    RabbitTestBinder binder = getBinder();
    ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
    properties.getExtension().setDeclareExchange(false);
    properties.getExtension().setBindQueue(false);
    Binding<MessageChannel> consumerBinding = binder.bindConsumer("propsUser1", "infra", createBindableChannel("input", new BindingProperties()), properties);
    Lifecycle endpoint = extractEndpoint(consumerBinding);
    SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint, "messageListenerContainer", SimpleMessageListenerContainer.class);
    assertThat(TestUtils.getPropertyValue(container, "missingQueuesFatal", Boolean.class)).isFalse();
    assertThat(container.isRunning()).isTrue();
    consumerBinding.unbind();
    assertThat(container.isRunning()).isFalse();
    Client client = new Client(adminUri());
    List<?> bindings = client.getBindingsBySource("/", exchange.getName());
    assertThat(bindings.size()).isEqualTo(1);
}
Also used : RabbitConsumerProperties(org.springframework.cloud.stream.binder.rabbit.properties.RabbitConsumerProperties) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) Lifecycle(org.springframework.context.Lifecycle) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) RabbitAdmin(org.springframework.amqp.rabbit.core.RabbitAdmin) MessageChannel(org.springframework.messaging.MessageChannel) DirectExchange(org.springframework.amqp.core.DirectExchange) Client(com.rabbitmq.http.client.Client) Queue(org.springframework.amqp.core.Queue) AnonymousQueue(org.springframework.amqp.core.AnonymousQueue) Test(org.junit.jupiter.api.Test)

Example 4 with Client

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

the class RabbitBinderTests method testConsumerPropertiesWithHeaderExchanges.

@Test
public void testConsumerPropertiesWithHeaderExchanges() throws Exception {
    RabbitTestBinder binder = getBinder();
    ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
    properties.getExtension().setExchangeType(ExchangeTypes.HEADERS);
    properties.getExtension().setAutoBindDlq(true);
    properties.getExtension().setDeadLetterExchange(ExchangeTypes.HEADERS);
    properties.getExtension().setDeadLetterExchange("propsHeader.dlx");
    Map<String, String> queueBindingArguments = new HashMap<>();
    queueBindingArguments.put("x-match", "any");
    queueBindingArguments.put("foo", "bar");
    properties.getExtension().setQueueBindingArguments(queueBindingArguments);
    properties.getExtension().setDlqBindingArguments(queueBindingArguments);
    String group = "bindingArgs";
    Binding<MessageChannel> consumerBinding = binder.bindConsumer("propsHeader", group, createBindableChannel("input", new BindingProperties()), properties);
    Lifecycle endpoint = extractEndpoint(consumerBinding);
    SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint, "messageListenerContainer", SimpleMessageListenerContainer.class);
    assertThat(container.isRunning()).isTrue();
    consumerBinding.unbind();
    assertThat(container.isRunning()).isFalse();
    assertThat(container.getQueueNames()[0]).isEqualTo("propsHeader." + group);
    Client client = new Client(adminUri());
    List<BindingInfo> bindings = client.getBindingsBySource("/", "propsHeader");
    int n = 0;
    while (n++ < 100 && bindings == null || bindings.size() < 1) {
        Thread.sleep(100);
        bindings = client.getBindingsBySource("/", "propsHeader");
    }
    assertThat(bindings.size()).isEqualTo(1);
    assertThat(bindings.get(0).getSource()).isEqualTo("propsHeader");
    assertThat(bindings.get(0).getDestination()).isEqualTo("propsHeader." + group);
    assertThat(bindings.get(0).getArguments()).hasEntrySatisfying("x-match", v -> assertThat(v).isEqualTo("any"));
    assertThat(bindings.get(0).getArguments()).hasEntrySatisfying("foo", v -> assertThat(v).isEqualTo("bar"));
    bindings = client.getBindingsBySource("/", "propsHeader.dlx");
    n = 0;
    while (n++ < 100 && bindings == null || bindings.size() < 1) {
        Thread.sleep(100);
        bindings = client.getBindingsBySource("/", "propsHeader.dlx");
    }
    assertThat(bindings.size()).isEqualTo(1);
    assertThat(bindings.get(0).getSource()).isEqualTo("propsHeader.dlx");
    assertThat(bindings.get(0).getDestination()).isEqualTo("propsHeader." + group + ".dlq");
    assertThat(bindings.get(0).getArguments()).hasEntrySatisfying("x-match", v -> assertThat(v).isEqualTo("any"));
    assertThat(bindings.get(0).getArguments()).hasEntrySatisfying("foo", v -> assertThat(v).isEqualTo("bar"));
    verifyAutoDeclareContextClear(binder);
}
Also used : RabbitConsumerProperties(org.springframework.cloud.stream.binder.rabbit.properties.RabbitConsumerProperties) HashMap(java.util.HashMap) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) Lifecycle(org.springframework.context.Lifecycle) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) LongString(com.rabbitmq.client.LongString) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) MessageChannel(org.springframework.messaging.MessageChannel) BindingInfo(com.rabbitmq.http.client.domain.BindingInfo) Client(com.rabbitmq.http.client.Client) Test(org.junit.jupiter.api.Test)

Example 5 with Client

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

the class RabbitBinderModuleTests method checkCustomizedArgs.

private void checkCustomizedArgs() throws MalformedURLException, URISyntaxException, InterruptedException {
    Client client = new Client(String.format("http://guest:guest@localhost:%d/api", RABBITMQ.getHttpPort()));
    List<BindingInfo> bindings = client.getBindingsBySource("/", "process-in-0");
    int n = 0;
    while (n++ < 100 && bindings == null || bindings.size() < 1) {
        Thread.sleep(100);
        bindings = client.getBindingsBySource("/", "process-in-0");
    }
    assertThat(bindings).isNotNull();
    assertThat(bindings.get(0).getArguments()).contains(entry("added.by", "customizer"));
    ExchangeInfo exchange = client.getExchange("/", "process-in-0");
    assertThat(exchange.getArguments()).contains(entry("added.by", "customizer"));
    QueueInfo queue = client.getQueue("/", bindings.get(0).getDestination());
    assertThat(queue.getArguments()).contains(entry("added.by", "customizer"));
    assertThat(queue.getArguments()).contains(entry("x-single-active-consumer", Boolean.TRUE));
}
Also used : QueueInfo(com.rabbitmq.http.client.domain.QueueInfo) BindingInfo(com.rabbitmq.http.client.domain.BindingInfo) Client(com.rabbitmq.http.client.Client) ExchangeInfo(com.rabbitmq.http.client.domain.ExchangeInfo) AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint)

Aggregations

Client (com.rabbitmq.http.client.Client)20 Test (org.junit.jupiter.api.Test)15 QueueInfo (com.rabbitmq.http.client.domain.QueueInfo)13 RabbitConsumerProperties (org.springframework.cloud.stream.binder.rabbit.properties.RabbitConsumerProperties)5 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)5 AmqpOutboundEndpoint (org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint)5 MessageChannel (org.springframework.messaging.MessageChannel)5 BindingInfo (com.rabbitmq.http.client.domain.BindingInfo)4 ExchangeInfo (com.rabbitmq.http.client.domain.ExchangeInfo)4 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)4 Channel (com.rabbitmq.client.Channel)3 LongString (com.rabbitmq.client.LongString)3 ClientParameters (com.rabbitmq.http.client.ClientParameters)3 HashMap (java.util.HashMap)3 Lifecycle (org.springframework.context.Lifecycle)3 Log (org.apache.commons.logging.Log)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 ApplicationContext (org.springframework.context.ApplicationContext)2 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 DeclareOk (com.rabbitmq.client.AMQP.Queue.DeclareOk)1