Search in sources :

Example 1 with BlockingQueueConsumer

use of org.springframework.amqp.rabbit.listener.BlockingQueueConsumer in project spring-integration by spring-projects.

the class ChannelTests method pubSubLostConnectionTest.

@Test
public void pubSubLostConnectionTest() throws Exception {
    final CyclicBarrier latch = new CyclicBarrier(2);
    channel.subscribe(message -> {
        try {
            latch.await(10, TimeUnit.SECONDS);
        } catch (Exception e) {
        }
    });
    this.channel.send(new GenericMessage<>("foo"));
    latch.await(10, TimeUnit.SECONDS);
    latch.reset();
    BlockingQueueConsumer consumer = (BlockingQueueConsumer) TestUtils.getPropertyValue(this.channel, "container.consumers", Set.class).iterator().next();
    connectionFactory.destroy();
    waitForNewConsumer(this.channel, consumer);
    this.channel.send(new GenericMessage<>("bar"));
    latch.await(10, TimeUnit.SECONDS);
    this.channel.destroy();
    this.pubSubWithEP.destroy();
    this.withEP.destroy();
    this.pollableWithEP.destroy();
    assertThat(TestUtils.getPropertyValue(connectionFactory, "connectionListener.delegates", Collection.class).size()).isEqualTo(0);
}
Also used : Set(java.util.Set) BlockingQueueConsumer(org.springframework.amqp.rabbit.listener.BlockingQueueConsumer) Collection(java.util.Collection) MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 2 with BlockingQueueConsumer

use of org.springframework.amqp.rabbit.listener.BlockingQueueConsumer in project spring-amqp by spring-projects.

the class RabbitBindingIntegrationTests method testSendAndReceiveWithTopicTwoCallbacks.

@Test
public void testSendAndReceiveWithTopicTwoCallbacks() {
    RabbitAdmin admin = new RabbitAdmin(connectionFactory);
    TopicExchange exchange = new TopicExchange("topic");
    admin.declareExchange(exchange);
    template.setExchange(exchange.getName());
    admin.declareBinding(BindingBuilder.bind(QUEUE).to(exchange).with("*.end"));
    template.execute(channel -> {
        BlockingQueueConsumer consumer = createConsumer(template);
        String tag = consumer.getConsumerTags().iterator().next();
        assertThat(tag).isNotNull();
        try {
            template.convertAndSend("foo", "message");
            String result = getResult(consumer, false);
            assertThat(result).isEqualTo(null);
        } finally {
            consumer.stop();
        }
        return null;
    });
    template.execute(channel -> {
        BlockingQueueConsumer consumer = createConsumer(template);
        String tag = consumer.getConsumerTags().iterator().next();
        assertThat(tag).isNotNull();
        try {
            template.convertAndSend("foo.end", "message");
            String result = getResult(consumer, true);
            assertThat(result).isEqualTo("message");
        } finally {
            consumer.stop();
        }
        return null;
    });
    admin.deleteExchange("topic");
}
Also used : BlockingQueueConsumer(org.springframework.amqp.rabbit.listener.BlockingQueueConsumer) TopicExchange(org.springframework.amqp.core.TopicExchange) Test(org.junit.jupiter.api.Test)

Example 3 with BlockingQueueConsumer

use of org.springframework.amqp.rabbit.listener.BlockingQueueConsumer in project spring-amqp by spring-projects.

the class RabbitBindingIntegrationTests method testSendAndReceiveWithFanout.

@Test
public void testSendAndReceiveWithFanout() {
    RabbitAdmin admin = new RabbitAdmin(connectionFactory);
    FanoutExchange exchange = new FanoutExchange("fanout");
    admin.declareExchange(exchange);
    template.setExchange(exchange.getName());
    admin.declareBinding(BindingBuilder.bind(QUEUE).to(exchange));
    template.execute(channel -> {
        BlockingQueueConsumer consumer = createConsumer(template);
        String tag = consumer.getConsumerTags().iterator().next();
        assertThat(tag).isNotNull();
        try {
            template.convertAndSend("message");
            String result = getResult(consumer, true);
            assertThat(result).isEqualTo("message");
        } finally {
            consumer.stop();
        }
        return null;
    });
    admin.deleteExchange("fanout");
}
Also used : BlockingQueueConsumer(org.springframework.amqp.rabbit.listener.BlockingQueueConsumer) FanoutExchange(org.springframework.amqp.core.FanoutExchange) Test(org.junit.jupiter.api.Test)

