Search in sources :

Example 11 with Client

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

the class RabbitBinderTests method testConsumerPropertiesWithUserInfrastructureCustomExchangeAndRK.

@Test
public void testConsumerPropertiesWithUserInfrastructureCustomExchangeAndRK() throws Exception {
    RabbitTestBinder binder = getBinder();
    ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
    properties.getExtension().setExchangeType(ExchangeTypes.DIRECT);
    properties.getExtension().setBindingRoutingKey("foo,bar");
    properties.getExtension().setBindingRoutingKeyDelimiter(",");
    properties.getExtension().setQueueNameGroupOnly(true);
    // properties.getExtension().setDelayedExchange(true); // requires delayed message
    // exchange plugin; tested locally
    String group = "infra";
    Binding<MessageChannel> consumerBinding = binder.bindConsumer("propsUser2", 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(group);
    Client client = new Client(adminUri());
    List<BindingInfo> bindings = client.getBindingsBySource("/", "propsUser2");
    int n = 0;
    while (n++ < 100 && bindings == null || bindings.size() < 1) {
        Thread.sleep(100);
        bindings = client.getBindingsBySource("/", "propsUser2");
    }
    assertThat(bindings.size()).isEqualTo(2);
    assertThat(bindings.get(0).getSource()).isEqualTo("propsUser2");
    assertThat(bindings.get(0).getDestination()).isEqualTo(group);
    assertThat(bindings.get(0).getRoutingKey()).isIn("foo", "bar");
    assertThat(bindings.get(1).getSource()).isEqualTo("propsUser2");
    assertThat(bindings.get(1).getDestination()).isEqualTo(group);
    assertThat(bindings.get(1).getRoutingKey()).isIn("foo", "bar");
    assertThat(bindings.get(1).getRoutingKey()).isNotEqualTo(bindings.get(0).getRoutingKey());
    ExchangeInfo exchange = client.getExchange("/", "propsUser2");
    while (n++ < 100 && exchange == null) {
        Thread.sleep(100);
        exchange = client.getExchange("/", "propsUser2");
    }
    assertThat(exchange.getType()).isEqualTo("direct");
    assertThat(exchange.isDurable()).isEqualTo(true);
    assertThat(exchange.isAutoDelete()).isEqualTo(false);
    verifyAutoDeclareContextClear(binder);
}
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) 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 12 with Client

use of com.rabbitmq.http.client.Client 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 13 with Client

use of com.rabbitmq.http.client.Client in project hop by rabbitmq.

the class SanityCheck method main.

public static void main(String[] args) {
    try {
        Client c = new Client(new ClientParameters().url("http://127.0.0.1:15672/api/").username("guest").password("guest"));
        c.getOverview();
        LoggerFactory.getLogger("rabbitmq").info("Test succeeded with Hop {}", Client.class.getPackage().getImplementationVersion());
        System.exit(0);
    } catch (Exception e) {
        LoggerFactory.getLogger("rabbitmq").info("Test failed", e);
        System.exit(1);
    }
}
Also used : ClientParameters(com.rabbitmq.http.client.ClientParameters) Client(com.rabbitmq.http.client.Client)

Example 14 with Client

use of com.rabbitmq.http.client.Client in project hop by rabbitmq.

the class AppTest method createUseClient.

@Test
void createUseClient() throws Exception {
    HttpLayerFactory httpLayerFactory = JdkHttpClientHttpLayer.configure().clientBuilderConsumer(clientBuilder -> clientBuilder.connectTimeout(Duration.ofSeconds(10))).requestBuilderConsumer(requestBuilder -> requestBuilder.timeout(Duration.ofSeconds(10)).setHeader("Authorization", authorization("guest", "guest"))).create();
    Client c = new Client(new ClientParameters().url("http://127.0.0.1:15672/api/").username("guest").password("guest").httpLayerFactory(httpLayerFactory));
    c.getOverview();
}
Also used : Test(org.junit.jupiter.api.Test) JdkHttpClientHttpLayer.authorization(com.rabbitmq.http.client.JdkHttpClientHttpLayer.authorization) Duration(java.time.Duration) JdkHttpClientHttpLayer(com.rabbitmq.http.client.JdkHttpClientHttpLayer) ClientParameters(com.rabbitmq.http.client.ClientParameters) Client(com.rabbitmq.http.client.Client) HttpLayerFactory(com.rabbitmq.http.client.HttpLayer.HttpLayerFactory) ClientParameters(com.rabbitmq.http.client.ClientParameters) HttpLayerFactory(com.rabbitmq.http.client.HttpLayer.HttpLayerFactory) Client(com.rabbitmq.http.client.Client) Test(org.junit.jupiter.api.Test)

Example 15 with Client

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

the class FixedReplyQueueDeadLetterTests method testQueueArgs1.

@Test
void testQueueArgs1() throws MalformedURLException, URISyntaxException, InterruptedException {
    Client client = new Client(brokerRunning.getAdminUri(), brokerRunning.getAdminUser(), brokerRunning.getAdminPassword());
    QueueInfo queue = await().until(() -> client.getQueue("/", "all.args.1"), 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.minLeaders.getValue());
    assertThat(arguments.get("x-single-active-consumer")).isEqualTo(Boolean.TRUE);
}
Also used : QueueInfo(com.rabbitmq.http.client.domain.QueueInfo) Client(com.rabbitmq.http.client.Client) Test(org.junit.jupiter.api.Test)

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