@Test
  public void testContainerWithDestNameNoCorrelation() throws Exception {
    BeanFactory beanFactory = mock(BeanFactory.class);
    when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME))
        .thenReturn(true);
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.initialize();
    when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class))
        .thenReturn(scheduler);
    final JmsOutboundGateway gateway = new JmsOutboundGateway();
    gateway.setBeanFactory(beanFactory);
    gateway.setConnectionFactory(getConnectionFactory());
    gateway.setRequestDestination(requestQueue4);
    gateway.setReplyDestinationName("reply4");
    gateway.setUseReplyContainer(true);
    gateway.afterPropertiesSet();
    gateway.start();
    final AtomicReference<Object> reply = new AtomicReference<Object>();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    Executors.newSingleThreadExecutor()
        .execute(
            () -> {
              latch1.countDown();
              try {
                reply.set(gateway.handleRequestMessage(new GenericMessage<String>("foo")));
              } finally {
                latch2.countDown();
              }
            });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    JmsTemplate template = new JmsTemplate();
    template.setConnectionFactory(getConnectionFactory());
    template.setReceiveTimeout(10000);
    javax.jms.Message request = template.receive(requestQueue4);
    assertNotNull(request);
    final javax.jms.Message jmsReply = request;
    template.send(
        request.getJMSReplyTo(),
        (MessageCreator)
            session -> {
              jmsReply.setJMSCorrelationID(jmsReply.getJMSMessageID());
              return jmsReply;
            });
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertNotNull(reply.get());

    gateway.stop();
    scheduler.destroy();
  }
  @Test
  public void testLazyContainerWithDest() throws Exception {
    BeanFactory beanFactory = mock(BeanFactory.class);
    when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME))
        .thenReturn(true);
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.initialize();
    when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class))
        .thenReturn(scheduler);
    final JmsOutboundGateway gateway = new JmsOutboundGateway();
    gateway.setBeanFactory(beanFactory);
    gateway.setConnectionFactory(getConnectionFactory());
    gateway.setRequestDestination(requestQueue7);
    gateway.setReplyDestination(replyQueue7);
    gateway.setCorrelationKey("JMSCorrelationID");
    gateway.setUseReplyContainer(true);
    gateway.setIdleReplyContainerTimeout(1, TimeUnit.SECONDS);
    gateway.setRequiresReply(true);
    gateway.setReceiveTimeout(20000);
    gateway.afterPropertiesSet();
    gateway.start();
    Executors.newSingleThreadExecutor()
        .execute(
            () -> {
              JmsTemplate template = new JmsTemplate();
              template.setConnectionFactory(getConnectionFactory());
              template.setReceiveTimeout(20000);
              receiveAndSend(template);
              receiveAndSend(template);
            });

    assertNotNull(gateway.handleRequestMessage(new GenericMessage<String>("foo")));
    DefaultMessageListenerContainer container =
        TestUtils.getPropertyValue(
            gateway, "replyContainer", DefaultMessageListenerContainer.class);
    int n = 0;
    while (n++ < 100 && container.isRunning()) {
      Thread.sleep(100);
    }
    assertFalse(container.isRunning());
    assertNotNull(gateway.handleRequestMessage(new GenericMessage<String>("foo")));
    assertTrue(container.isRunning());

    gateway.stop();
    assertFalse(container.isRunning());
    scheduler.destroy();
  }