Example 4 with BlockingQueueConsumer

use of org.springframework.amqp.rabbit.listener.BlockingQueueConsumer in project spring-amqp by spring-projects.

the class RabbitBindingIntegrationTests method testSendAndReceiveWithTopicConsumeInBackground.

@Test
public // @Ignore("Not sure yet if we need to support a use case like this")
void testSendAndReceiveWithTopicConsumeInBackground() throws Exception {
    RabbitAdmin admin = new RabbitAdmin(connectionFactory);
    TopicExchange exchange = new TopicExchange("topic");
    admin.declareExchange(exchange);
    template.setExchange(exchange.getName());
    admin.declareBinding(BindingBuilder.bind(QUEUE).to(exchange).with("*.end"));
    final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    cachingConnectionFactory.setHost("localhost");
    final RabbitTemplate template1 = new RabbitTemplate(cachingConnectionFactory);
    template1.setExchange(exchange.getName());
    BlockingQueueConsumer consumer = template1.execute(channel -> {
        BlockingQueueConsumer consumer1 = createConsumer(template1);
        String tag = consumer1.getConsumerTags().iterator().next();
        assertThat(tag).isNotNull();
        return consumer1;
    });
    template1.convertAndSend("foo", "message");
    String result = getResult(consumer, false);
    assertThat(result).isEqualTo(null);
    template1.convertAndSend("foo.end", "message");
    result = getResult(consumer, true);
    assertThat(result).isEqualTo("message");
    consumer.stop();
    admin.deleteExchange("topic");
    cachingConnectionFactory.destroy();
}
Also used : CachingConnectionFactory(org.springframework.amqp.rabbit.connection.CachingConnectionFactory) BlockingQueueConsumer(org.springframework.amqp.rabbit.listener.BlockingQueueConsumer) TopicExchange(org.springframework.amqp.core.TopicExchange) Test(org.junit.jupiter.api.Test)

Example 5 with BlockingQueueConsumer

use of org.springframework.amqp.rabbit.listener.BlockingQueueConsumer in project spring-integration by spring-projects.

the class ChannelTests method waitForNewConsumer.

@SuppressWarnings("unchecked")
private void waitForNewConsumer(PublishSubscribeAmqpChannel channel, BlockingQueueConsumer consumer) throws Exception {
    final Object consumersMonitor = TestUtils.getPropertyValue(channel, "container.consumersMonitor");
    int n = 0;
    while (n++ < 100) {
        Set<BlockingQueueConsumer> consumers = TestUtils.getPropertyValue(channel, "container.consumers", Set.class);
        synchronized (consumersMonitor) {
            if (!consumers.isEmpty()) {
                BlockingQueueConsumer newConsumer = consumers.iterator().next();
                if (newConsumer != consumer && newConsumer.getConsumerTags().size() > 0) {
                    break;
                }
            }
        }
        Thread.sleep(100);
    }
    assertThat(n < 100).as("Failed to restart consumer").isTrue();
}
Also used : BlockingQueueConsumer(org.springframework.amqp.rabbit.listener.BlockingQueueConsumer)

Aggregations

BlockingQueueConsumer (org.springframework.amqp.rabbit.listener.BlockingQueueConsumer)11 Test (org.junit.jupiter.api.Test)5 TopicExchange (org.springframework.amqp.core.TopicExchange)4 AcknowledgeMode (org.springframework.amqp.core.AcknowledgeMode)3 Collection (java.util.Collection)1 Set (java.util.Set)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 Test (org.junit.Test)1 FanoutExchange (org.springframework.amqp.core.FanoutExchange)1 CachingConnectionFactory (org.springframework.amqp.rabbit.connection.CachingConnectionFactory)1 DefaultMessagePropertiesConverter (org.springframework.amqp.rabbit.support.DefaultMessagePropertiesConverter)1 MessageConversionException (org.springframework.amqp.support.converter.MessageConversionException)1