private SimpleMessageListenerContainer createContainer() throws Exception {
   // if (!messageDriven) TODO: no container attributes would apply if not message-driven
   SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
   if (this.acknowledgeMode != null) {
     container.setAcknowledgeMode(this.acknowledgeMode);
   }
   if (!ObjectUtils.isEmpty(this.adviceChain)) {
     container.setAdviceChain(this.adviceChain);
   }
   container.setAutoStartup(this.autoStartup);
   container.setChannelTransacted(this.channelTransacted);
   if (this.concurrentConsumers != null) {
     container.setConcurrentConsumers(this.concurrentConsumers);
   }
   container.setConnectionFactory(this.connectionFactory);
   if (this.errorHandler != null) {
     container.setErrorHandler(this.errorHandler);
   }
   if (this.exposeListenerChannel != null) {
     container.setExposeListenerChannel(this.exposeListenerChannel);
   }
   if (this.messagePropertiesConverter != null) {
     container.setMessagePropertiesConverter(this.messagePropertiesConverter);
   }
   if (this.phase != null) {
     container.setPhase(this.phase);
   }
   if (this.prefetchCount != null) {
     container.setPrefetchCount(this.prefetchCount);
   }
   if (this.receiveTimeout != null) {
     container.setReceiveTimeout(this.receiveTimeout);
   }
   if (this.recoveryInterval != null) {
     container.setRecoveryInterval(this.recoveryInterval);
   }
   if (this.shutdownTimeout != null) {
     container.setShutdownTimeout(this.shutdownTimeout);
   }
   if (this.taskExecutor != null) {
     container.setTaskExecutor(this.taskExecutor);
   }
   if (this.transactionAttribute != null) {
     container.setTransactionAttribute(this.transactionAttribute);
   }
   if (this.transactionManager != null) {
     container.setTransactionManager(this.transactionManager);
   }
   if (this.txSize != null) {
     container.setTxSize(this.txSize);
   }
   if (this.missingQueuesFatal != null) {
     container.setMissingQueuesFatal(this.missingQueuesFatal);
   }
   return container;
 }
 @Bean
 public SimpleMessageListenerContainer messageListenerContainer(AmqpAdmin amqpAdmin) {
   SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
   container.setConnectionFactory(rabbitConnectionFactory());
   container.setQueueNames(clientQueue(amqpAdmin).getName());
   container.setMessageListener(
       new MessageListenerAdapter(serviceMessageReceiver, messageConverter));
   // No acks will be sent (incompatible with channelTransacted=true). RabbitMQ calls this
   // "autoack" because the broker assumes all messages are acked without any action from the
   // consumer.
   container.setAcknowledgeMode(AcknowledgeMode.NONE);
   return container;
 }
 private SimpleMessageListenerContainer createContainer(Object listener) {
   SimpleMessageListenerContainer container =
       new SimpleMessageListenerContainer(template.getConnectionFactory());
   container.setMessageListener(new MessageListenerAdapter(listener));
   container.setQueueNames(queue.getName());
   container.setTxSize(txSize);
   container.setPrefetchCount(txSize);
   container.setConcurrentConsumers(concurrentConsumers);
   container.setChannelTransacted(transactional);
   container.setAcknowledgeMode(AcknowledgeMode.AUTO);
   container.afterPropertiesSet();
   container.start();
   return container;
 }
  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();
  }
 private SimpleMessageListenerContainer createContainer(Object listener) {
   SimpleMessageListenerContainer container =
       new SimpleMessageListenerContainer(template.getConnectionFactory());
   container.setMessageListener(listener);
   container.setQueueNames(queue.getName());
   container.setTxSize(txSize);
   container.setPrefetchCount(txSize);
   container.setConcurrentConsumers(concurrentConsumers);
   container.setChannelTransacted(transactional);
   container.setAcknowledgeMode(acknowledgeMode);
   // requires RabbitMQ 3.2.x
   //		container.setConsumerArguments(Collections. <String, Object> singletonMap("x-priority",
   // Integer.valueOf(10)));
   if (externalTransaction) {
     container.setTransactionManager(new TestTransactionManager());
   }
   container.afterPropertiesSet();
   container.start();
   return container;
 }
  private void doTestRetry(
      int messageCount, int txSize, int failFrequency, int concurrentConsumers, boolean stateful)
      throws Exception {

    int failedMessageCount =
        messageCount / failFrequency + (messageCount % failFrequency == 0 ? 0 : 1);

    RabbitTemplate template = createTemplate(concurrentConsumers);
    for (int i = 0; i < messageCount; i++) {
      template.convertAndSend(queue.getName(), i);
    }

    final SimpleMessageListenerContainer container =
        new SimpleMessageListenerContainer(template.getConnectionFactory());
    PojoListener listener = new PojoListener(failFrequency);
    container.setMessageListener(new MessageListenerAdapter(listener));
    container.setAcknowledgeMode(AcknowledgeMode.AUTO);
    container.setChannelTransacted(true);
    container.setTxSize(txSize);
    container.setConcurrentConsumers(concurrentConsumers);

    final CountDownLatch latch = new CountDownLatch(failedMessageCount);
    container.setAdviceChain(new Advice[] {createRetryInterceptor(latch, stateful)});

    container.setQueueNames(queue.getName());
    container.afterPropertiesSet();
    container.start();

    try {

      int timeout = Math.min(1 + 2 * messageCount / concurrentConsumers, 30);

      final int count = messageCount;
      logger.debug("Waiting for messages with timeout = " + timeout + " (s)");
      Executors.newSingleThreadExecutor()
          .execute(
              () -> {
                while (container.getActiveConsumerCount() > 0) {
                  try {
                    Thread.sleep(100L);
                  } catch (InterruptedException e) {
                    latch.countDown();
                    Thread.currentThread().interrupt();
                    return;
                  }
                }
                for (int i = 0; i < count; i++) {
                  latch.countDown();
                }
              });
      boolean waited = latch.await(timeout, TimeUnit.SECONDS);
      logger.info("All messages recovered: " + waited);
      assertEquals(concurrentConsumers, container.getActiveConsumerCount());
      assertTrue("Timed out waiting for messages", waited);

      // Retried each failure 3 times (default retry policy)...
      assertEquals(3 * failedMessageCount, listener.getCount());

      // All failed messages recovered
      assertEquals(null, template.receiveAndConvert(queue.getName()));

    } finally {
      container.shutdown();
      ((DisposableBean) template.getConnectionFactory()).destroy();

      assertEquals(0, container.getActiveConsumerCount());
    }
  }
 private void doRegisterConsumer(
     String name,
     MessageChannel moduleInputChannel,
     Queue queue,
     RabbitPropertiesAccessor properties,
     boolean isPubSub) {
   // Fix for XD-2503
   // Temporarily overrides the thread context classloader with the one where the
   // SimpleMessageListenerContainer
   // is defined
   // This allows for the proxying that happens while initializing the
   // SimpleMessageListenerContainer to work
   // correctly
   ClassLoader originalClassloader = Thread.currentThread().getContextClassLoader();
   try {
     ClassUtils.overrideThreadContextClassLoader(
         SimpleMessageListenerContainer.class.getClassLoader());
     SimpleMessageListenerContainer listenerContainer =
         new SimpleMessageListenerContainer(this.connectionFactory);
     listenerContainer.setAcknowledgeMode(
         properties.getAcknowledgeMode(this.defaultAcknowledgeMode));
     listenerContainer.setChannelTransacted(
         properties.getTransacted(this.defaultChannelTransacted));
     listenerContainer.setDefaultRequeueRejected(
         properties.getRequeueRejected(this.defaultDefaultRequeueRejected));
     if (!isPubSub) {
       int concurrency = properties.getConcurrency(this.defaultConcurrency);
       concurrency = concurrency > 0 ? concurrency : 1;
       listenerContainer.setConcurrentConsumers(concurrency);
       int maxConcurrency = properties.getMaxConcurrency(this.defaultMaxConcurrency);
       if (maxConcurrency > concurrency) {
         listenerContainer.setMaxConcurrentConsumers(maxConcurrency);
       }
     }
     listenerContainer.setPrefetchCount(properties.getPrefetchCount(this.defaultPrefetchCount));
     listenerContainer.setTxSize(properties.getTxSize(this.defaultTxSize));
     listenerContainer.setTaskExecutor(new SimpleAsyncTaskExecutor(queue.getName() + "-"));
     listenerContainer.setQueues(queue);
     int maxAttempts = properties.getMaxAttempts(this.defaultMaxAttempts);
     if (maxAttempts > 1 || properties.getRepublishToDLQ(this.defaultRepublishToDLQ)) {
       RetryOperationsInterceptor retryInterceptor =
           RetryInterceptorBuilder.stateless()
               .maxAttempts(maxAttempts)
               .backOffOptions(
                   properties.getBackOffInitialInterval(this.defaultBackOffInitialInterval),
                   properties.getBackOffMultiplier(this.defaultBackOffMultiplier),
                   properties.getBackOffMaxInterval(this.defaultBackOffMaxInterval))
               .recoverer(determineRecoverer(name, properties))
               .build();
       listenerContainer.setAdviceChain(new Advice[] {retryInterceptor});
     }
     listenerContainer.setAfterReceivePostProcessors(this.decompressingPostProcessor);
     listenerContainer.setMessagePropertiesConverter(
         RabbitMessageChannelBinder.inboundMessagePropertiesConverter);
     listenerContainer.afterPropertiesSet();
     AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(listenerContainer);
     adapter.setBeanFactory(this.getBeanFactory());
     DirectChannel bridgeToModuleChannel = new DirectChannel();
     bridgeToModuleChannel.setBeanFactory(this.getBeanFactory());
     bridgeToModuleChannel.setBeanName(name + ".bridge");
     adapter.setOutputChannel(bridgeToModuleChannel);
     adapter.setBeanName("inbound." + name);
     DefaultAmqpHeaderMapper mapper = new DefaultAmqpHeaderMapper();
     mapper.setRequestHeaderNames(
         properties.getRequestHeaderPattens(this.defaultRequestHeaderPatterns));
     mapper.setReplyHeaderNames(properties.getReplyHeaderPattens(this.defaultReplyHeaderPatterns));
     adapter.setHeaderMapper(mapper);
     adapter.afterPropertiesSet();
     Binding consumerBinding = Binding.forConsumer(name, adapter, moduleInputChannel, properties);
     addBinding(consumerBinding);
     ReceivingHandler convertingBridge = new ReceivingHandler();
     convertingBridge.setOutputChannel(moduleInputChannel);
     convertingBridge.setBeanName(name + ".convert.bridge");
     convertingBridge.afterPropertiesSet();
     bridgeToModuleChannel.subscribe(convertingBridge);
     consumerBinding.start();
   } finally {
     Thread.currentThread().setContextClassLoader(originalClassloader);
   }
 }