@Before
 public void createConnectionFactory() {
   CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
   connectionFactory.setChannelCacheSize(concurrentConsumers);
   connectionFactory.setPort(BrokerTestUtils.getPort());
   template.setConnectionFactory(connectionFactory);
 }
 @Before
 public void declareQueue() {
   CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
   connectionFactory.setHost("localhost");
   connectionFactory.setChannelCacheSize(concurrentConsumers);
   connectionFactory.setPort(BrokerTestUtils.getPort());
   template.setConnectionFactory(connectionFactory);
 }
 @Before
 public void create() {
   connectionFactory = new CachingConnectionFactory();
   connectionFactory.setHost("localhost");
   connectionFactory.setChannelCacheSize(10);
   connectionFactory.setPort(BrokerTestUtils.getPort());
   connectionFactoryWithConfirmsEnabled = new CachingConnectionFactory();
   connectionFactoryWithConfirmsEnabled.setHost("localhost");
   // When using publisher confirms, the cache size needs to be large enough
   // otherwise channels can be closed before confirms are received.
   connectionFactoryWithConfirmsEnabled.setChannelCacheSize(100);
   connectionFactoryWithConfirmsEnabled.setPort(BrokerTestUtils.getPort());
   connectionFactoryWithConfirmsEnabled.setPublisherConfirms(true);
   templateWithConfirmsEnabled = new RabbitTemplate(connectionFactoryWithConfirmsEnabled);
   connectionFactoryWithReturnsEnabled = new CachingConnectionFactory();
   connectionFactoryWithReturnsEnabled.setHost("localhost");
   connectionFactoryWithReturnsEnabled.setChannelCacheSize(1);
   connectionFactoryWithReturnsEnabled.setPort(BrokerTestUtils.getPort());
   connectionFactoryWithReturnsEnabled.setPublisherReturns(true);
   templateWithReturnsEnabled = new RabbitTemplate(connectionFactoryWithReturnsEnabled);
 }
 private RabbitTemplate createTemplate(int concurrentConsumers) {
   RabbitTemplate template = new RabbitTemplate();
   CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
   connectionFactory.setHost("localhost");
   connectionFactory.setChannelCacheSize(concurrentConsumers);
   connectionFactory.setPort(BrokerTestUtils.getPort());
   template.setConnectionFactory(connectionFactory);
   if (messageConverter == null) {
     SimpleMessageConverter messageConverter = new SimpleMessageConverter();
     messageConverter.setCreateMessageIds(true);
     this.messageConverter = messageConverter;
   }
   template.setMessageConverter(messageConverter);
   return template;
 }
  private void doTest(int concurrentConsumers, ContainerConfigurer configurer) {
    int messageCount = 10;
    RabbitTemplate template = new RabbitTemplate();
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setHost("localhost");
    connectionFactory.setChannelCacheSize(concurrentConsumers);
    connectionFactory.setPort(BrokerTestUtils.getPort());
    template.setConnectionFactory(connectionFactory);
    SimpleMessageConverter messageConverter = new SimpleMessageConverter();
    messageConverter.setCreateMessageIds(true);
    template.setMessageConverter(messageConverter);
    for (int i = 0; i < messageCount; i++) {
      template.convertAndSend(queue1.getName(), new Integer(i));
      template.convertAndSend(queue2.getName(), new Integer(i));
    }
    final SimpleMessageListenerContainer container =
        new SimpleMessageListenerContainer(connectionFactory);
    final CountDownLatch latch = new CountDownLatch(messageCount * 2);
    PojoListener listener = new PojoListener(latch);
    container.setMessageListener(new MessageListenerAdapter(listener));
    container.setAcknowledgeMode(AcknowledgeMode.AUTO);
    container.setChannelTransacted(true);
    container.setConcurrentConsumers(concurrentConsumers);
    configurer.configure(container);
    container.afterPropertiesSet();
    container.start();
    try {
      int timeout = Math.min(1 + messageCount / concurrentConsumers, 30);
      boolean waited = latch.await(timeout, TimeUnit.SECONDS);
      logger.info("All messages recovered: " + waited);
      assertEquals(concurrentConsumers, container.getActiveConsumerCount());
      assertTrue("Timed out waiting for messages", waited);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      throw new IllegalStateException("unexpected interruption");
    } finally {
      container.shutdown();
      assertEquals(0, container.getActiveConsumerCount());
    }
    assertNull(template.receiveAndConvert(queue1.getName()));
    assertNull(template.receiveAndConvert(queue2.getName()));

    connectionFactory.destroy();
  }
  @Bean
  public CachingConnectionFactory rabbitConnectionFactory() {
    log.info("AMQP URL: {}", isBlank(amqpConnectionUri) ? "localhost:5672" : amqpConnectionUri);

    RabbitConnectionFactoryConfig config = uriToConnectionConfig(amqpConnectionUri);
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(config.getHost());
    connectionFactory.setPort(config.getPort());

    if (config.getUsername() != null) {
      connectionFactory.setUsername(config.getUsername());
    }
    if (config.getPassword() != null) {
      connectionFactory.setPassword(config.getPassword());
    }
    if (config.getVirtualHost() != null) {
      connectionFactory.setVirtualHost(StringUtils.remove(config.getVirtualHost(), '/'));
    }

    return connectionFactory;
  }