Search in sources :

Example 11 with SmartLifecycle

use of org.springframework.context.SmartLifecycle in project spring-integration by spring-projects.

the class XPathHeaderEnricherParserTests method testParse.

@Test
public void testParse() throws Exception {
    EventDrivenConsumer consumer = (EventDrivenConsumer) context.getBean("parseOnly");
    assertEquals(2, TestUtils.getPropertyValue(consumer, "handler.order"));
    assertEquals(123L, TestUtils.getPropertyValue(consumer, "handler.messagingTemplate.sendTimeout"));
    assertEquals(-1, TestUtils.getPropertyValue(consumer, "phase"));
    assertFalse(TestUtils.getPropertyValue(consumer, "autoStartup", Boolean.class));
    SmartLifecycleRoleController roleController = context.getBean(SmartLifecycleRoleController.class);
    @SuppressWarnings("unchecked") List<SmartLifecycle> list = (List<SmartLifecycle>) TestUtils.getPropertyValue(roleController, "lifecycles", MultiValueMap.class).get("foo");
    assertThat(list, contains((SmartLifecycle) consumer));
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) SmartLifecycleRoleController(org.springframework.integration.support.SmartLifecycleRoleController) List(java.util.List) SmartLifecycle(org.springframework.context.SmartLifecycle) Test(org.junit.Test)

Example 12 with SmartLifecycle

use of org.springframework.context.SmartLifecycle in project spring-integration by spring-projects.

the class XPathRouterParserTests method testParse.

@Test
public void testParse() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("XPathRouterTests-context.xml", this.getClass());
    EventDrivenConsumer consumer = (EventDrivenConsumer) context.getBean("parseOnly");
    assertEquals(2, TestUtils.getPropertyValue(consumer, "handler.order"));
    assertEquals(123L, TestUtils.getPropertyValue(consumer, "handler.messagingTemplate.sendTimeout"));
    assertEquals(-1, TestUtils.getPropertyValue(consumer, "phase"));
    assertFalse(TestUtils.getPropertyValue(consumer, "autoStartup", Boolean.class));
    SmartLifecycleRoleController roleController = context.getBean(SmartLifecycleRoleController.class);
    @SuppressWarnings("unchecked") List<SmartLifecycle> list = (List<SmartLifecycle>) TestUtils.getPropertyValue(roleController, "lifecycles", MultiValueMap.class).get("foo");
    assertThat(list, contains((SmartLifecycle) consumer));
    context.close();
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) SmartLifecycleRoleController(org.springframework.integration.support.SmartLifecycleRoleController) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) List(java.util.List) SmartLifecycle(org.springframework.context.SmartLifecycle) Test(org.junit.Test)

Example 13 with SmartLifecycle

use of org.springframework.context.SmartLifecycle in project spring-integration by spring-projects.

the class XsltPayloadTransformerParserTests method testParse.

@Test
public void testParse() throws Exception {
    EventDrivenConsumer consumer = (EventDrivenConsumer) applicationContext.getBean("parseOnly");
    assertEquals(2, TestUtils.getPropertyValue(consumer, "handler.order"));
    assertEquals(123L, TestUtils.getPropertyValue(consumer, "handler.messagingTemplate.sendTimeout"));
    assertEquals(-1, TestUtils.getPropertyValue(consumer, "phase"));
    assertFalse(TestUtils.getPropertyValue(consumer, "autoStartup", Boolean.class));
    SmartLifecycleRoleController roleController = applicationContext.getBean(SmartLifecycleRoleController.class);
    @SuppressWarnings("unchecked") List<SmartLifecycle> list = (List<SmartLifecycle>) TestUtils.getPropertyValue(roleController, "lifecycles", MultiValueMap.class).get("foo");
    assertThat(list, contains((SmartLifecycle) consumer));
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) SmartLifecycleRoleController(org.springframework.integration.support.SmartLifecycleRoleController) List(java.util.List) SmartLifecycle(org.springframework.context.SmartLifecycle) Test(org.junit.Test)

