INT-1257, INT-1263 Refactoring Message History (work in progress): Replaced MessageHistoryBeanPostProcessor with MessageHistoryConfigurer in order to set tracking parameters at the right time.

This commit is contained in:
Mark Fisher
2010-08-26 17:58:18 +00:00
parent 6da90a4c51
commit f535cdbcd1
6 changed files with 116 additions and 50 deletions

View File

@@ -30,12 +30,12 @@ import org.springframework.beans.factory.xml.ParserContext;
*/
public class MessageHistoryParser extends AbstractSimpleBeanDefinitionParser {
private static final String POST_PROCESSOR_CLASSNAME = "org.springframework.integration.history.MessageHistoryBeanPostProcessor";
private static final String CONFIGURER_CLASSNAME = "org.springframework.integration.history.MessageHistoryConfigurer";
@Override
protected String getBeanClassName(Element element) {
return POST_PROCESSOR_CLASSNAME;
return CONFIGURER_CLASSNAME;
}
@Override
@@ -44,10 +44,10 @@ public class MessageHistoryParser extends AbstractSimpleBeanDefinitionParser {
}
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) {
if (parserContext.getRegistry().containsBeanDefinition(POST_PROCESSOR_CLASSNAME)) {
throw new BeanDefinitionStoreException("At most one MessageHistoryBeanPostProcessor may be registered within a context.");
if (parserContext.getRegistry().containsBeanDefinition(CONFIGURER_CLASSNAME)) {
throw new BeanDefinitionStoreException("At most one MessageHistoryConfigurer may be registered within a context.");
}
return POST_PROCESSOR_CLASSNAME;
return CONFIGURER_CLASSNAME;
}
}

View File

@@ -46,6 +46,7 @@ import org.springframework.integration.history.TrackableComponent;
import org.springframework.integration.mapping.InboundMessageMapper;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
/**
@@ -161,6 +162,11 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint implements Trackab
public void setShouldTrack(boolean shouldTrack) {
this.shouldTrack = shouldTrack;
if (!CollectionUtils.isEmpty(this.gatewayMap)) {
for (SimpleMessagingGateway gateway : this.gatewayMap.values()) {
gateway.setShouldTrack(shouldTrack);
}
}
}
public void setTypeConverter(TypeConverter typeConverter) {

View File

@@ -1,43 +0,0 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.history;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* @author Mark Fisher
* @since 2.0
*/
//TODO: check name against pattern (include/exclude filters?), and check type as well?
public class MessageHistoryBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof TrackableComponent) {
((TrackableComponent) bean).setShouldTrack(true);
}
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof TrackableComponent) {
((TrackableComponent) bean).setShouldTrack(true);
}
return bean;
}
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.history;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.context.SmartLifecycle;
import org.springframework.util.Assert;
import org.springframework.util.PatternMatchUtils;
/**
* @author Mark Fisher
* @since 2.0
*/
public class MessageHistoryConfigurer implements SmartLifecycle, BeanFactoryAware {
private final Log logger = LogFactory.getLog(this.getClass());
private volatile String[] componentNamePatterns = new String[] { "*" };
private volatile BeanFactory beanFactory;
private volatile boolean running;
private volatile boolean autoStartup = true;
private int phase = Integer.MIN_VALUE;
public void setComponentNamePatterns(String[] componentNamePatterns) {
Assert.notEmpty(componentNamePatterns, "componentNamePatterns must not be empty");
this.componentNamePatterns = componentNamePatterns;
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
/*
* SmartLifecycle implementation
*/
public boolean isRunning() {
return this.running;
}
public boolean isAutoStartup() {
return this.autoStartup;
}
public int getPhase() {
return this.phase;
}
public void start() {
if (this.beanFactory != null && this.beanFactory instanceof ListableBeanFactory) {
Map<String, TrackableComponent> trackableComponents = BeanFactoryUtils.beansOfTypeIncludingAncestors(
(ListableBeanFactory) this.beanFactory, TrackableComponent.class);
for (TrackableComponent component : trackableComponents.values()) {
String componentName = component.getComponentName();
boolean shouldTrack = PatternMatchUtils.simpleMatch(this.componentNamePatterns, componentName);
component.setShouldTrack(shouldTrack);
if (shouldTrack && this.logger.isInfoEnabled()) {
this.logger.info("Enabling MessageHistory tracking for component '" + componentName + "'");
}
}
}
this.running = true;
}
public void stop() {
this.running = false;
}
public void stop(Runnable callback) {
this.stop();
callback.run();
}
}

View File

@@ -30,6 +30,6 @@
<int:channel id="endOfThePipeChannel"/>
<bean class="org.springframework.integration.history.MessageHistoryBeanPostProcessor"/>
<bean class="org.springframework.integration.history.MessageHistoryConfigurer"/>
</beans>

View File

@@ -40,6 +40,6 @@
<property name="cacheProducers" value="false"/>
</bean>
<bean class="org.springframework.integration.history.MessageHistoryBeanPostProcessor"/>
<bean class="org.springframework.integration.history.MessageHistoryConfigurer"/>
</beans>