Added channel reference to Subscription and refactored/simplified TargetAdapter (implements MessageHandler).
This commit is contained in:
@@ -19,12 +19,10 @@ package org.springframework.integration.adapter;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.handler.MessageHandler;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageMapper;
|
||||
import org.springframework.integration.message.SimplePayloadMessageMapper;
|
||||
import org.springframework.integration.scheduling.PollingSchedule;
|
||||
import org.springframework.integration.scheduling.Schedule;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -32,35 +30,12 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public abstract class AbstractTargetAdapter<T> implements TargetAdapter {
|
||||
public abstract class AbstractTargetAdapter<T> implements MessageHandler {
|
||||
|
||||
protected Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
private String name;
|
||||
|
||||
private MessageChannel channel;
|
||||
|
||||
private MessageMapper<?,T> mapper = new SimplePayloadMessageMapper<T>();
|
||||
|
||||
private Schedule schedule = new PollingSchedule(5);
|
||||
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setChannel(MessageChannel channel) {
|
||||
Assert.notNull(channel, "'channel' must not be null");
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
public MessageChannel getChannel() {
|
||||
return this.channel;
|
||||
}
|
||||
|
||||
public void setMessageMapper(MessageMapper<?,T> mapper) {
|
||||
Assert.notNull(mapper, "'mapper' must not be null");
|
||||
@@ -71,15 +46,6 @@ public abstract class AbstractTargetAdapter<T> implements TargetAdapter {
|
||||
return this.mapper;
|
||||
}
|
||||
|
||||
public void setSchedule(Schedule schedule) {
|
||||
Assert.notNull(schedule, "'schedule' must not be null");
|
||||
this.schedule = schedule;
|
||||
}
|
||||
|
||||
public Schedule getSchedule() {
|
||||
return this.schedule;
|
||||
}
|
||||
|
||||
public final Message handle(Message message) {
|
||||
this.sendToTarget(this.mapper.fromMessage(message));
|
||||
return null;
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2007 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.adapter;
|
||||
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.handler.MessageHandler;
|
||||
|
||||
/**
|
||||
* Base interface for target adapters.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public interface TargetAdapter extends MessageHandler {
|
||||
|
||||
String getName();
|
||||
|
||||
void setChannel(MessageChannel channel);
|
||||
|
||||
}
|
||||
@@ -28,11 +28,11 @@ import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.Lifecycle;
|
||||
import org.springframework.integration.MessagingConfigurationException;
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.integration.adapter.AbstractTargetAdapter;
|
||||
import org.springframework.integration.adapter.SourceAdapter;
|
||||
import org.springframework.integration.adapter.TargetAdapter;
|
||||
import org.springframework.integration.channel.ChannelRegistry;
|
||||
import org.springframework.integration.channel.ChannelRegistryAware;
|
||||
import org.springframework.integration.channel.DefaultChannelRegistry;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.channel.SimpleChannel;
|
||||
@@ -40,6 +40,7 @@ import org.springframework.integration.dispatcher.DefaultMessageDispatcher;
|
||||
import org.springframework.integration.dispatcher.DispatcherPolicy;
|
||||
import org.springframework.integration.dispatcher.MessageDispatcher;
|
||||
import org.springframework.integration.endpoint.ConcurrencyPolicy;
|
||||
import org.springframework.integration.endpoint.DefaultMessageEndpoint;
|
||||
import org.springframework.integration.endpoint.MessageEndpoint;
|
||||
import org.springframework.integration.handler.MessageHandler;
|
||||
import org.springframework.integration.handler.PooledMessageHandler;
|
||||
@@ -50,7 +51,6 @@ import org.springframework.integration.scheduling.Schedule;
|
||||
import org.springframework.integration.scheduling.SimpleMessagingTaskScheduler;
|
||||
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* The messaging bus. Serves as a registry for channels and endpoints, manages their lifecycle,
|
||||
@@ -64,7 +64,7 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
|
||||
|
||||
private ChannelRegistry channelRegistry = new DefaultChannelRegistry();
|
||||
|
||||
private Map<String, MessageHandler> handlers = new ConcurrentHashMap<String, MessageHandler>();
|
||||
private Map<String, MessageEndpoint> endpoints = new ConcurrentHashMap<String, MessageEndpoint>();
|
||||
|
||||
private Map<MessageChannel, MessageDispatcher> dispatchers = new ConcurrentHashMap<MessageChannel, MessageDispatcher>();
|
||||
|
||||
@@ -90,8 +90,6 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
|
||||
this.registerChannels(applicationContext);
|
||||
this.registerEndpoints(applicationContext);
|
||||
this.registerSourceAdapters(applicationContext);
|
||||
this.registerTargetAdapters(applicationContext);
|
||||
this.activateSubscriptions(applicationContext);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,28 +138,6 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void registerTargetAdapters(ApplicationContext context) {
|
||||
Map<String, TargetAdapter> targetAdapterBeans =
|
||||
(Map<String, TargetAdapter>) context.getBeansOfType(TargetAdapter.class);
|
||||
for (Map.Entry<String, TargetAdapter> entry : targetAdapterBeans.entrySet()) {
|
||||
this.registerTargetAdapter(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void activateSubscriptions(ApplicationContext context) {
|
||||
Map<String, Subscription> subscriptionBeans =
|
||||
(Map<String, Subscription>) context.getBeansOfType(Subscription.class);
|
||||
for (Subscription subscription : subscriptionBeans.values()) {
|
||||
this.activateSubscription(subscription);
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("activated subscription to channel '" + subscription.getChannel() +
|
||||
"' for handler '" + subscription.getHandler() + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void initialize() {
|
||||
if (this.getInvalidMessageChannel() == null) {
|
||||
this.setInvalidMessageChannel(new SimpleChannel(Integer.MAX_VALUE));
|
||||
@@ -212,32 +188,72 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
|
||||
}
|
||||
this.dispatchers.put(channel, dispatcher);
|
||||
this.channelRegistry.registerChannel(name, channel);
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("registered channel '" + name + "'");
|
||||
}
|
||||
}
|
||||
|
||||
public void registerEndpoint(String name, MessageEndpoint endpoint) {
|
||||
public void registerHandler(String name, MessageHandler handler, Subscription subscription) {
|
||||
this.registerHandler(name, handler, subscription, null);
|
||||
}
|
||||
|
||||
public void registerHandler(String name, MessageHandler handler, Subscription subscription, ConcurrencyPolicy concurrencyPolicy) {
|
||||
if (!this.initialized) {
|
||||
this.initialize();
|
||||
}
|
||||
Assert.notNull(name, "'name' must not be null");
|
||||
Assert.notNull(endpoint, "'endpoint' must not be null");
|
||||
Assert.notNull(handler, "'handler' must not be null");
|
||||
Assert.notNull(subscription, "'subscription' must not be null");
|
||||
DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint();
|
||||
endpoint.setName(name);
|
||||
this.handlers.put(name, endpoint);
|
||||
endpoint.setChannelRegistry(this);
|
||||
Schedule schedule = endpoint.getSchedule();
|
||||
if (endpoint.getInputChannelName() != null) {
|
||||
Subscription subscription = new Subscription();
|
||||
subscription.setHandler(name);
|
||||
subscription.setChannel(endpoint.getInputChannelName());
|
||||
if (schedule != null) {
|
||||
subscription.setSchedule(schedule);
|
||||
}
|
||||
this.activateSubscription(subscription);
|
||||
endpoint.setHandler(handler);
|
||||
endpoint.setSubscription(subscription);
|
||||
endpoint.setConcurrencyPolicy(concurrencyPolicy);
|
||||
this.registerEndpoint(name, endpoint);
|
||||
}
|
||||
|
||||
public void registerEndpoint(String name, MessageEndpoint endpoint) {
|
||||
if (endpoint instanceof ChannelRegistryAware) {
|
||||
((ChannelRegistryAware) endpoint).setChannelRegistry(this.channelRegistry);
|
||||
}
|
||||
if (this.autoCreateChannels) {
|
||||
String defaultOutputChannelName = endpoint.getDefaultOutputChannelName();
|
||||
if (StringUtils.hasText(defaultOutputChannelName) && this.lookupChannel(defaultOutputChannelName) == null) {
|
||||
this.registerChannel(defaultOutputChannelName, new SimpleChannel());
|
||||
this.endpoints.put(name, endpoint);
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("registered endpoint '" + name + "'");
|
||||
}
|
||||
}
|
||||
|
||||
private void activateEndpoints() {
|
||||
for (MessageEndpoint endpoint : this.endpoints.values()) {
|
||||
this.activateEndpoint(endpoint);
|
||||
}
|
||||
}
|
||||
|
||||
private void activateEndpoint(MessageEndpoint endpoint) {
|
||||
Subscription subscription = endpoint.getSubscription();
|
||||
MessageChannel channel = subscription.getChannel();
|
||||
if (channel == null) {
|
||||
String channelName = subscription.getChannelName();
|
||||
if (channelName == null) {
|
||||
throw new MessagingConfigurationException("endpoint '" + endpoint.getName() +
|
||||
"' must provide either 'channel' or 'channelName' in its subscription metadata");
|
||||
}
|
||||
channel = this.lookupChannel(channelName);
|
||||
if (channel == null) {
|
||||
if (this.autoCreateChannels == false) {
|
||||
throw new MessagingException("Cannot activate subscription, unknown channel '" + channelName +
|
||||
"'. Consider enabling the 'autoCreateChannels' option for the message bus.");
|
||||
}
|
||||
if (this.logger.isInfoEnabled()) {
|
||||
logger.info("auto-creating channel '" + channel.getName() + "'");
|
||||
}
|
||||
channel = new SimpleChannel();
|
||||
this.registerChannel(channelName, channel);
|
||||
}
|
||||
}
|
||||
this.registerWithDispatcher(channel, endpoint, subscription.getSchedule(), endpoint.getConcurrencyPolicy());
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("activated subscription to channel '" + channel.getName() +
|
||||
"' for endpoint '" + endpoint.getName() + "'");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,56 +275,11 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
|
||||
}
|
||||
}
|
||||
|
||||
public void registerTargetAdapter(String name, TargetAdapter targetAdapter) {
|
||||
if (targetAdapter instanceof AbstractTargetAdapter) {
|
||||
AbstractTargetAdapter<?> adapter = (AbstractTargetAdapter<?>) targetAdapter;
|
||||
adapter.setName(name);
|
||||
this.handlers.put(name, adapter);
|
||||
MessageChannel channel = adapter.getChannel();
|
||||
Schedule schedule = adapter.getSchedule();
|
||||
ConcurrencyPolicy concurrencyPolicy = new ConcurrencyPolicy();
|
||||
concurrencyPolicy.setCoreConcurrency(1);
|
||||
concurrencyPolicy.setMaxConcurrency(1);
|
||||
this.doActivate(channel, adapter, schedule, concurrencyPolicy);
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("registered target adapter '" + name + "'");
|
||||
}
|
||||
}
|
||||
|
||||
public void activateSubscription(Subscription subscription) {
|
||||
String channelName = subscription.getChannel();
|
||||
String handlerName = subscription.getHandler();
|
||||
Schedule schedule = subscription.getSchedule();
|
||||
ConcurrencyPolicy concurrencyPolicy = subscription.getConcurrencyPolicy();
|
||||
MessageHandler handler = this.handlers.get(handlerName);
|
||||
if (handler == null) {
|
||||
throw new MessagingException("Cannot activate subscription, unknown handler '" + handlerName + "'");
|
||||
}
|
||||
MessageChannel channel = this.lookupChannel(channelName);
|
||||
if (channel == null) {
|
||||
if (this.autoCreateChannels == false) {
|
||||
throw new MessagingException("Cannot activate subscription, unknown channel '" + channelName +
|
||||
"'. Consider enabling the 'autoCreateChannels' option for the message bus.");
|
||||
}
|
||||
if (this.logger.isInfoEnabled()) {
|
||||
logger.info("auto-creating channel '" + channelName + "'");
|
||||
}
|
||||
channel = new SimpleChannel();
|
||||
this.registerChannel(channelName, channel);
|
||||
}
|
||||
this.doActivate(channel, handler, schedule, concurrencyPolicy);
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("activated subscription to channel '" + channelName +
|
||||
"' for handler '" + handlerName + "'");
|
||||
}
|
||||
}
|
||||
|
||||
private void doActivate(MessageChannel channel, MessageHandler handler, Schedule schedule, ConcurrencyPolicy concurrencyPolicy) {
|
||||
private void registerWithDispatcher(MessageChannel channel, MessageHandler handler, Schedule schedule, ConcurrencyPolicy concurrencyPolicy) {
|
||||
MessageDispatcher dispatcher = dispatchers.get(channel);
|
||||
if (dispatcher == null) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("no dispatcher available for channel '" + channel + "', be sure to register the channel");
|
||||
logger.warn("no dispatcher available for channel '" + channel.getName() + "', be sure to register the channel");
|
||||
}
|
||||
}
|
||||
if (concurrencyPolicy != null) {
|
||||
@@ -335,8 +306,8 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
|
||||
}
|
||||
this.starting = true;
|
||||
synchronized (this.lifecycleMonitor) {
|
||||
this.activateEndpoints();
|
||||
this.taskScheduler.start();
|
||||
this.running = true;
|
||||
for (MessageDispatcher dispatcher : this.dispatchers.values()) {
|
||||
dispatcher.start();
|
||||
if (logger.isInfoEnabled()) {
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package org.springframework.integration.bus;
|
||||
|
||||
import org.springframework.integration.endpoint.ConcurrencyPolicy;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.scheduling.Schedule;
|
||||
|
||||
/**
|
||||
@@ -26,29 +26,39 @@ import org.springframework.integration.scheduling.Schedule;
|
||||
*/
|
||||
public class Subscription {
|
||||
|
||||
private String channel;
|
||||
private MessageChannel channel;
|
||||
|
||||
private String handler;
|
||||
private String channelName;
|
||||
|
||||
private Schedule schedule;
|
||||
|
||||
private ConcurrencyPolicy concurrencyPolicy;
|
||||
|
||||
|
||||
public String getChannel() {
|
||||
return this.channel;
|
||||
public Subscription() {
|
||||
}
|
||||
|
||||
public void setChannel(String channel) {
|
||||
public Subscription(MessageChannel channel) {
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
public String getHandler() {
|
||||
return this.handler;
|
||||
public Subscription(String channelName) {
|
||||
this.channelName = channelName;
|
||||
}
|
||||
|
||||
public void setHandler(String handler) {
|
||||
this.handler = handler;
|
||||
|
||||
public MessageChannel getChannel() {
|
||||
return this.channel;
|
||||
}
|
||||
|
||||
public void setChannel(MessageChannel channel) {
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
public String getChannelName() {
|
||||
return (this.channel != null) ? this.channel.getName() : this.channelName;
|
||||
}
|
||||
|
||||
public void setChannelName(String channelName) {
|
||||
this.channelName = channelName;
|
||||
}
|
||||
|
||||
public Schedule getSchedule() {
|
||||
@@ -59,12 +69,4 @@ public class Subscription {
|
||||
this.schedule = schedule;
|
||||
}
|
||||
|
||||
public ConcurrencyPolicy getConcurrencyPolicy() {
|
||||
return this.concurrencyPolicy;
|
||||
}
|
||||
|
||||
public void setConcurrencyPolicy(ConcurrencyPolicy concurrencyPolicy) {
|
||||
this.concurrencyPolicy = concurrencyPolicy;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,9 +19,6 @@ package org.springframework.integration.channel;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -31,8 +28,6 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class DefaultChannelRegistry implements ChannelRegistry {
|
||||
|
||||
private Log logger = LogFactory.getLog(this.getClass());
|
||||
|
||||
private Map<String, MessageChannel> channels = new ConcurrentHashMap<String, MessageChannel>();
|
||||
|
||||
private MessageChannel invalidMessageChannel;
|
||||
@@ -55,9 +50,6 @@ public class DefaultChannelRegistry implements ChannelRegistry {
|
||||
Assert.notNull(channel, "'channel' must not be null");
|
||||
channel.setName(name);
|
||||
this.channels.put(name, channel);
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("registered channel '" + name + "'");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ import org.springframework.integration.adapter.DefaultTargetAdapter;
|
||||
import org.springframework.integration.adapter.MethodInvokingSource;
|
||||
import org.springframework.integration.adapter.MethodInvokingTarget;
|
||||
import org.springframework.integration.adapter.PollingSourceAdapter;
|
||||
import org.springframework.integration.bus.Subscription;
|
||||
import org.springframework.integration.endpoint.DefaultMessageEndpoint;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -79,6 +81,7 @@ public class ChannelAdapterParser implements BeanDefinitionParser {
|
||||
if (StringUtils.hasText(period)) {
|
||||
adapterDef.getPropertyValues().addPropertyValue("period", period);
|
||||
}
|
||||
adapterDef.getPropertyValues().addPropertyValue("channel", new RuntimeBeanReference(channel));
|
||||
}
|
||||
else {
|
||||
adapterDef = new RootBeanDefinition(DefaultTargetAdapter.class);
|
||||
@@ -89,12 +92,22 @@ public class ChannelAdapterParser implements BeanDefinitionParser {
|
||||
String invokerBeanName = parserContext.getReaderContext().generateBeanName(invokerDef);
|
||||
parserContext.registerBeanComponent(new BeanComponentDefinition(invokerDef, invokerBeanName));
|
||||
adapterDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference(invokerBeanName));
|
||||
adapterDef.getPropertyValues().addPropertyValue("channel", new RuntimeBeanReference(channel));
|
||||
adapterDef.setSource(parserContext.extractSource(element));
|
||||
String beanName = element.getAttribute(ID_ATTRIBUTE);
|
||||
if (!StringUtils.hasText(beanName)) {
|
||||
beanName = parserContext.getReaderContext().generateBeanName(adapterDef);
|
||||
}
|
||||
if (!this.isInbound) {
|
||||
RootBeanDefinition endpointDef = new RootBeanDefinition(DefaultMessageEndpoint.class);
|
||||
RootBeanDefinition subscriptionDef = new RootBeanDefinition(Subscription.class);
|
||||
subscriptionDef.getPropertyValues().addPropertyValue("channel", new RuntimeBeanReference(channel));
|
||||
String subscriptionBeanName = parserContext.getReaderContext().generateBeanName(subscriptionDef);
|
||||
parserContext.registerBeanComponent(new BeanComponentDefinition(subscriptionDef, subscriptionBeanName));
|
||||
endpointDef.getPropertyValues().addPropertyValue("subscription", new RuntimeBeanReference(subscriptionBeanName));
|
||||
endpointDef.getPropertyValues().addPropertyValue("handler", new RuntimeBeanReference(beanName));
|
||||
String endpointBeanName = parserContext.getReaderContext().generateBeanName(endpointDef);
|
||||
parserContext.registerBeanComponent(new BeanComponentDefinition(endpointDef, endpointBeanName));
|
||||
}
|
||||
parserContext.registerBeanComponent(new BeanComponentDefinition(adapterDef, beanName));
|
||||
return adapterDef;
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.xml.BeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.MessagingConfigurationException;
|
||||
import org.springframework.integration.bus.Subscription;
|
||||
import org.springframework.integration.endpoint.ConcurrencyPolicy;
|
||||
import org.springframework.integration.endpoint.DefaultMessageEndpoint;
|
||||
import org.springframework.integration.handler.DefaultMessageHandlerAdapter;
|
||||
@@ -49,7 +50,9 @@ public class EndpointParser implements BeanDefinitionParser {
|
||||
|
||||
private static final String INPUT_CHANNEL_ATTRIBUTE = "input-channel";
|
||||
|
||||
private static final String INPUT_CHANNEL_PROPERTY = "inputChannelName";
|
||||
private static final String SUBSCRIPTION_PROPERTY = "subscription";
|
||||
|
||||
private static final String CHANNEL_PROPERTY = "channel";
|
||||
|
||||
private static final String DEFAULT_OUTPUT_CHANNEL_ATTRIBUTE = "default-output-channel";
|
||||
|
||||
@@ -90,8 +93,9 @@ public class EndpointParser implements BeanDefinitionParser {
|
||||
RootBeanDefinition endpointDef = new RootBeanDefinition(DefaultMessageEndpoint.class);
|
||||
endpointDef.setSource(parserContext.extractSource(element));
|
||||
String inputChannel = element.getAttribute(INPUT_CHANNEL_ATTRIBUTE);
|
||||
RootBeanDefinition subscriptionDef = new RootBeanDefinition(Subscription.class);
|
||||
if (StringUtils.hasText(inputChannel)) {
|
||||
endpointDef.getPropertyValues().addPropertyValue(INPUT_CHANNEL_PROPERTY, inputChannel);
|
||||
subscriptionDef.getPropertyValues().addPropertyValue(CHANNEL_PROPERTY, new RuntimeBeanReference(inputChannel));
|
||||
}
|
||||
String defaultOutputChannel = element.getAttribute(DEFAULT_OUTPUT_CHANNEL_ATTRIBUTE);
|
||||
if (StringUtils.hasText(defaultOutputChannel)) {
|
||||
@@ -104,7 +108,7 @@ public class EndpointParser implements BeanDefinitionParser {
|
||||
if (child.getNodeType() == Node.ELEMENT_NODE) {
|
||||
String localName = child.getLocalName();
|
||||
if (CONCURRENCY_ELEMENT.equals(localName)) {
|
||||
parseConcurrencyPolicy((Element) child, endpointDef);
|
||||
parseConcurrencyPolicy((Element) child, subscriptionDef);
|
||||
}
|
||||
else if (HANDLER_ELEMENT.equals(localName)) {
|
||||
String ref = ((Element) child).getAttribute(REF_ATTRIBUTE);
|
||||
@@ -113,6 +117,9 @@ public class EndpointParser implements BeanDefinitionParser {
|
||||
}
|
||||
}
|
||||
}
|
||||
String subscriptionBeanName = parserContext.getReaderContext().generateBeanName(subscriptionDef);
|
||||
parserContext.registerBeanComponent(new BeanComponentDefinition(subscriptionDef, subscriptionBeanName));
|
||||
endpointDef.getPropertyValues().addPropertyValue(SUBSCRIPTION_PROPERTY, new RuntimeBeanReference(subscriptionBeanName));
|
||||
if (childHandlerRefs.size() > 0) {
|
||||
if (childHandlerRefs.size() == 1) {
|
||||
endpointDef.getPropertyValues().addPropertyValue(
|
||||
@@ -153,7 +160,7 @@ public class EndpointParser implements BeanDefinitionParser {
|
||||
return endpointDef;
|
||||
}
|
||||
|
||||
private void parseConcurrencyPolicy(Element concurrencyElement, RootBeanDefinition endpointDefinition) {
|
||||
private void parseConcurrencyPolicy(Element concurrencyElement, RootBeanDefinition subscriptionDefinition) {
|
||||
ConcurrencyPolicy policy = new ConcurrencyPolicy();
|
||||
String coreConcurrency = concurrencyElement.getAttribute(CORE_CONCURRENCY_ATTRIBUTE);
|
||||
String maxConcurrency = concurrencyElement.getAttribute(MAX_CONCURRENCY_ATTRIBUTE);
|
||||
@@ -163,7 +170,7 @@ public class EndpointParser implements BeanDefinitionParser {
|
||||
if (StringUtils.hasText(maxConcurrency)) {
|
||||
policy.setMaxConcurrency(Integer.parseInt(maxConcurrency));
|
||||
}
|
||||
endpointDefinition.getPropertyValues().addPropertyValue(CONCURRENCY_POLICY_PROPERTY, policy);
|
||||
subscriptionDefinition.getPropertyValues().addPropertyValue(CONCURRENCY_POLICY_PROPERTY, policy);
|
||||
}
|
||||
|
||||
private void parseSchedule(Element scheduleElement, RootBeanDefinition endpointDefinition) {
|
||||
|
||||
@@ -44,10 +44,10 @@ import org.springframework.integration.annotation.Polled;
|
||||
import org.springframework.integration.annotation.Router;
|
||||
import org.springframework.integration.annotation.Splitter;
|
||||
import org.springframework.integration.bus.MessageBus;
|
||||
import org.springframework.integration.bus.Subscription;
|
||||
import org.springframework.integration.channel.ChannelRegistryAware;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.channel.SimpleChannel;
|
||||
import org.springframework.integration.endpoint.ConcurrencyPolicy;
|
||||
import org.springframework.integration.endpoint.DefaultMessageEndpoint;
|
||||
import org.springframework.integration.handler.MessageHandler;
|
||||
import org.springframework.integration.handler.MessageHandlerChain;
|
||||
@@ -106,24 +106,23 @@ public class MessageEndpointAnnotationPostProcessor implements BeanPostProcessor
|
||||
return bean;
|
||||
}
|
||||
DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint();
|
||||
this.configureInputChannel(bean, beanName, endpointAnnotation, endpoint);
|
||||
this.configureDefaultOutputChannel(bean, beanName, endpointAnnotation, endpoint);
|
||||
this.configureInput(bean, beanName, endpointAnnotation, endpoint);
|
||||
MessageHandlerChain handlerChain = this.createHandlerChain(bean);
|
||||
if (handlerChain != null) {
|
||||
endpoint.setHandler(handlerChain);
|
||||
}
|
||||
this.messageBus.registerEndpoint(beanName, endpoint);
|
||||
return endpoint;
|
||||
endpoint.setHandler(handlerChain);
|
||||
this.configureDefaultOutput(bean, beanName, endpointAnnotation, endpoint);
|
||||
this.messageBus.registerEndpoint(bean + "-endpoint", endpoint);
|
||||
return bean;
|
||||
}
|
||||
|
||||
private void configureInputChannel(final Object bean, final String beanName,
|
||||
MessageEndpoint annotation, final DefaultMessageEndpoint endpoint) {
|
||||
private void configureInput(final Object bean, final String beanName, MessageEndpoint annotation,
|
||||
final DefaultMessageEndpoint endpoint) {
|
||||
String channelName = annotation.input();
|
||||
if (StringUtils.hasText(channelName)) {
|
||||
endpoint.setInputChannelName(channelName);
|
||||
Subscription subscription = new Subscription();
|
||||
subscription.setChannelName(channelName);
|
||||
Schedule schedule = new PollingSchedule(annotation.pollPeriod());
|
||||
endpoint.setSchedule(schedule);
|
||||
return;
|
||||
subscription.setSchedule(schedule);
|
||||
endpoint.setSubscription(subscription);
|
||||
}
|
||||
ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() {
|
||||
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
|
||||
@@ -140,22 +139,16 @@ public class MessageEndpointAnnotationPostProcessor implements BeanPostProcessor
|
||||
String channelName = beanName + "-inputChannel";
|
||||
messageBus.registerChannel(channelName, channel);
|
||||
messageBus.registerSourceAdapter(beanName + "-sourceAdapter", adapter);
|
||||
endpoint.setInputChannelName(channelName);
|
||||
Subscription subscription = new Subscription(channel);
|
||||
Schedule schedule = new PollingSchedule(period);
|
||||
endpoint.setSchedule(schedule);
|
||||
if (period > 0) {
|
||||
ConcurrencyPolicy concurrencyPolicy = new ConcurrencyPolicy();
|
||||
concurrencyPolicy.setCoreConcurrency(1);
|
||||
concurrencyPolicy.setMaxConcurrency(1);
|
||||
endpoint.setConcurrencyPolicy(concurrencyPolicy);
|
||||
}
|
||||
return;
|
||||
subscription.setSchedule(schedule);
|
||||
endpoint.setSubscription(subscription);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void configureDefaultOutputChannel(final Object bean, final String beanName,
|
||||
private void configureDefaultOutput(final Object bean, final String beanName,
|
||||
final MessageEndpoint annotation, final DefaultMessageEndpoint endpoint) {
|
||||
String channelName = annotation.defaultOutput();
|
||||
if (StringUtils.hasText(channelName)) {
|
||||
@@ -177,8 +170,9 @@ public class MessageEndpointAnnotationPostProcessor implements BeanPostProcessor
|
||||
DefaultTargetAdapter adapter = new DefaultTargetAdapter(target);
|
||||
SimpleChannel channel = new SimpleChannel();
|
||||
String channelName = beanName + "-defaultOutputChannel";
|
||||
Subscription subscription = new Subscription(channel);
|
||||
messageBus.registerChannel(channelName, channel);
|
||||
messageBus.registerTargetAdapter(beanName + "-targetAdapter", adapter);
|
||||
messageBus.registerHandler(beanName + "-targetAdapter", adapter, subscription);
|
||||
endpoint.setDefaultOutputChannelName(channelName);
|
||||
foundDefaultOutput = true;
|
||||
return;
|
||||
|
||||
@@ -28,7 +28,7 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.integration.annotation.Subscriber;
|
||||
import org.springframework.integration.bus.MessageBus;
|
||||
import org.springframework.integration.endpoint.DefaultMessageEndpoint;
|
||||
import org.springframework.integration.bus.Subscription;
|
||||
import org.springframework.integration.handler.DefaultMessageHandlerAdapter;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -85,13 +85,10 @@ public class SubscriberAnnotationPostProcessor implements BeanPostProcessor {
|
||||
adapter.setMethodName(method.getName());
|
||||
adapter.setObject(bean);
|
||||
adapter.afterPropertiesSet();
|
||||
DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint();
|
||||
endpoint.setInputChannelName(channelName);
|
||||
endpoint.setChannelRegistry(messageBus);
|
||||
endpoint.setHandler(adapter);
|
||||
String endpointName = ClassUtils.getShortNameAsProperty(targetClass) +
|
||||
String adapterName = ClassUtils.getShortNameAsProperty(targetClass) +
|
||||
"-" + method.getName() + "-endpoint";
|
||||
messageBus.registerEndpoint(endpointName, endpoint);
|
||||
Subscription subscription = new Subscription(channelName);
|
||||
messageBus.registerHandler(adapterName, adapter, subscription);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -19,31 +19,30 @@ package org.springframework.integration.endpoint;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.integration.MessageHandlingException;
|
||||
import org.springframework.integration.MessagingConfigurationException;
|
||||
import org.springframework.integration.bus.Subscription;
|
||||
import org.springframework.integration.channel.ChannelRegistry;
|
||||
import org.springframework.integration.channel.ChannelRegistryAware;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.handler.MessageHandler;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.scheduling.Schedule;
|
||||
|
||||
/**
|
||||
* Default implementation of the {@link MessageEndpoint} interface.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class DefaultMessageEndpoint implements MessageEndpoint, BeanNameAware {
|
||||
public class DefaultMessageEndpoint implements MessageEndpoint, ChannelRegistryAware, BeanNameAware {
|
||||
|
||||
private String name;
|
||||
|
||||
private String inputChannelName;
|
||||
|
||||
private String defaultOutputChannelName;
|
||||
|
||||
private MessageHandler handler;
|
||||
|
||||
private Schedule schedule;
|
||||
private Subscription subscription;
|
||||
|
||||
private ConcurrencyPolicy concurrencyPolicy;
|
||||
|
||||
private String defaultOutputChannelName;
|
||||
|
||||
private ChannelRegistry channelRegistry;
|
||||
|
||||
|
||||
@@ -59,18 +58,8 @@ public class DefaultMessageEndpoint implements MessageEndpoint, BeanNameAware {
|
||||
this.setName(beanName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name of the channel from which this endpoint receives messages.
|
||||
*/
|
||||
public void setInputChannelName(String inputChannelName) {
|
||||
this.inputChannelName = inputChannelName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of the channel from which this endpoint receives messages.
|
||||
*/
|
||||
public String getInputChannelName() {
|
||||
return this.inputChannelName;
|
||||
public String getDefaultOutputChannelName() {
|
||||
return this.defaultOutputChannelName;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,8 +69,8 @@ public class DefaultMessageEndpoint implements MessageEndpoint, BeanNameAware {
|
||||
this.defaultOutputChannelName = defaultOutputChannelName;
|
||||
}
|
||||
|
||||
public String getDefaultOutputChannelName() {
|
||||
return this.defaultOutputChannelName;
|
||||
public MessageHandler getHandler() {
|
||||
return this.handler;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,12 +80,12 @@ public class DefaultMessageEndpoint implements MessageEndpoint, BeanNameAware {
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
public Schedule getSchedule() {
|
||||
return this.schedule;
|
||||
public Subscription getSubscription() {
|
||||
return this.subscription;
|
||||
}
|
||||
|
||||
public void setSchedule(Schedule schedule) {
|
||||
this.schedule = schedule;
|
||||
public void setSubscription(Subscription subscription) {
|
||||
this.subscription = subscription;
|
||||
}
|
||||
|
||||
public ConcurrencyPolicy getConcurrencyPolicy() {
|
||||
@@ -114,7 +103,7 @@ public class DefaultMessageEndpoint implements MessageEndpoint, BeanNameAware {
|
||||
this.channelRegistry = channelRegistry;
|
||||
}
|
||||
|
||||
public Message handle(Message<?> message) {
|
||||
public Message<?> handle(Message<?> message) {
|
||||
if (this.handler == null) {
|
||||
if (this.defaultOutputChannelName == null) {
|
||||
throw new MessagingConfigurationException(
|
||||
|
||||
@@ -16,9 +16,8 @@
|
||||
|
||||
package org.springframework.integration.endpoint;
|
||||
|
||||
import org.springframework.integration.channel.ChannelRegistry;
|
||||
import org.springframework.integration.bus.Subscription;
|
||||
import org.springframework.integration.handler.MessageHandler;
|
||||
import org.springframework.integration.scheduling.Schedule;
|
||||
|
||||
/**
|
||||
* Base interface for message endpoints.
|
||||
@@ -27,19 +26,11 @@ import org.springframework.integration.scheduling.Schedule;
|
||||
*/
|
||||
public interface MessageEndpoint extends MessageHandler {
|
||||
|
||||
void setName(String name);
|
||||
String getName();
|
||||
|
||||
void setInputChannelName(String inputChannelName);
|
||||
MessageHandler getHandler();
|
||||
|
||||
String getInputChannelName();
|
||||
|
||||
void setDefaultOutputChannelName(String defaultOutputChannelName);
|
||||
|
||||
String getDefaultOutputChannelName();
|
||||
|
||||
void setChannelRegistry(ChannelRegistry channelRegistry);
|
||||
|
||||
Schedule getSchedule();
|
||||
Subscription getSubscription();
|
||||
|
||||
ConcurrencyPolicy getConcurrencyPolicy();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user