Example 14 with SmartLifecycle

use of org.springframework.context.SmartLifecycle in project spring-integration by spring-projects.

the class XmlPayloadValidatingFilterParserTests method testParse.

@Test
public void testParse() throws Exception {
    EventDrivenConsumer consumer = (EventDrivenConsumer) ac.getBean("parseOnly");
    assertEquals(2, TestUtils.getPropertyValue(consumer, "handler.order"));
    assertEquals(123L, TestUtils.getPropertyValue(consumer, "handler.messagingTemplate.sendTimeout"));
    assertEquals(-1, TestUtils.getPropertyValue(consumer, "phase"));
    assertFalse(TestUtils.getPropertyValue(consumer, "autoStartup", Boolean.class));
    SmartLifecycleRoleController roleController = ac.getBean(SmartLifecycleRoleController.class);
    @SuppressWarnings("unchecked") List<SmartLifecycle> list = (List<SmartLifecycle>) TestUtils.getPropertyValue(roleController, "lifecycles", MultiValueMap.class).get("foo");
    assertThat(list, contains((SmartLifecycle) consumer));
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) SmartLifecycleRoleController(org.springframework.integration.support.SmartLifecycleRoleController) List(java.util.List) SmartLifecycle(org.springframework.context.SmartLifecycle) Test(org.junit.Test)

Example 15 with SmartLifecycle

use of org.springframework.context.SmartLifecycle in project spring-framework by spring-projects.

the class DefaultLifecycleProcessor method doStop.

/**
 * Stop the specified bean as part of the given set of Lifecycle beans,
 * making sure that any beans that depends on it are stopped first.
 * @param lifecycleBeans a Map with bean name as key and Lifecycle instance as value
 * @param beanName the name of the bean to stop
 */
private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final String beanName, final CountDownLatch latch, final Set<String> countDownBeanNames) {
    Lifecycle bean = lifecycleBeans.remove(beanName);
    if (bean != null) {
        String[] dependentBeans = getBeanFactory().getDependentBeans(beanName);
        for (String dependentBean : dependentBeans) {
            doStop(lifecycleBeans, dependentBean, latch, countDownBeanNames);
        }
        try {
            if (bean.isRunning()) {
                if (bean instanceof SmartLifecycle) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Asking bean '" + beanName + "' of type [" + bean.getClass().getName() + "] to stop");
                    }
                    countDownBeanNames.add(beanName);
                    ((SmartLifecycle) bean).stop(() -> {
                        latch.countDown();
                        countDownBeanNames.remove(beanName);
                        if (logger.isDebugEnabled()) {
                            logger.debug("Bean '" + beanName + "' completed its stop procedure");
                        }
                    });
                } else {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Stopping bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
                    }
                    bean.stop();
                    if (logger.isDebugEnabled()) {
                        logger.debug("Successfully stopped bean '" + beanName + "'");
                    }
                }
            } else if (bean instanceof SmartLifecycle) {
                // Don't wait for beans that aren't running...
                latch.countDown();
            }
        } catch (Throwable ex) {
            if (logger.isWarnEnabled()) {
                logger.warn("Failed to stop bean '" + beanName + "'", ex);
            }
        }
    }
}
Also used : Lifecycle(org.springframework.context.Lifecycle) SmartLifecycle(org.springframework.context.SmartLifecycle) SmartLifecycle(org.springframework.context.SmartLifecycle)

Aggregations

SmartLifecycle (org.springframework.context.SmartLifecycle)16 Test (org.junit.Test)11 SmartLifecycleRoleController (org.springframework.integration.support.SmartLifecycleRoleController)8 List (java.util.List)7 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)7 Lifecycle (org.springframework.context.Lifecycle)3 LinkedHashMap (java.util.LinkedHashMap)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Log (org.apache.commons.logging.Log)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 InOrder (org.mockito.InOrder)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 EnableMessageHistory (org.springframework.integration.config.EnableMessageHistory)1