Search in sources :

Example 6 with Client

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

the class RabbitListenerTests method queueOverAmqp.

@Test
void queueOverAmqp() throws Exception {
    Client client = new Client("http://guest:guest@localhost:" + managementPort() + "/api");
    QueueInfo queue = client.getQueue("/", "stream.created.over.amqp");
    assertThat(queue.getArguments().get("x-queue-type")).isEqualTo("stream");
}
Also used : QueueInfo(com.rabbitmq.http.client.domain.QueueInfo) Client(com.rabbitmq.http.client.Client) Test(org.junit.jupiter.api.Test)

Example 7 with Client

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

the class LocalizedQueueConnectionFactory method determineConnectionFactory.

@Nullable
private ConnectionFactory determineConnectionFactory(String queue) {
    for (int i = 0; i < this.adminUris.length; i++) {
        String adminUri = this.adminUris[i];
        if (!adminUri.endsWith("/api/")) {
            adminUri += "/api/";
        }
        try {
            Client client = createClient(adminUri, this.username, this.password);
            QueueInfo queueInfo = client.getQueue(this.vhost, queue);
            if (queueInfo != null) {
                String node = queueInfo.getNode();
                if (node != null) {
                    String uri = this.nodeToAddress.get(node);
                    if (uri != null) {
                        return nodeConnectionFactory(queue, node, uri);
                    }
                    if (this.logger.isDebugEnabled()) {
                        this.logger.debug("No match for node: " + node);
                    }
                }
            } else {
                throw new AmqpException("Admin returned null QueueInfo");
            }
        } catch (Exception e) {
            this.logger.warn("Failed to determine queue location for: " + queue + " at: " + adminUri + ": " + e.getMessage());
        }
    }
    this.logger.warn("Failed to determine queue location for: " + queue + ", using default connection factory");
    return null;
}
Also used : QueueInfo(com.rabbitmq.http.client.domain.QueueInfo) AmqpException(org.springframework.amqp.AmqpException) Client(com.rabbitmq.http.client.Client) AmqpException(org.springframework.amqp.AmqpException) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) Nullable(org.springframework.lang.Nullable)

Example 8 with Client

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

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

the class LocalizedQueueConnectionFactoryTests method testFailOver.

@Test
public void testFailOver() throws Exception {
    ConnectionFactory defaultConnectionFactory = mockCF("localhost:1234", null);
    String rabbit1 = "localhost:1235";
    String rabbit2 = "localhost:1236";
    String[] addresses = new String[] { rabbit1, rabbit2 };
    String[] adminUris = new String[] { "http://localhost:11235", "http://localhost:11236" };
    String[] nodes = new String[] { "rabbit@foo", "rabbit@bar" };
    String vhost = "/";
    String username = "guest";
    String password = "guest";
    final AtomicBoolean firstServer = new AtomicBoolean(true);
    final Client client1 = doCreateClient(adminUris[0], username, password, nodes[0]);
    final Client client2 = doCreateClient(adminUris[1], username, password, nodes[1]);
    final Map<String, ConnectionFactory> mockCFs = new HashMap<String, ConnectionFactory>();
    CountDownLatch latch1 = new CountDownLatch(1);
    CountDownLatch latch2 = new CountDownLatch(1);
    mockCFs.put(rabbit1, mockCF(rabbit1, latch1));
    mockCFs.put(rabbit2, mockCF(rabbit2, latch2));
    LocalizedQueueConnectionFactory lqcf = new LocalizedQueueConnectionFactory(defaultConnectionFactory, addresses, adminUris, nodes, vhost, username, password, false, null) {

        @Override
        protected Client createClient(String adminUri, String username, String password) {
            return firstServer.get() ? client1 : client2;
        }

        @Override
        protected ConnectionFactory createConnectionFactory(String address, String node) {
            return mockCFs.get(address);
        }
    };
    Map<?, ?> nodeAddress = TestUtils.getPropertyValue(lqcf, "nodeToAddress", Map.class);
    assertThat(nodeAddress.get("rabbit@foo")).isEqualTo(rabbit1);
    assertThat(nodeAddress.get("rabbit@bar")).isEqualTo(rabbit2);
    String[] admins = TestUtils.getPropertyValue(lqcf, "adminUris", String[].class);
    assertThat(admins).containsExactly(adminUris);
    Log logger = spy(TestUtils.getPropertyValue(lqcf, "logger", Log.class));
    willReturn(true).given(logger).isInfoEnabled();
    new DirectFieldAccessor(lqcf).setPropertyValue("logger", logger);
    willAnswer(new CallsRealMethods()).given(logger).debug(anyString());
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(lqcf);
    container.setQueueNames("q");
    container.afterPropertiesSet();
    container.start();
    assertThat(latch1.await(10, TimeUnit.SECONDS)).isTrue();
    Channel channel = this.channels.get(rabbit1);
    assertThat(channel).isNotNull();
    verify(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class));
    verify(logger, atLeast(1)).info(captor.capture());
    assertThat(assertLog(captor.getAllValues(), "Queue: q is on node: rabbit@foo at: localhost:1235")).isTrue();
    // Fail rabbit1 and verify the container switches to rabbit2
    firstServer.set(false);
    this.consumers.get(rabbit1).handleCancel(consumerTags.get(rabbit1));
    assertThat(latch2.await(10, TimeUnit.SECONDS)).isTrue();
    channel = this.channels.get(rabbit2);
    assertThat(channel).isNotNull();
    verify(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class));
    container.stop();
    verify(logger, atLeast(1)).info(captor.capture());
    assertThat(assertLog(captor.getAllValues(), "Queue: q is on node: rabbit@bar at: localhost:1236")).isTrue();
}
Also used : HashMap(java.util.HashMap) Log(org.apache.commons.logging.Log) Channel(com.rabbitmq.client.Channel) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(com.rabbitmq.client.Consumer) CallsRealMethods(org.mockito.internal.stubbing.answers.CallsRealMethods) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Client(com.rabbitmq.http.client.Client) Test(org.junit.jupiter.api.Test)

Example 10 with Client

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

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