use of org.springframework.context.Lifecycle in project spring-integration by spring-projects.
the class MockIntegrationContext method substituteMessageSourceFor.
private void substituteMessageSourceFor(String endpointId, Object messagingComponent, Class<?> endpointClass, String property, boolean autoStartup) {
Object endpoint = this.beanFactory.getBean(endpointId, endpointClass);
if (autoStartup && endpoint instanceof Lifecycle) {
((Lifecycle) endpoint).stop();
}
DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(endpoint);
this.beans.put(endpointId, directFieldAccessor.getPropertyValue(property));
directFieldAccessor.setPropertyValue(property, messagingComponent);
if (autoStartup && endpoint instanceof Lifecycle) {
((Lifecycle) endpoint).start();
}
}
use of org.springframework.context.Lifecycle in project spring-integration by spring-projects.
the class IntegrationMBeanExporter method enhanceSourceMonitor.
private MessageSourceMetrics enhanceSourceMonitor(MessageSourceMetrics monitor) {
MessageSourceMetrics result = monitor;
if (monitor.getManagedName() != null) {
return monitor;
}
// Assignment algorithm and bean id, with bean id pulled reflectively out of enclosing endpoint if possible
String[] names = this.applicationContext.getBeanNamesForType(AbstractEndpoint.class);
String name = null;
String endpointName = null;
String source = "endpoint";
Object endpoint = null;
for (String beanName : names) {
endpoint = this.applicationContext.getBean(beanName);
Object field = null;
if (monitor instanceof MessagingGatewaySupport && endpoint == monitor) {
field = monitor;
} else {
try {
field = extractTarget(getField(endpoint, "source"));
} catch (Exception e) {
logger.trace("Could not get source from bean = " + beanName);
}
}
if (field == monitor) {
name = beanName;
endpointName = beanName;
break;
}
}
if (endpointName == null) {
endpoint = null;
}
if (name != null && endpoint != null && name.startsWith("_org.springframework.integration")) {
name = getInternalComponentName(name);
source = "internal";
}
if (name != null && endpoint != null && name.startsWith("org.springframework.integration")) {
Object target = endpoint;
if (endpoint instanceof Advised) {
TargetSource targetSource = ((Advised) endpoint).getTargetSource();
if (targetSource != null) {
try {
target = targetSource.getTarget();
} catch (Exception e) {
logger.error("Could not get handler from bean = " + name);
}
}
}
Object outputChannel = null;
if (target instanceof MessagingGatewaySupport) {
outputChannel = ((MessagingGatewaySupport) target).getRequestChannel();
} else {
outputChannel = getField(target, "outputChannel");
}
if (outputChannel != null) {
if (!this.anonymousSourceCounters.containsKey(outputChannel)) {
this.anonymousSourceCounters.put(outputChannel, new AtomicLong());
}
AtomicLong count = this.anonymousSourceCounters.get(outputChannel);
long total = count.incrementAndGet();
String suffix = "";
/*
* Short hack to makes sure object names are unique if more than one endpoint has the same input channel
*/
if (total > 1) {
suffix = "#" + total;
}
name = outputChannel + suffix;
source = "anonymous";
}
}
if (endpoint instanceof Lifecycle) {
// Wrap the monitor in a lifecycle so it exposes the start/stop operations
if (endpoint instanceof TrackableComponent) {
if (monitor instanceof MessageSourceManagement) {
result = new LifecycleTrackableMessageSourceManagement((Lifecycle) endpoint, (MessageSourceManagement) monitor);
} else {
result = new LifecycleTrackableMessageSourceMetrics((Lifecycle) endpoint, monitor);
}
} else {
if (monitor instanceof MessageSourceManagement) {
result = new LifecycleMessageSourceManagement((Lifecycle) endpoint, (MessageSourceManagement) monitor);
} else {
result = new LifecycleMessageSourceMetrics((Lifecycle) endpoint, monitor);
}
}
}
if (name == null) {
name = monitor.toString();
source = "source";
}
if (endpointName != null) {
this.beansByEndpointName.put(name, endpointName);
}
monitor.setManagedType(source);
monitor.setManagedName(name);
return result;
}
use of org.springframework.context.Lifecycle in project spring-integration by spring-projects.
the class IntegrationMBeanExporter method enhanceHandlerMonitor.
private MessageHandlerMetrics enhanceHandlerMonitor(MessageHandlerMetrics monitor) {
MessageHandlerMetrics result = monitor;
if (monitor.getManagedName() != null && monitor.getManagedType() != null) {
return monitor;
}
// Assignment algorithm and bean id, with bean id pulled reflectively out of enclosing endpoint if possible
String[] names = this.applicationContext.getBeanNamesForType(AbstractEndpoint.class);
String name = null;
String endpointName = null;
String source = "endpoint";
Object endpoint = null;
for (String beanName : names) {
endpoint = this.applicationContext.getBean(beanName);
try {
Object field = extractTarget(getField(endpoint, "handler"));
if (field == monitor || this.extractTarget(this.handlerInAnonymousWrapper(field)) == monitor) {
name = beanName;
endpointName = beanName;
break;
}
} catch (Exception e) {
logger.trace("Could not get handler from bean = " + beanName);
}
}
if (name != null && endpoint != null && name.startsWith("_org.springframework.integration")) {
name = getInternalComponentName(name);
source = "internal";
}
if (name != null && endpoint != null && name.startsWith("org.springframework.integration")) {
Object target = endpoint;
if (endpoint instanceof Advised) {
TargetSource targetSource = ((Advised) endpoint).getTargetSource();
if (targetSource != null) {
try {
target = targetSource.getTarget();
} catch (Exception e) {
logger.error("Could not get handler from bean = " + name);
}
}
}
Object field = getField(target, "inputChannel");
if (field != null) {
if (!this.anonymousHandlerCounters.containsKey(field)) {
this.anonymousHandlerCounters.put(field, new AtomicLong());
}
AtomicLong count = this.anonymousHandlerCounters.get(field);
long total = count.incrementAndGet();
String suffix = "";
/*
* Short hack to makes sure object names are unique if more than one endpoint has the same input channel
*/
if (total > 1) {
suffix = "#" + total;
}
name = field + suffix;
source = "anonymous";
}
}
if (endpoint instanceof Lifecycle) {
// Wrap the monitor in a lifecycle so it exposes the start/stop operations
if (monitor instanceof MappingMessageRouterManagement) {
if (monitor instanceof TrackableComponent) {
result = new TrackableRouterMetrics((Lifecycle) endpoint, (MappingMessageRouterManagement) monitor);
} else {
result = new RouterMetrics((Lifecycle) endpoint, (MappingMessageRouterManagement) monitor);
}
} else {
if (monitor instanceof TrackableComponent) {
result = new LifecycleTrackableMessageHandlerMetrics((Lifecycle) endpoint, monitor);
} else {
result = new LifecycleMessageHandlerMetrics((Lifecycle) endpoint, monitor);
}
}
}
if (name == null) {
if (monitor instanceof NamedComponent) {
name = ((NamedComponent) monitor).getComponentName();
}
if (name == null) {
name = monitor.toString();
}
source = "handler";
}
if (endpointName != null) {
this.beansByEndpointName.put(name, endpointName);
}
monitor.setManagedType(source);
monitor.setManagedName(name);
return result;
}
use of org.springframework.context.Lifecycle in project spring-integration by spring-projects.
the class IntegrationMBeanExporter method afterSingletonsInstantiated.
@Override
public void afterSingletonsInstantiated() {
Map<String, MessageHandlerMetrics> messageHandlers = this.applicationContext.getBeansOfType(MessageHandlerMetrics.class);
for (Entry<String, MessageHandlerMetrics> entry : messageHandlers.entrySet()) {
String beanName = entry.getKey();
MessageHandlerMetrics bean = entry.getValue();
if (this.handlerInAnonymousWrapper(bean) != null) {
if (logger.isDebugEnabled()) {
logger.debug("Skipping " + beanName + " because it wraps another handler");
}
continue;
}
// If the handler is proxied, we have to extract the target to expose as an MBean.
// The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
MessageHandlerMetrics monitor = (MessageHandlerMetrics) extractTarget(bean);
this.handlers.add(monitor);
}
Map<String, MessageSourceMetrics> messageSources = this.applicationContext.getBeansOfType(MessageSourceMetrics.class);
for (Entry<String, MessageSourceMetrics> entry : messageSources.entrySet()) {
// If the source is proxied, we have to extract the target to expose as an MBean.
// The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
MessageSourceMetrics monitor = (MessageSourceMetrics) extractTarget(entry.getValue());
this.sources.add(monitor);
}
Map<String, MessageChannelMetrics> messageChannels = this.applicationContext.getBeansOfType(MessageChannelMetrics.class);
for (Entry<String, MessageChannelMetrics> entry : messageChannels.entrySet()) {
// If the channel is proxied, we have to extract the target to expose as an MBean.
// The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
MessageChannelMetrics monitor = (MessageChannelMetrics) extractTarget(entry.getValue());
this.channels.add(monitor);
}
Map<String, MessageProducer> messageProducers = this.applicationContext.getBeansOfType(MessageProducer.class);
for (Entry<String, MessageProducer> entry : messageProducers.entrySet()) {
MessageProducer messageProducer = entry.getValue();
if (messageProducer instanceof Lifecycle) {
Lifecycle target = (Lifecycle) extractTarget(messageProducer);
if (!(target instanceof AbstractMessageProducingHandler)) {
this.inboundLifecycleMessageProducers.add(target);
}
}
}
super.afterSingletonsInstantiated();
try {
registerChannels();
registerHandlers();
registerSources();
registerEndpoints();
if (this.applicationContext.containsBean(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME)) {
Object messageHistoryConfigurer = this.applicationContext.getBean(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME);
if (messageHistoryConfigurer instanceof MessageHistoryConfigurer) {
registerBeanInstance(messageHistoryConfigurer, IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME);
}
}
if (!this.applicationContext.containsBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME)) {
this.managementConfigurer = new IntegrationManagementConfigurer();
this.managementConfigurer.setDefaultCountsEnabled(true);
this.managementConfigurer.setDefaultStatsEnabled(true);
this.managementConfigurer.setApplicationContext(this.applicationContext);
this.managementConfigurer.setBeanName(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME);
this.managementConfigurer.afterSingletonsInstantiated();
} else {
this.managementConfigurer = this.applicationContext.getBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME, IntegrationManagementConfigurer.class);
}
} catch (RuntimeException e) {
unregisterBeans();
throw e;
}
}
use of org.springframework.context.Lifecycle 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);
}
}
}
}
Aggregations