Search in sources :

Example 11 with Lifecycle

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

the class DefaultLifecycleProcessor method stopBeans.

private void stopBeans() {
    Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
    Map<Integer, LifecycleGroup> phases = new HashMap<>();
    for (Map.Entry<String, Lifecycle> entry : lifecycleBeans.entrySet()) {
        Lifecycle bean = entry.getValue();
        int shutdownOrder = getPhase(bean);
        LifecycleGroup group = phases.get(shutdownOrder);
        if (group == null) {
            group = new LifecycleGroup(shutdownOrder, this.timeoutPerShutdownPhase, lifecycleBeans, false);
            phases.put(shutdownOrder, group);
        }
        group.add(entry.getKey(), bean);
    }
    if (!phases.isEmpty()) {
        List<Integer> keys = new ArrayList<>(phases.keySet());
        Collections.sort(keys, Collections.reverseOrder());
        for (Integer key : keys) {
            phases.get(key).stop();
        }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Lifecycle(org.springframework.context.Lifecycle) SmartLifecycle(org.springframework.context.SmartLifecycle) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 12 with Lifecycle

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

the class DefaultLifecycleProcessor method startBeans.

// internal helpers
private void startBeans(boolean autoStartupOnly) {
    Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
    Map<Integer, LifecycleGroup> phases = new HashMap<>();
    for (Map.Entry<String, ? extends Lifecycle> entry : lifecycleBeans.entrySet()) {
        Lifecycle bean = entry.getValue();
        if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
            int phase = getPhase(bean);
            LifecycleGroup group = phases.get(phase);
            if (group == null) {
                group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
                phases.put(phase, group);
            }
            group.add(entry.getKey(), bean);
        }
    }
    if (!phases.isEmpty()) {
        List<Integer> keys = new ArrayList<>(phases.keySet());
        Collections.sort(keys);
        for (Integer key : keys) {
            phases.get(key).start();
        }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Lifecycle(org.springframework.context.Lifecycle) SmartLifecycle(org.springframework.context.SmartLifecycle) ArrayList(java.util.ArrayList) SmartLifecycle(org.springframework.context.SmartLifecycle) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 13 with Lifecycle

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

the class PollingLifecycleTests method testAdapterLifecycleIsPropagatedToMessageSource.

@Test
public void testAdapterLifecycleIsPropagatedToMessageSource() throws Exception {
    SourcePollingChannelAdapterFactoryBean adapterFactory = new SourcePollingChannelAdapterFactoryBean();
    adapterFactory.setOutputChannel(new NullChannel());
    adapterFactory.setBeanFactory(mock(ConfigurableBeanFactory.class));
    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setTrigger(new PeriodicTrigger(2000));
    adapterFactory.setPollerMetadata(pollerMetadata);
    final AtomicBoolean startInvoked = new AtomicBoolean();
    final AtomicBoolean stopInvoked = new AtomicBoolean();
    MethodInvokingMessageSource source = new MethodInvokingMessageSource();
    source.setObject(new Lifecycle() {

        @Override
        public void start() {
            startInvoked.set(true);
        }

        @Override
        public void stop() {
            stopInvoked.set(true);
        }

        @Override
        public boolean isRunning() {
            return false;
        }
    });
    source.setMethodName("isRunning");
    adapterFactory.setSource(source);
    SourcePollingChannelAdapter adapter = adapterFactory.getObject();
    adapter.setTaskScheduler(this.taskScheduler);
    adapter.start();
    adapter.stop();
    assertTrue(startInvoked.get());
    assertTrue(stopInvoked.get());
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SourcePollingChannelAdapterFactoryBean(org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean) Lifecycle(org.springframework.context.Lifecycle) NullChannel(org.springframework.integration.channel.NullChannel) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Test(org.junit.Test)

Example 14 with Lifecycle

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

the class IntegrationConfigUtils method registerRoleControllerDefinitionIfNecessary.

public static void registerRoleControllerDefinitionIfNecessary(BeanDefinitionRegistry registry) {
    if (!registry.containsBeanDefinition(IntegrationContextUtils.INTEGRATION_LIFECYCLE_ROLE_CONTROLLER)) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(SmartLifecycleRoleController.class);
        builder.addConstructorArgValue(new ManagedList<String>());
        builder.addConstructorArgValue(new ManagedList<Lifecycle>());
        registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_LIFECYCLE_ROLE_CONTROLLER, builder.getBeanDefinition());
    }
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) Lifecycle(org.springframework.context.Lifecycle)

Example 15 with Lifecycle

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

the class MockIntegrationContext method substituteMessageHandlerFor.

public void substituteMessageHandlerFor(String consumerEndpointId, MessageHandler mockMessageHandler, boolean autoStartup) {
    Object endpoint = this.beanFactory.getBean(consumerEndpointId, IntegrationConsumer.class);
    if (autoStartup && endpoint instanceof Lifecycle) {
        ((Lifecycle) endpoint).stop();
    }
    DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(endpoint);
    Object targetMessageHandler = directFieldAccessor.getPropertyValue("handler");
    this.beans.put(consumerEndpointId, targetMessageHandler);
    if (mockMessageHandler instanceof MessageProducer) {
        if (targetMessageHandler instanceof MessageProducer) {
            MessageChannel outputChannel = ((MessageProducer) targetMessageHandler).getOutputChannel();
            ((MessageProducer) mockMessageHandler).setOutputChannel(outputChannel);
        } else {
            if (mockMessageHandler instanceof MockMessageHandler) {
                if (TestUtils.getPropertyValue(mockMessageHandler, "hasReplies", Boolean.class)) {
                    throw new IllegalStateException("The [" + mockMessageHandler + "] " + "with replies can't replace simple MessageHandler [" + targetMessageHandler + "]");
                }
            } else {
                throw new IllegalStateException("The MessageProducer handler [" + mockMessageHandler + "] " + "can't replace simple MessageHandler [" + targetMessageHandler + "]");
            }
        }
    }
    directFieldAccessor.setPropertyValue("handler", mockMessageHandler);
    if (autoStartup && endpoint instanceof Lifecycle) {
        ((Lifecycle) endpoint).start();
    }
}
Also used : MockMessageHandler(org.springframework.integration.test.mock.MockMessageHandler) MessageChannel(org.springframework.messaging.MessageChannel) Lifecycle(org.springframework.context.Lifecycle) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MessageProducer(org.springframework.integration.core.MessageProducer)

Aggregations

Lifecycle (org.springframework.context.Lifecycle)25 SmartLifecycle (org.springframework.context.SmartLifecycle)6 MessageChannel (org.springframework.messaging.MessageChannel)6 Test (org.junit.Test)4 MessageProducer (org.springframework.integration.core.MessageProducer)4 LinkedHashMap (java.util.LinkedHashMap)3 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 MessageHandler (org.springframework.messaging.MessageHandler)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 JMException (javax.management.JMException)2 Before (org.junit.Before)2 Test (org.junit.jupiter.api.Test)2 TargetSource (org.springframework.aop.TargetSource)2 Advised (org.springframework.aop.framework.Advised)2