Added AbstractConsumerFactoryBean and SplitterFactoryBean to support different consumer types that can only be initialized after the "ref" Object's type is known (for example, see INT-397).
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.config;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.endpoint.AbstractReplyProducingMessageConsumer;
|
||||
import org.springframework.integration.message.MessageConsumer;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Base class for FactoryBeans that create MessageConsumer instances.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public abstract class AbstractConsumerFactoryBean implements FactoryBean {
|
||||
|
||||
private volatile MessageConsumer consumer;
|
||||
|
||||
private volatile Object targetObject;
|
||||
|
||||
private volatile String targetMethodName;
|
||||
|
||||
private volatile MessageChannel outputChannel;
|
||||
|
||||
private volatile boolean initialized;
|
||||
|
||||
private final Object initializationMonitor = new Object();
|
||||
|
||||
|
||||
public void setTargetObject(Object targetObject) {
|
||||
this.targetObject = targetObject;
|
||||
}
|
||||
|
||||
public void setTargetMethodName(String targetMethodName) {
|
||||
this.targetMethodName = targetMethodName;
|
||||
}
|
||||
|
||||
public void setOutputChannel(MessageChannel outputChannel) {
|
||||
this.outputChannel = outputChannel;
|
||||
}
|
||||
|
||||
public Object getObject() throws Exception {
|
||||
if (this.consumer == null) {
|
||||
this.initializeConsumer();
|
||||
Assert.notNull(this.consumer, "failed to create MessageConsumer");
|
||||
if (this.outputChannel != null
|
||||
&& this.consumer instanceof AbstractReplyProducingMessageConsumer) {
|
||||
((AbstractReplyProducingMessageConsumer) this.consumer).setOutputChannel(this.outputChannel);
|
||||
}
|
||||
}
|
||||
return this.consumer;
|
||||
}
|
||||
|
||||
public Class<?> getObjectType() {
|
||||
if (this.consumer != null) {
|
||||
return this.consumer.getClass();
|
||||
}
|
||||
return MessageConsumer.class;
|
||||
}
|
||||
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void initializeConsumer() {
|
||||
synchronized (this.initializationMonitor) {
|
||||
if (this.initialized) {
|
||||
return;
|
||||
}
|
||||
this.consumer = this.createConsumer(this.targetObject, this.targetMethodName);
|
||||
this.initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses must implement this method to create the MessageConsumer.
|
||||
*/
|
||||
protected abstract MessageConsumer createConsumer(Object targetObject, String targetMethodName);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.config;
|
||||
|
||||
import org.springframework.integration.message.MessageConsumer;
|
||||
import org.springframework.integration.splitter.AbstractMessageSplitter;
|
||||
import org.springframework.integration.splitter.DefaultMessageSplitter;
|
||||
import org.springframework.integration.splitter.MethodInvokingSplitter;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Factory bean for creating a Message Splitter.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class SplitterFactoryBean extends AbstractConsumerFactoryBean {
|
||||
|
||||
@Override
|
||||
protected MessageConsumer createConsumer(Object targetObject, String targetMethodName) {
|
||||
if (targetObject == null) {
|
||||
Assert.isTrue(!StringUtils.hasText(targetMethodName),
|
||||
"'method' should only be provided when 'ref' is also provided");
|
||||
return new DefaultMessageSplitter();
|
||||
}
|
||||
if (targetObject instanceof AbstractMessageSplitter) {
|
||||
return (AbstractMessageSplitter) targetObject;
|
||||
}
|
||||
return (StringUtils.hasText(targetMethodName))
|
||||
? new MethodInvokingSplitter(targetObject, targetMethodName)
|
||||
: new MethodInvokingSplitter(targetObject);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,8 +20,6 @@ import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.splitter.DefaultMessageSplitter;
|
||||
import org.springframework.integration.splitter.MethodInvokingSplitter;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -33,17 +31,16 @@ public class SplitterParser extends AbstractConsumerEndpointParser {
|
||||
|
||||
@Override
|
||||
protected BeanDefinitionBuilder parseConsumer(Element element, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(SplitterFactoryBean.class);
|
||||
if (element.hasAttribute(REF_ATTRIBUTE)) {
|
||||
String ref = element.getAttribute(REF_ATTRIBUTE);
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MethodInvokingSplitter.class);
|
||||
builder.addConstructorArgReference(ref);
|
||||
builder.addPropertyReference("targetObject", ref);
|
||||
if (StringUtils.hasText(element.getAttribute(METHOD_ATTRIBUTE))) {
|
||||
String method = element.getAttribute(METHOD_ATTRIBUTE);
|
||||
builder.addConstructorArgValue(method);
|
||||
builder.addPropertyValue("targetMethodName", method);
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
return BeanDefinitionBuilder.genericBeanDefinition(DefaultMessageSplitter.class);
|
||||
return builder;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user