Major refactoring of message bus, scheduling, and dispatching.

This commit is contained in:
Mark Fisher
2008-01-14 19:41:48 +00:00
parent 9737b329dc
commit 2b29791fde
53 changed files with 1253 additions and 957 deletions

View File

@@ -21,7 +21,6 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.MessagingConfigurationException;
import org.springframework.integration.bus.ConsumerPolicy;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageMapper;
@@ -41,8 +40,6 @@ public abstract class AbstractSourceAdapter<T> implements SourceAdapter, Initial
private MessageMapper<?,T> mapper = new SimplePayloadMessageMapper<T>();
private ConsumerPolicy consumerPolicy;
private long sendTimeout = -1;
private volatile boolean initialized = false;
@@ -70,15 +67,6 @@ public abstract class AbstractSourceAdapter<T> implements SourceAdapter, Initial
return this.mapper;
}
public void setConsumerPolicy(ConsumerPolicy consumerPolicy) {
Assert.notNull(consumerPolicy, "'consumerPolicy' must not be null");
this.consumerPolicy = consumerPolicy;
}
public ConsumerPolicy getConsumerPolicy() {
return this.consumerPolicy;
}
public final void afterPropertiesSet() {
if (this.channel == null) {
throw new MessagingConfigurationException("'channel' is required");

View File

@@ -19,11 +19,12 @@ package org.springframework.integration.adapter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.integration.bus.ConsumerPolicy;
import org.springframework.integration.channel.MessageChannel;
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;
/**
@@ -41,7 +42,7 @@ public abstract class AbstractTargetAdapter<T> implements TargetAdapter {
private MessageMapper<?,T> mapper = new SimplePayloadMessageMapper<T>();
private ConsumerPolicy policy = ConsumerPolicy.newPollingPolicy(5);
private Schedule schedule = new PollingSchedule(5);
public void setName(String name) {
@@ -70,13 +71,13 @@ public abstract class AbstractTargetAdapter<T> implements TargetAdapter {
return this.mapper;
}
public void setConsumerPolicy(ConsumerPolicy policy) {
Assert.notNull(policy, "'policy' must not be null");
this.policy = policy;
public void setSchedule(Schedule schedule) {
Assert.notNull(schedule, "'schedule' must not be null");
this.schedule = schedule;
}
public ConsumerPolicy getConsumerPolicy() {
return this.policy;
public Schedule getSchedule() {
return this.schedule;
}
public final Message handle(Message message) {

View File

@@ -18,11 +18,16 @@ package org.springframework.integration.adapter;
import java.util.Collection;
import org.springframework.context.Lifecycle;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.bus.ConsumerPolicy;
import org.springframework.integration.bus.MessageDispatcher;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.MessageMapper;
import org.springframework.integration.scheduling.MessagingTask;
import org.springframework.integration.scheduling.MessagingTaskScheduler;
import org.springframework.integration.scheduling.MessagingTaskSchedulerAware;
import org.springframework.integration.scheduling.PollingSchedule;
import org.springframework.integration.scheduling.Schedule;
import org.springframework.integration.scheduling.SimpleMessagingTaskScheduler;
import org.springframework.util.Assert;
/**
@@ -32,19 +37,44 @@ import org.springframework.util.Assert;
*
* @author Mark Fisher
*/
public class PollingSourceAdapter<T> extends AbstractSourceAdapter<T> implements MessageDispatcher {
private static int DEFAULT_PERIOD = 1000;
public class PollingSourceAdapter<T> extends AbstractSourceAdapter<T> implements MessagingTaskSchedulerAware, Lifecycle {
private PollableSource<T> source;
private PollingSchedule schedule = new PollingSchedule(1000);
private MessagingTaskScheduler scheduler;
private int maxMessagesPerTask = 1;
private volatile boolean starting;
private volatile boolean running;
public PollingSourceAdapter(PollableSource<T> source) {
Assert.notNull(source, "'source' must not be null");
this.source = source;
this.setConsumerPolicy(ConsumerPolicy.newPollingPolicy(DEFAULT_PERIOD));
}
public void setInitialDelay(long intialDelay) {
Assert.isTrue(intialDelay >= 0, "'intialDelay' must not be negative");
this.schedule.setInitialDelay(intialDelay);
}
public void setPeriod(long period) {
this.schedule.setPeriod(period);
}
public void setMaxMessagesPerTask(int maxMessagesPerTask) {
Assert.isTrue(maxMessagesPerTask > 0, "'maxMessagesPerTask' must be at least one");
this.maxMessagesPerTask = maxMessagesPerTask;
}
public void setMessagingTaskScheduler(MessagingTaskScheduler scheduler) {
Assert.notNull(scheduler, "scheduler must not be null");
this.scheduler = scheduler;
}
protected PollableSource<T> getSource() {
@@ -56,32 +86,42 @@ public class PollingSourceAdapter<T> extends AbstractSourceAdapter<T> implements
}
public void start() {
if (this.isRunning() || this.starting) {
return;
}
this.starting = true;
if (!this.isInitialized()) {
this.afterPropertiesSet();
}
if (this.scheduler == null) {
if (logger.isInfoEnabled()) {
logger.info("no task scheduler has been provided, will create one");
}
SimpleMessagingTaskScheduler taskScheduler = new SimpleMessagingTaskScheduler();
taskScheduler.setCorePoolSize(1);
this.scheduler = taskScheduler;
}
if (!this.scheduler.isRunning()) {
this.scheduler.start();
}
this.scheduler.schedule(new PollingSourceAdapterTask());
this.running = true;
this.starting = false;
}
public void stop() {
this.running = false;
}
public void setPeriod(int period) {
Assert.isTrue(period > 0, "'period' must be a positive value");
this.getConsumerPolicy().setPeriod(period);
}
public void setMaxMessagesPerTask(int maxMessagesPerTask) {
Assert.isTrue(maxMessagesPerTask > 0, "'maxMessagesPerTask' must be a positive value");
this.getConsumerPolicy().setMaxMessagesPerTask(maxMessagesPerTask);
}
public int dispatch() {
public int processMessages() {
if (!this.isRunning()) {
if (logger.isDebugEnabled()) {
logger.debug("source adapter not polling since it has not yet been started");
}
return 0;
}
int messagesProcessed = 0;
int limit = this.getConsumerPolicy().getMaxMessagesPerTask();
int limit = this.maxMessagesPerTask;
Collection<T> results = this.source.poll(limit);
if (results != null) {
if (results.size() > limit) {
@@ -96,4 +136,16 @@ public class PollingSourceAdapter<T> extends AbstractSourceAdapter<T> implements
return messagesProcessed;
}
private class PollingSourceAdapterTask implements MessagingTask {
public void run() {
processMessages();
}
public Schedule getSchedule() {
return schedule;
}
}
}

View File

@@ -16,7 +16,6 @@
package org.springframework.integration.adapter;
import org.springframework.integration.bus.ConsumerPolicy;
import org.springframework.integration.channel.MessageChannel;
/**
@@ -26,8 +25,6 @@ import org.springframework.integration.channel.MessageChannel;
*/
public interface SourceAdapter {
ConsumerPolicy getConsumerPolicy();
void setChannel(MessageChannel channel);
}

View File

@@ -16,7 +16,6 @@
package org.springframework.integration.adapter;
import org.springframework.integration.bus.ConsumerPolicy;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.handler.MessageHandler;
@@ -31,6 +30,4 @@ public interface TargetAdapter extends MessageHandler {
void setChannel(MessageChannel channel);
ConsumerPolicy getConsumerPolicy();
}

View File

@@ -24,7 +24,6 @@ import org.springframework.context.Lifecycle;
import org.springframework.core.task.TaskExecutor;
import org.springframework.integration.MessagingConfigurationException;
import org.springframework.integration.adapter.AbstractSourceAdapter;
import org.springframework.integration.bus.ConsumerPolicy;
import org.springframework.jms.listener.AbstractJmsListeningContainer;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.support.converter.MessageConverter;
@@ -50,7 +49,15 @@ public class JmsMessageDrivenSourceAdapter extends AbstractSourceAdapter<Object>
private TaskExecutor taskExecutor;
private ConsumerPolicy policy = ConsumerPolicy.newEventDrivenPolicy();
private long receiveTimeout = 1000;
private int concurrentConsumers = 1;
private int maxConcurrentConsumers = 1;
private int maxMessagesPerTask = Integer.MIN_VALUE;
private int idleTaskExecutionLimit = 1;
private long sendTimeout = -1;
@@ -104,10 +111,11 @@ public class JmsMessageDrivenSourceAdapter extends AbstractSourceAdapter<Object>
if (this.destinationName != null) {
dmlc.setDestinationName(this.destinationName);
}
dmlc.setReceiveTimeout(this.policy.getReceiveTimeout());
dmlc.setConcurrentConsumers(this.policy.getConcurrency());
dmlc.setMaxConcurrentConsumers(this.policy.getMaxConcurrency());
dmlc.setMaxMessagesPerTask(this.policy.getMaxMessagesPerTask());
dmlc.setReceiveTimeout(this.receiveTimeout);
dmlc.setConcurrentConsumers(this.concurrentConsumers);
dmlc.setMaxConcurrentConsumers(this.maxConcurrentConsumers);
dmlc.setMaxMessagesPerTask(this.maxMessagesPerTask);
dmlc.setIdleTaskExecutionLimit(this.idleTaskExecutionLimit);
dmlc.setAutoStartup(false);
ChannelPublishingJmsListener listener = new ChannelPublishingJmsListener(this.getChannel());
listener.setMessageConverter(this.messageConverter);

View File

@@ -1,121 +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.bus;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.Lifecycle;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.util.Assert;
/**
* Abstract base class for message dispatchers. Delegates to a
* {@link MessageRetriever} strategy.
*
* @author Mark Fisher
*/
public abstract class AbstractMessageDispatcher implements MessageDispatcher {
protected Log logger = LogFactory.getLog(this.getClass());
private MessageRetriever retriever;
private List<MessageHandler> handlers = new CopyOnWriteArrayList<MessageHandler>();
private volatile boolean running;
private Object lifecycleMonitor = new Object();
public AbstractMessageDispatcher(MessageRetriever retriever) {
this.retriever = retriever;
}
public void addHandler(MessageHandler handler) {
Assert.notNull(handler, "'handler' must not be null");
if (this.isRunning() && handler instanceof Lifecycle) {
((Lifecycle) handler).start();
}
this.handlers.add(handler);
}
protected List<MessageHandler> getHandlers() {
return this.handlers;
}
public boolean isRunning() {
return this.running;
}
public void start() {
synchronized (this.lifecycleMonitor) {
if (!this.isRunning()) {
for (MessageHandler handler : this.handlers) {
if (handler instanceof Lifecycle) {
((Lifecycle) handler).start();
}
}
this.running = true;
}
}
}
public void stop() {
synchronized (this.lifecycleMonitor) {
if (this.isRunning()) {
for (MessageHandler handler : this.handlers) {
if (handler instanceof Lifecycle) {
((Lifecycle) handler).stop();
}
}
this.running = false;
}
}
}
/**
* Retrieves messages and dispatches to the executors.
*
* @return the number of messages processed
*/
public int dispatch() {
if (!this.isRunning()) {
return 0;
}
int messagesProcessed = 0;
Collection<Message<?>> messages = this.retriever.retrieveMessages();
if (messages == null) {
return 0;
}
for (Message<?> message : messages) {
if (dispatchMessage(message)) {
messagesProcessed++;
}
}
return messagesProcessed;
}
protected abstract boolean dispatchMessage(Message<?> message);
}

View File

@@ -1,181 +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.bus;
import java.util.concurrent.TimeUnit;
import org.springframework.util.Assert;
/**
* A container for Message consumer configuration metadata.
*
* @author Mark Fisher
*/
public class ConsumerPolicy {
private static final int DEFAULT_CONCURRENCY = 1;
private static final int DEFAULT_MAX_CONCURRENCY = 10;
private static final int DEFAULT_MAX_MESSAGES_PER_TASK = 1;
private static final int DEFAULT_REJECTION_LIMIT = 5;
private static final int DEFAULT_RETRY_INTERVAL = 1000;
private static final long DEFAULT_RECEIVE_TIMEOUT = 1000;
private int concurrency = DEFAULT_CONCURRENCY;
private int maxConcurrency = DEFAULT_MAX_CONCURRENCY;
private int maxMessagesPerTask = DEFAULT_MAX_MESSAGES_PER_TASK;
private int rejectionLimit = DEFAULT_REJECTION_LIMIT;
private int retryInterval = DEFAULT_RETRY_INTERVAL;
private int initialDelay = 0;
private int period = 5;
private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
private boolean fixedRate = false;
private long receiveTimeout = DEFAULT_RECEIVE_TIMEOUT;
/**
* Factory method for a basic polling policy.
*
* @param period the polling interval
*/
public static ConsumerPolicy newPollingPolicy(int period) {
Assert.isTrue(period > 0, "'period' must be a positive value");
ConsumerPolicy policy = new ConsumerPolicy();
policy.setPeriod(period);
policy.setConcurrency(1);
policy.setMaxConcurrency(1);
return policy;
}
/**
* Factory method for a basic event-driven policy.
*/
public static ConsumerPolicy newEventDrivenPolicy() {
ConsumerPolicy policy = new ConsumerPolicy();
policy.setPeriod(-1);
policy.setConcurrency(1);
policy.setMaxConcurrency(10);
return policy;
}
public int getInitialDelay() {
return this.initialDelay;
}
public void setInitialDelay(int initialDelay) {
this.initialDelay = initialDelay;
}
public int getPeriod() {
return this.period;
}
public void setPeriod(int period) {
this.period = period;
}
public TimeUnit getTimeUnit() {
return this.timeUnit;
}
public void setTimeUnit(TimeUnit timeUnit) {
this.timeUnit = timeUnit;
}
public boolean isFixedRate() {
return this.fixedRate;
}
public void setFixedRate(boolean fixedRate) {
this.fixedRate = fixedRate;
}
public int getConcurrency() {
return this.concurrency;
}
public void setConcurrency(int concurrency) {
if (concurrency < 1) {
throw new IllegalArgumentException("'concurrency' value must be at least 1");
}
this.concurrency = concurrency;
}
public int getMaxConcurrency() {
return this.maxConcurrency;
}
public void setMaxConcurrency(int maxConcurrency) {
if (maxConcurrency < 1) {
throw new IllegalArgumentException("'maxConcurrency' value must be at least 1");
}
this.maxConcurrency = maxConcurrency;
}
public int getMaxMessagesPerTask() {
return this.maxMessagesPerTask;
}
public void setMaxMessagesPerTask(int maxMessagesPerTask) {
if (maxMessagesPerTask == 0) {
throw new IllegalArgumentException("'maxMessagesPerTask' must not be 0");
}
this.maxMessagesPerTask = maxMessagesPerTask;
}
public int getRejectionLimit() {
return this.rejectionLimit;
}
public void setRejectionLimit(int rejectionLimit) {
if (rejectionLimit < 1) {
throw new IllegalArgumentException("'rejectionLimit' must be at least 1");
}
this.rejectionLimit = rejectionLimit;
}
public int getRetryInterval() {
return this.retryInterval;
}
public void setRetryInterval(int retryInterval) {
this.retryInterval = retryInterval;
}
public long getReceiveTimeout() {
return this.receiveTimeout;
}
public void setReceiveTimeout(long receiveTimeout) {
this.receiveTimeout = receiveTimeout;
}
}

View File

@@ -20,7 +20,6 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -37,9 +36,17 @@ import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.DefaultChannelRegistry;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.SimpleChannel;
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.MessageEndpoint;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.ErrorMessage;
import org.springframework.integration.scheduling.MessagePublishingErrorHandler;
import org.springframework.integration.scheduling.MessagingTaskScheduler;
import org.springframework.integration.scheduling.MessagingTaskSchedulerAware;
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;
@@ -58,23 +65,27 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
private Map<String, MessageHandler> handlers = new ConcurrentHashMap<String, MessageHandler>();
private List<MessageDispatcher> dispatchers = new CopyOnWriteArrayList<MessageDispatcher>();
private Map<MessageChannel, MessageDispatcher> dispatchers = new ConcurrentHashMap<MessageChannel, MessageDispatcher>();
private List<DispatcherTask> dispatcherTasks = new CopyOnWriteArrayList<DispatcherTask>();
private List<Lifecycle> lifecycleSourceAdapters = new CopyOnWriteArrayList<Lifecycle>();
private ScheduledThreadPoolExecutor dispatcherExecutor;
private MessagingTaskScheduler taskScheduler;
private int dispatcherPoolSize = 10;
private boolean autoCreateChannels;
private boolean running;
private volatile boolean initialized;
private volatile boolean starting;
private volatile boolean running;
private Object lifecycleMonitor = new Object();
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Assert.notNull(applicationContext, "applicationContext must not be null");
Assert.notNull(applicationContext, "'applicationContext' must not be null");
this.registerChannels(applicationContext);
this.registerEndpoints(applicationContext);
this.registerSourceAdapters(applicationContext);
@@ -88,8 +99,8 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
public void setDispatcherPoolSize(int dispatcherPoolSize) {
Assert.isTrue(dispatcherPoolSize > 0, "'dispatcherPoolSize' must be at least 1");
this.dispatcherPoolSize = dispatcherPoolSize;
if (this.dispatcherExecutor != null) {
this.dispatcherExecutor.setCorePoolSize(dispatcherPoolSize);
if (this.taskScheduler != null && this.taskScheduler instanceof SimpleMessagingTaskScheduler) {
((SimpleMessagingTaskScheduler) this.taskScheduler).setCorePoolSize(dispatcherPoolSize);
}
}
@@ -145,23 +156,29 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
this.activateSubscription(subscription);
if (logger.isInfoEnabled()) {
logger.info("activated subscription to channel '" + subscription.getChannel() +
"' for receiver '" + subscription.getReceiver() + "'");
"' for handler '" + subscription.getHandler() + "'");
}
}
}
public void initialize() {
initDispatcherExecutor();
if (this.getInvalidMessageChannel() == null) {
this.setInvalidMessageChannel(new SimpleChannel(Integer.MAX_VALUE));
}
initScheduler();
this.initialized = true;
}
private void initDispatcherExecutor() {
private void initScheduler() {
CustomizableThreadFactory threadFactory = new CustomizableThreadFactory();
threadFactory.setThreadNamePrefix("dispatcher-executor-");
threadFactory.setThreadGroup(new ThreadGroup("dispatcher-executors"));
this.dispatcherExecutor = new ScheduledThreadPoolExecutor(this.dispatcherPoolSize, threadFactory);
SimpleMessagingTaskScheduler scheduler = new SimpleMessagingTaskScheduler();
scheduler.setCorePoolSize(this.dispatcherPoolSize);
scheduler.setThreadFactory(threadFactory);
scheduler.setErrorHandler(new MessagePublishingErrorHandler(this.getInvalidMessageChannel()));
scheduler.afterPropertiesSet();
this.taskScheduler = scheduler;
}
public MessageChannel getInvalidMessageChannel() {
@@ -177,23 +194,42 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
}
public void registerChannel(String name, MessageChannel channel) {
this.registerChannel(name, channel, null);
}
public void registerChannel(String name, MessageChannel channel, DispatcherPolicy dispatcherPolicy) {
if (!this.initialized) {
this.initialize();
}
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel);
dispatcher.setMessagingTaskScheduler(this.taskScheduler);
if (dispatcherPolicy != null) {
dispatcher.setMaxMessagesPerTask(dispatcherPolicy.getMaxMessagesPerTask());
dispatcher.setReceiveTimeout(dispatcherPolicy.getReceiveTimeout());
dispatcher.setRejectionLimit(dispatcherPolicy.getRejectionLimit());
dispatcher.setRetryInterval(dispatcherPolicy.getRetryInterval());
}
this.dispatchers.put(channel, dispatcher);
this.channelRegistry.registerChannel(name, channel);
}
public void registerEndpoint(String name, MessageEndpoint endpoint) {
if (!this.initialized) {
this.initialize();
}
Assert.notNull(name, "'name' must not be null");
Assert.notNull(endpoint, "'endpoint' must not be null");
endpoint.setName(name);
this.handlers.put(name, endpoint);
if (logger.isInfoEnabled()) {
logger.info("registered endpoint '" + name + "'");
}
endpoint.setChannelRegistry(this);
if (endpoint.getInputChannelName() != null && endpoint.getConsumerPolicy() != null) {
Schedule schedule = endpoint.getSchedule();
if (endpoint.getInputChannelName() != null) {
Subscription subscription = new Subscription();
subscription.setHandler(name);
subscription.setChannel(endpoint.getInputChannelName());
subscription.setReceiver(name);
subscription.setPolicy(endpoint.getConsumerPolicy());
if (schedule != null) {
subscription.setSchedule(schedule);
}
this.activateSubscription(subscription);
}
if (this.autoCreateChannels) {
@@ -205,12 +241,17 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
}
public void registerSourceAdapter(String name, SourceAdapter adapter) {
ConsumerPolicy policy = adapter.getConsumerPolicy();
if (adapter instanceof MessageDispatcher) {
MessageDispatcher dispatcher = (MessageDispatcher) adapter;
this.dispatchers.add(dispatcher);
DispatcherTask dispatcherTask = new DispatcherTask(dispatcher, policy);
this.addDispatcherTask(dispatcherTask);
if (!this.initialized) {
this.initialize();
}
if (adapter instanceof MessagingTaskSchedulerAware) {
((MessagingTaskSchedulerAware) adapter).setMessagingTaskScheduler(this.taskScheduler);
}
if (adapter instanceof Lifecycle) {
this.lifecycleSourceAdapters.add((Lifecycle) adapter);
if (this.isRunning()) {
((Lifecycle) adapter).start();
}
}
if (logger.isInfoEnabled()) {
logger.info("registered source adapter '" + name + "'");
@@ -221,10 +262,13 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
if (targetAdapter instanceof AbstractTargetAdapter) {
AbstractTargetAdapter<?> adapter = (AbstractTargetAdapter<?>) targetAdapter;
adapter.setName(name);
this.handlers.put(name, targetAdapter);
this.handlers.put(name, adapter);
MessageChannel channel = adapter.getChannel();
ConsumerPolicy policy = adapter.getConsumerPolicy();
this.doActivate(channel, adapter, policy);
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 + "'");
@@ -233,8 +277,9 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
public void activateSubscription(Subscription subscription) {
String channelName = subscription.getChannel();
String handlerName = subscription.getReceiver();
ConsumerPolicy policy = subscription.getPolicy();
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 + "'");
@@ -251,58 +296,27 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
channel = new SimpleChannel();
this.registerChannel(channelName, channel);
}
this.doActivate(channel, handler, policy);
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, ConsumerPolicy policy) {
PooledMessageHandler pooledHandler = new PooledMessageHandler(handler, policy.getConcurrency(), policy.getMaxConcurrency());
ChannelPollingMessageDispatcher dispatcher = new ChannelPollingMessageDispatcher(channel, policy);
dispatcher.setRejectionLimit(policy.getRejectionLimit());
dispatcher.setRetryInterval(policy.getRetryInterval());
dispatcher.addHandler(pooledHandler);
DispatcherTask dispatcherTask = new DispatcherTask(dispatcher, policy);
private void doActivate(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");
}
}
if (concurrencyPolicy != null) {
handler = new PooledMessageHandler(handler, concurrencyPolicy.getCoreConcurrency(), concurrencyPolicy.getMaxConcurrency());
}
dispatcher.addHandler(handler, schedule);
if (this.isRunning()) {
dispatcher.start();
}
this.dispatchers.add(dispatcher);
this.addDispatcherTask(dispatcherTask);
if (this.logger.isInfoEnabled()) {
logger.info("registered dispatcher task: channel='" +
channel.getName() + "' handler='" + handler + "'");
}
}
private void addDispatcherTask(DispatcherTask dispatcherTask) {
this.dispatcherTasks.add(dispatcherTask);
if (this.isRunning()) {
scheduleDispatcherTask(dispatcherTask);
if (this.logger.isInfoEnabled()) {
logger.info("scheduled dispatcher task");
}
}
}
private void scheduleDispatcherTask(DispatcherTask task) {
ConsumerPolicy policy = task.getPolicy();
if (policy.getPeriod() <= 0) {
if (policy.getReceiveTimeout() <= 0) {
if (logger.isWarnEnabled()) {
logger.warn("Scheduling a repeating task with no receive timeout is not recommended! " +
"Consider providing a positive value for either 'period' or 'receiveTimeout'");
}
}
dispatcherExecutor.schedule(new RepeatingDispatcherTask(task), policy.getInitialDelay(), policy.getTimeUnit());
}
else if (policy.isFixedRate()) {
dispatcherExecutor.scheduleAtFixedRate(task, policy.getInitialDelay(), policy.getPeriod(), policy.getTimeUnit());
}
else {
dispatcherExecutor.scheduleWithFixedDelay(task, policy.getInitialDelay(), policy.getPeriod(), policy.getTimeUnit());
}
}
public boolean isRunning() {
@@ -312,22 +326,33 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
}
public void start() {
if (this.dispatcherExecutor == null) {
if (!this.initialized) {
this.initialize();
}
if (this.isRunning() || this.starting) {
return;
}
this.starting = true;
synchronized (this.lifecycleMonitor) {
if (!this.isRunning()) {
this.running = true;
for (MessageDispatcher dispatcher : this.dispatchers) {
dispatcher.start();
if (logger.isInfoEnabled()) {
logger.info("started dispatcher '" + dispatcher + "'");
}
}
for (DispatcherTask task : this.dispatcherTasks) {
scheduleDispatcherTask(task);
this.taskScheduler.start();
this.running = true;
for (MessageDispatcher dispatcher : this.dispatchers.values()) {
dispatcher.start();
if (logger.isInfoEnabled()) {
logger.info("started dispatcher '" + dispatcher + "'");
}
}
for (Lifecycle adapter : this.lifecycleSourceAdapters) {
adapter.start();
if (logger.isInfoEnabled()) {
logger.info("started source adapter '" + adapter + "'");
}
}
}
this.running = true;
this.starting = false;
if (logger.isInfoEnabled()) {
logger.info("message bus started");
}
}
@@ -335,8 +360,14 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
synchronized (this.lifecycleMonitor) {
if (this.isRunning()) {
this.running = false;
this.dispatcherExecutor.shutdownNow();
for (MessageDispatcher dispatcher : this.dispatchers) {
this.taskScheduler.stop();
for (Lifecycle adapter : this.lifecycleSourceAdapters) {
adapter.stop();
if (logger.isInfoEnabled()) {
logger.info("stopped source adapter '" + adapter + "'");
}
}
for (MessageDispatcher dispatcher : this.dispatchers.values()) {
dispatcher.stop();
if (logger.isInfoEnabled()) {
logger.info("stopped dispatcher '" + dispatcher + "'");
@@ -346,59 +377,4 @@ public class MessageBus implements ChannelRegistry, ApplicationContextAware, Lif
}
}
private void handleDispatchError(DispatcherTask task, Throwable t) {
try {
this.getInvalidMessageChannel().send(new ErrorMessage(t), 1000);
}
catch (Throwable ignore) { // message will be logged only
}
if (logger.isWarnEnabled()) {
logger.warn("failure occurred while dispatching message", t);
}
}
private class DispatcherTask implements Runnable {
private MessageDispatcher dispatcher;
private ConsumerPolicy policy;
public DispatcherTask(MessageDispatcher dispatcher, ConsumerPolicy policy) {
this.dispatcher = dispatcher;
this.policy = policy;
}
public ConsumerPolicy getPolicy() {
return this.policy;
}
public void run() {
try {
dispatcher.dispatch();
}
catch (Throwable t) {
handleDispatchError(this, t);
}
}
}
private class RepeatingDispatcherTask implements Runnable {
private DispatcherTask task;
RepeatingDispatcherTask(DispatcherTask task) {
this.task = task;
}
public void run() {
task.run();
dispatcherExecutor.execute(new RepeatingDispatcherTask(task));
}
}
}

View File

@@ -27,7 +27,9 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.Lifecycle;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.dispatcher.MessageHandlerNotRunningException;
import org.springframework.integration.dispatcher.MessageHandlerRejectedExecutionException;
import org.springframework.integration.dispatcher.MessageSelectorRejectedException;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.selector.MessageSelector;
@@ -112,7 +114,7 @@ public class PooledMessageHandler implements MessageHandler, Lifecycle {
}
}
public Message handle(Message<?> message) {
public Message<?> handle(Message<?> message) {
if (!this.isRunning()) {
throw new MessageHandlerNotRunningException();
}

View File

@@ -16,6 +16,9 @@
package org.springframework.integration.bus;
import org.springframework.integration.endpoint.ConcurrencyPolicy;
import org.springframework.integration.scheduling.Schedule;
/**
* Configuration metadata for activating a subscription.
*
@@ -25,9 +28,11 @@ public class Subscription {
private String channel;
private String receiver;
private String handler;
private ConsumerPolicy policy = new ConsumerPolicy();
private Schedule schedule;
private ConcurrencyPolicy concurrencyPolicy;
public String getChannel() {
@@ -38,20 +43,28 @@ public class Subscription {
this.channel = channel;
}
public String getReceiver() {
return this.receiver;
public String getHandler() {
return this.handler;
}
public void setReceiver(String receiver) {
this.receiver = receiver;
public void setHandler(String handler) {
this.handler = handler;
}
public ConsumerPolicy getPolicy() {
return this.policy;
public Schedule getSchedule() {
return this.schedule;
}
public void setPolicy(ConsumerPolicy policy) {
this.policy = policy;
public void setSchedule(Schedule schedule) {
this.schedule = schedule;
}
public ConcurrencyPolicy getConcurrencyPolicy() {
return this.concurrencyPolicy;
}
public void setConcurrencyPolicy(ConcurrencyPolicy concurrencyPolicy) {
this.concurrencyPolicy = concurrencyPolicy;
}
}

View File

@@ -31,10 +31,11 @@ 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.ConsumerPolicy;
import org.springframework.integration.endpoint.GenericMessageEndpoint;
import org.springframework.integration.endpoint.ConcurrencyPolicy;
import org.springframework.integration.endpoint.DefaultMessageEndpoint;
import org.springframework.integration.handler.DefaultMessageHandlerAdapter;
import org.springframework.integration.handler.MessageHandlerChain;
import org.springframework.integration.scheduling.PollingSchedule;
import org.springframework.util.StringUtils;
/**
@@ -74,15 +75,19 @@ public class EndpointParser implements BeanDefinitionParser {
private static final String PERIOD_ATTRIBUTE = "period";
private static final String PERIOD_PROPERTY = "period";
private static final String SCHEDULE_PROPERTY = "schedule";
private static final String CONSUMER_ELEMENT = "consumer";
private static final String CONCURRENCY_ELEMENT = "concurrency";
private static final String CONSUMER_POLICY_PROPERTY = "consumerPolicy";
private static final String CORE_CONCURRENCY_ATTRIBUTE = "core";
private static final String MAX_CONCURRENCY_ATTRIBUTE = "max";
private static final String CONCURRENCY_POLICY_PROPERTY = "concurrencyPolicy";
public BeanDefinition parse(Element element, ParserContext parserContext) {
RootBeanDefinition endpointDef = new RootBeanDefinition(GenericMessageEndpoint.class);
RootBeanDefinition endpointDef = new RootBeanDefinition(DefaultMessageEndpoint.class);
endpointDef.setSource(parserContext.extractSource(element));
String inputChannel = element.getAttribute(INPUT_CHANNEL_ATTRIBUTE);
if (StringUtils.hasText(inputChannel)) {
@@ -98,10 +103,8 @@ public class EndpointParser implements BeanDefinitionParser {
Node child = childNodes.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
String localName = child.getLocalName();
if (CONSUMER_ELEMENT.equals(localName)) {
String consumerBeanName = parseConsumer((Element) child, parserContext);
endpointDef.getPropertyValues().addPropertyValue(
CONSUMER_POLICY_PROPERTY, new RuntimeBeanReference(consumerBeanName));
if (CONCURRENCY_ELEMENT.equals(localName)) {
parseConcurrencyPolicy((Element) child, endpointDef);
}
else if (HANDLER_ELEMENT.equals(localName)) {
String ref = ((Element) child).getAttribute(REF_ATTRIBUTE);
@@ -150,15 +153,26 @@ public class EndpointParser implements BeanDefinitionParser {
return endpointDef;
}
private String parseConsumer(Element element, ParserContext parserContext) {
RootBeanDefinition consumerDef = new RootBeanDefinition(ConsumerPolicy.class);
String period = element.getAttribute(PERIOD_ATTRIBUTE);
if (StringUtils.hasText(period)) {
consumerDef.getPropertyValues().addPropertyValue(PERIOD_PROPERTY, Integer.parseInt(period));
private void parseConcurrencyPolicy(Element concurrencyElement, RootBeanDefinition endpointDefinition) {
ConcurrencyPolicy policy = new ConcurrencyPolicy();
String coreConcurrency = concurrencyElement.getAttribute(CORE_CONCURRENCY_ATTRIBUTE);
String maxConcurrency = concurrencyElement.getAttribute(MAX_CONCURRENCY_ATTRIBUTE);
if (StringUtils.hasText(coreConcurrency)) {
policy.setCoreConcurrency(Integer.parseInt(coreConcurrency));
}
String beanName = parserContext.getReaderContext().generateBeanName(consumerDef);
parserContext.registerBeanComponent(new BeanComponentDefinition(consumerDef, beanName));
return beanName;
if (StringUtils.hasText(maxConcurrency)) {
policy.setMaxConcurrency(Integer.parseInt(maxConcurrency));
}
endpointDefinition.getPropertyValues().addPropertyValue(CONCURRENCY_POLICY_PROPERTY, policy);
}
private void parseSchedule(Element scheduleElement, RootBeanDefinition endpointDefinition) {
PollingSchedule schedule = new PollingSchedule(5);
String period = scheduleElement.getAttribute(PERIOD_ATTRIBUTE);
if (StringUtils.hasText(period)) {
schedule.setPeriod(Integer.parseInt(period));
}
endpointDefinition.getPropertyValues().addPropertyValue(SCHEDULE_PROPERTY, schedule);
}
private String parseHandlerAdapter(String handlerRef, String handlerMethod, ParserContext parserContext) {

View File

@@ -43,18 +43,20 @@ import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.Polled;
import org.springframework.integration.annotation.Router;
import org.springframework.integration.annotation.Splitter;
import org.springframework.integration.bus.ConsumerPolicy;
import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.channel.ChannelRegistryAware;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.SimpleChannel;
import org.springframework.integration.endpoint.GenericMessageEndpoint;
import org.springframework.integration.endpoint.ConcurrencyPolicy;
import org.springframework.integration.endpoint.DefaultMessageEndpoint;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.handler.MessageHandlerChain;
import org.springframework.integration.handler.config.DefaultMessageHandlerCreator;
import org.springframework.integration.handler.config.MessageHandlerCreator;
import org.springframework.integration.handler.config.RouterMessageHandlerCreator;
import org.springframework.integration.handler.config.SplitterMessageHandlerCreator;
import org.springframework.integration.scheduling.PollingSchedule;
import org.springframework.integration.scheduling.Schedule;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
@@ -103,7 +105,7 @@ public class MessageEndpointAnnotationPostProcessor implements BeanPostProcessor
if (endpointAnnotation == null) {
return bean;
}
GenericMessageEndpoint endpoint = new GenericMessageEndpoint();
DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint();
this.configureInputChannel(bean, beanName, endpointAnnotation, endpoint);
this.configureDefaultOutputChannel(bean, beanName, endpointAnnotation, endpoint);
MessageHandlerChain handlerChain = this.createHandlerChain(bean);
@@ -115,13 +117,12 @@ public class MessageEndpointAnnotationPostProcessor implements BeanPostProcessor
}
private void configureInputChannel(final Object bean, final String beanName,
MessageEndpoint annotation, final GenericMessageEndpoint endpoint) {
MessageEndpoint annotation, final DefaultMessageEndpoint endpoint) {
String channelName = annotation.input();
if (StringUtils.hasText(channelName)) {
endpoint.setInputChannelName(channelName);
ConsumerPolicy consumerPolicy = new ConsumerPolicy();
consumerPolicy.setPeriod(annotation.pollPeriod());
endpoint.setConsumerPolicy(consumerPolicy);
Schedule schedule = new PollingSchedule(annotation.pollPeriod());
endpoint.setSchedule(schedule);
return;
}
ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() {
@@ -140,11 +141,13 @@ public class MessageEndpointAnnotationPostProcessor implements BeanPostProcessor
messageBus.registerChannel(channelName, channel);
messageBus.registerSourceAdapter(beanName + "-sourceAdapter", adapter);
endpoint.setInputChannelName(channelName);
endpoint.getConsumerPolicy().setPeriod(period);
Schedule schedule = new PollingSchedule(period);
endpoint.setSchedule(schedule);
if (period > 0) {
endpoint.getConsumerPolicy().setConcurrency(1);
endpoint.getConsumerPolicy().setMaxConcurrency(1);
endpoint.getConsumerPolicy().setMaxMessagesPerTask(1);
ConcurrencyPolicy concurrencyPolicy = new ConcurrencyPolicy();
concurrencyPolicy.setCoreConcurrency(1);
concurrencyPolicy.setMaxConcurrency(1);
endpoint.setConcurrencyPolicy(concurrencyPolicy);
}
return;
}
@@ -153,7 +156,7 @@ public class MessageEndpointAnnotationPostProcessor implements BeanPostProcessor
}
private void configureDefaultOutputChannel(final Object bean, final String beanName,
final MessageEndpoint annotation, final GenericMessageEndpoint endpoint) {
final MessageEndpoint annotation, final DefaultMessageEndpoint endpoint) {
String channelName = annotation.defaultOutput();
if (StringUtils.hasText(channelName)) {
endpoint.setDefaultOutputChannelName(channelName);

View File

@@ -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.GenericMessageEndpoint;
import org.springframework.integration.endpoint.DefaultMessageEndpoint;
import org.springframework.integration.handler.DefaultMessageHandlerAdapter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -85,7 +85,7 @@ public class SubscriberAnnotationPostProcessor implements BeanPostProcessor {
adapter.setMethodName(method.getName());
adapter.setObject(bean);
adapter.afterPropertiesSet();
GenericMessageEndpoint endpoint = new GenericMessageEndpoint();
DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint();
endpoint.setInputChannelName(channelName);
endpoint.setChannelRegistry(messageBus);
endpoint.setHandler(adapter);

View File

@@ -99,7 +99,8 @@
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:sequence>
<xsd:element ref="consumer" minOccurs="0" maxOccurs="1"/>
<xsd:element ref="schedule" minOccurs="0" maxOccurs="1"/>
<xsd:element ref="concurrency-policy" minOccurs="0" maxOccurs="1"/>
<xsd:element ref="handler" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="input-channel" type="xsd:string"/>
@@ -111,17 +112,29 @@
</xsd:complexType>
</xsd:element>
<xsd:element name="consumer">
<xsd:element name="schedule">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a consumer policy.
Defines a schedule.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="period" type="xsd:int"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="concurrency-policy">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a concurrency policy.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="core" type="xsd:int"/>
<xsd:attribute name="max" type="xsd:int"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="handler">
<xsd:complexType>
<xsd:annotation>

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.bus;
package org.springframework.integration.dispatcher;
import java.util.Collection;
import java.util.LinkedList;
@@ -22,14 +22,14 @@ import java.util.List;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.Message;
import org.springframework.util.Assert;
/**
* Message retriever that polls a {@link MessageChannel}. The number of
* messages retrieved per poll is limited by the '<em>maxMessagesPerTask</em>'
* property of the provided {@link ConsumerPolicy}, and the timeout for each
* receive call is determined by the policy's '<em>receiveTimeout</em>'
* property, and the timeout for each receive call is determined by the '<em>receiveTimeout</em>'
* property. In general, it is recommended to use a value of 1 for
* 'maxMessagesPerTask' whenever a non-zero timeout is provided. Otherwise the
* 'maxMessagesPerTask' whenever a significant timeout is provided. Otherwise the
* retriever may be holding on to available messages while waiting for
* additional messages.
*
@@ -39,19 +39,40 @@ public class ChannelPollingMessageRetriever implements MessageRetriever {
private MessageChannel channel;
private ConsumerPolicy policy;
private int maxMessagesPerTask = DispatcherPolicy.DEFAULT_MAX_MESSAGES_PER_TASK;
private long receiveTimeout = DispatcherPolicy.DEFAULT_RECEIVE_TIMEOUT;
public ChannelPollingMessageRetriever(MessageChannel channel, ConsumerPolicy policy) {
public ChannelPollingMessageRetriever(MessageChannel channel) {
Assert.notNull(channel, "'channel' must not be null");
this.channel = channel;
this.policy = policy;
}
public void setMaxMessagesPerTask(int maxMessagesPerTask) {
Assert.isTrue(maxMessagesPerTask > 0, "'maxMessagesPerTask' must be at least 1");
this.maxMessagesPerTask = maxMessagesPerTask;
}
public void setReceiveTimeout(long receiveTimeout) {
this.receiveTimeout = receiveTimeout;
}
public MessageChannel getChannel() {
return this.channel;
}
public Collection<Message<?>> retrieveMessages() {
List<Message<?>> messages = new LinkedList<Message<?>>();
while (messages.size() < this.policy.getMaxMessagesPerTask()) {
Message<?> message = this.channel.receive(this.policy.getReceiveTimeout());
while (messages.size() < this.maxMessagesPerTask) {
Message<?> message = null;
if (this.receiveTimeout < 0) {
message = this.channel.receive();
}
else {
message = this.channel.receive(this.receiveTimeout);
}
if (message == null) {
return messages;
}

View File

@@ -0,0 +1,206 @@
/*
* 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.dispatcher;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.Lifecycle;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.scheduling.MessagingTaskScheduler;
import org.springframework.integration.scheduling.MessagingTaskSchedulerAware;
import org.springframework.integration.scheduling.PollingSchedule;
import org.springframework.integration.scheduling.Schedule;
import org.springframework.integration.scheduling.SimpleMessagingTaskScheduler;
import org.springframework.util.Assert;
/**
* Default implementation of {@link MessageDispatcher}. For a non-broadcasting
* {@link MessageChannel} (point-to-point), each handler can be registered with
* a {@link Schedule}. If the channel is broadcasting (publish-subscribe), the
* handlers will all be scheduled together according to the dispatcher's
* {@link #defaultSchedule}.
*
* @author Mark Fisher
*/
public class DefaultMessageDispatcher implements MessageDispatcher, MessagingTaskSchedulerAware {
protected Log logger = LogFactory.getLog(this.getClass());
private MessageChannel channel;
private int maxMessagesPerTask = DispatcherPolicy.DEFAULT_MAX_MESSAGES_PER_TASK;
private long receiveTimeout = DispatcherPolicy.DEFAULT_RECEIVE_TIMEOUT;
private int rejectionLimit = DispatcherPolicy.DEFAULT_REJECTION_LIMIT;
private long retryInterval = DispatcherPolicy.DEFAULT_RETRY_INTERVAL;
private boolean shouldFailOnRejectionLimit = true;
private MessagingTaskScheduler scheduler;
private Schedule defaultSchedule = new PollingSchedule(5);
private Map<Schedule, List<MessageHandler>> scheduledHandlers = new ConcurrentHashMap<Schedule, List<MessageHandler>>();
private List<ScheduledFuture<?>> futures = new CopyOnWriteArrayList<ScheduledFuture<?>>();
private volatile boolean running;
private Object lifecycleMonitor = new Object();
public DefaultMessageDispatcher(MessageChannel channel) {
Assert.notNull(channel, "'channel' must not be null");
this.channel = channel;
}
public void setMaxMessagesPerTask(int maxMessagesPerTask) {
Assert.isTrue(maxMessagesPerTask > 0, "'maxMessagesPerTask' must be at least 1");
this.maxMessagesPerTask = maxMessagesPerTask;
}
public void setReceiveTimeout(long receiveTimeout) {
this.receiveTimeout = receiveTimeout;
}
public void setRejectionLimit(int rejectionLimit) {
Assert.isTrue(rejectionLimit > 0, "'rejectionLimit' must be at least 1");
this.rejectionLimit = rejectionLimit;
}
public void setShouldFailOnRejectionLimit(boolean shouldFailOnRejectionLimit) {
this.shouldFailOnRejectionLimit = shouldFailOnRejectionLimit;
}
public void setRetryInterval(long retryInterval) {
Assert.isTrue(retryInterval >= 0, "'retryInterval' must not be negative");
this.retryInterval = retryInterval;
}
public void setMessagingTaskScheduler(MessagingTaskScheduler scheduler) {
Assert.notNull(scheduler, "'scheduler' must not be null");
this.scheduler = scheduler;
}
public void setDefaultSchedule(Schedule defaultSchedule) {
Assert.notNull(defaultSchedule, "'defaultSchedule' must not be null");
this.defaultSchedule = defaultSchedule;
}
public void addHandler(MessageHandler handler) {
this.addHandler(handler, null);
}
public void addHandler(MessageHandler handler, Schedule schedule) {
Assert.notNull(handler, "'handler' must not be null");
if (schedule == null) {
schedule = this.defaultSchedule;
}
else if (this.channel.isBroadcaster()) {
if (logger.isInfoEnabled()) {
logger.info("This dispatcher's channel is a broadcaster, and therefore all handlers are " +
"scheduled with its 'defaultSchedule'. The provided schedule will be ignored.");
}
schedule = this.defaultSchedule;
}
if (this.isRunning() && handler instanceof Lifecycle) {
((Lifecycle) handler).start();
}
if (this.scheduledHandlers.containsKey(schedule)) {
this.scheduledHandlers.get(schedule).add(handler);
}
else {
List<MessageHandler> handlerList = new CopyOnWriteArrayList<MessageHandler>();
handlerList.add(handler);
this.scheduledHandlers.put(schedule, handlerList);
}
}
public boolean isRunning() {
return this.running;
}
public void start() {
if (this.scheduler == null) {
if (logger.isInfoEnabled()) {
logger.info("no scheduler was provided, will create one");
}
this.scheduler = new SimpleMessagingTaskScheduler();
}
if (!this.scheduler.isRunning()) {
this.scheduler.start();
}
synchronized (this.lifecycleMonitor) {
if (!this.isRunning()) {
for (Map.Entry<Schedule, List<MessageHandler>> entry : this.scheduledHandlers.entrySet()) {
Schedule schedule = entry.getKey();
List<MessageHandler> handlers = entry.getValue();
ChannelPollingMessageRetriever retriever = new ChannelPollingMessageRetriever(channel);
retriever.setMaxMessagesPerTask(this.maxMessagesPerTask);
retriever.setReceiveTimeout(this.receiveTimeout);
DispatcherTask task = new DispatcherTask(retriever);
task.setSchedule(schedule);
task.setRejectionLimit(this.rejectionLimit);
task.setRetryInterval(this.retryInterval);
task.setBroadcast(channel.isBroadcaster());
task.setShouldFailOnRejectionLimit(this.shouldFailOnRejectionLimit);
for (MessageHandler handler : handlers) {
if (handler instanceof Lifecycle) {
((Lifecycle) handler).start();
}
task.addHandler(handler);
}
ScheduledFuture<?> future = this.scheduler.schedule(task);
if (future != null) {
futures.add(future);
}
}
this.running = true;
}
}
}
public void stop() {
synchronized (this.lifecycleMonitor) {
if (this.isRunning()) {
for (ScheduledFuture<?> future : this.futures) {
future.cancel(true);
for (List<MessageHandler> handlerList : scheduledHandlers.values()) {
for (MessageHandler handler : handlerList) {
if (handler instanceof Lifecycle) {
((Lifecycle) handler).stop();
}
}
}
}
this.running = false;
}
}
}
}

View File

@@ -0,0 +1,82 @@
/*
* 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.dispatcher;
import org.springframework.integration.endpoint.EndpointPolicy;
import org.springframework.util.Assert;
/**
* Metadata for a {@link MessageDispatcher}.
*
* @author Mark Fisher
*/
public class DispatcherPolicy implements EndpointPolicy {
public final static int DEFAULT_MAX_MESSAGES_PER_TASK = 1;
public final static long DEFAULT_RECEIVE_TIMEOUT = 1000;
public final static int DEFAULT_REJECTION_LIMIT = 5;
public final static long DEFAULT_RETRY_INTERVAL = 1000;
private int maxMessagesPerTask = DEFAULT_MAX_MESSAGES_PER_TASK;
private long receiveTimeout = DEFAULT_RECEIVE_TIMEOUT;
private int rejectionLimit = DEFAULT_REJECTION_LIMIT;
private long retryInterval = DEFAULT_RETRY_INTERVAL;
public int getMaxMessagesPerTask() {
return this.maxMessagesPerTask;
}
public void setMaxMessagesPerTask(int maxMessagesPerTask) {
Assert.isTrue(maxMessagesPerTask > 0, "'maxMessagePerTask' must be at least 1");
this.maxMessagesPerTask = maxMessagesPerTask;
}
public long getReceiveTimeout() {
return this.receiveTimeout;
}
public void setReceiveTimeout(long receiveTimeout) {
this.receiveTimeout = receiveTimeout;
}
public int getRejectionLimit() {
return this.rejectionLimit;
}
public void setRejectionLimit(int rejectionLimit) {
Assert.isTrue(rejectionLimit > 0, "'rejectionLimit' must be at least 1");
this.rejectionLimit = rejectionLimit;
}
public long getRetryInterval() {
return this.retryInterval;
}
public void setRetryInterval(long retryInterval) {
Assert.isTrue(retryInterval >= 0, "'retryInterval' must not be negative");
this.retryInterval = retryInterval;
}
}

View File

@@ -14,39 +14,65 @@
* limitations under the License.
*/
package org.springframework.integration.bus;
package org.springframework.integration.dispatcher;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.integration.MessageDeliveryException;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.scheduling.MessagingTask;
import org.springframework.integration.scheduling.Schedule;
import org.springframework.util.Assert;
/**
* The base implementation of a polling {@link MessageDispatcher}. If
* A task for polling {@link MessageDispatcher MessageDispatchers}. If
* {@link #broadcast} is set to <code>false</code> (the default), each message
* will be sent to a single {@link MessageHandler}. Otherwise, each
* retrieved {@link Message} will be sent to all handlers.
* retrieved {@link Message} will be sent to all of the handlers.
*
* @author Mark Fisher
*/
public class BasePollingMessageDispatcher extends AbstractMessageDispatcher {
public class DispatcherTask implements MessagingTask {
private Log logger = LogFactory.getLog(this.getClass());
private boolean broadcast = false;
private Schedule schedule;
private int rejectionLimit = 5;
private long retryInterval = 1000;
private boolean shouldFailOnRejectionLimit = true;
private MessageRetriever retriever;
public BasePollingMessageDispatcher(MessageRetriever retriever) {
super(retriever);
private List<MessageHandler> handlers = new CopyOnWriteArrayList<MessageHandler>();
public DispatcherTask(MessageChannel channel) {
Assert.notNull(channel, "'channel' must not be null");
this.retriever = new ChannelPollingMessageRetriever(channel);
this.broadcast = channel.isBroadcaster();
}
public DispatcherTask(MessageRetriever retriever) {
Assert.notNull(retriever, "'retriever' must not be null");
if (retriever instanceof ChannelPollingMessageRetriever) {
this.broadcast = ((ChannelPollingMessageRetriever) retriever).getChannel().isBroadcaster();
}
this.retriever = retriever;
}
@@ -54,6 +80,15 @@ public class BasePollingMessageDispatcher extends AbstractMessageDispatcher {
this.broadcast = broadcast;
}
public void setSchedule(Schedule schedule) {
Assert.notNull(schedule, "'schedule' must not be null");
this.schedule = schedule;
}
public Schedule getSchedule() {
return this.schedule;
}
public void setRejectionLimit(int rejectionLimit) {
Assert.isTrue(rejectionLimit > 0, "'rejectionLimit' must be at least 1");
this.rejectionLimit = rejectionLimit;
@@ -72,10 +107,33 @@ public class BasePollingMessageDispatcher extends AbstractMessageDispatcher {
this.shouldFailOnRejectionLimit = shouldFailOnRejectionLimit;
}
@Override
public void addHandler(MessageHandler handler) {
Assert.notNull(handler, "'handler' must not be null");
this.handlers.add(handler);
}
/**
* Retrieves messages and dispatches to the executors.
*
* @return the number of messages processed
*/
public int dispatch() {
int messagesProcessed = 0;
Collection<Message<?>> messages = this.retriever.retrieveMessages();
if (messages == null) {
return 0;
}
for (Message<?> message : messages) {
if (dispatchMessage(message)) {
messagesProcessed++;
}
}
return messagesProcessed;
}
protected boolean dispatchMessage(Message<?> message) {
int attempts = 0;
List<MessageHandler> targets = new ArrayList<MessageHandler>(this.getHandlers());
List<MessageHandler> targets = new ArrayList<MessageHandler>(this.handlers);
while (attempts < this.rejectionLimit) {
if (attempts > 0) {
if (logger.isDebugEnabled()) {
@@ -137,4 +195,8 @@ public class BasePollingMessageDispatcher extends AbstractMessageDispatcher {
return false;
}
public void run() {
this.dispatch();
}
}

View File

@@ -14,23 +14,21 @@
* limitations under the License.
*/
package org.springframework.integration.bus;
package org.springframework.integration.dispatcher;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.context.Lifecycle;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.scheduling.Schedule;
/**
* A {@link MessageDispatcher} that polls a {@link MessageChannel}.
* Strategy interface for dispatching messages.
*
* @author Mark Fisher
*/
public class ChannelPollingMessageDispatcher extends BasePollingMessageDispatcher {
public interface MessageDispatcher extends Lifecycle {
public ChannelPollingMessageDispatcher(MessageChannel channel, int period) {
this(channel, ConsumerPolicy.newPollingPolicy(period));
}
void addHandler(MessageHandler handler);
public ChannelPollingMessageDispatcher(MessageChannel channel, ConsumerPolicy policy) {
super(new ChannelPollingMessageRetriever(channel, policy));
}
void addHandler(MessageHandler handler, Schedule schedule);
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.bus;
package org.springframework.integration.dispatcher;
import org.springframework.integration.MessageHandlingException;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.bus;
package org.springframework.integration.dispatcher;
import org.springframework.integration.MessageHandlingException;
@@ -26,6 +26,10 @@ import org.springframework.integration.MessageHandlingException;
*/
public class MessageHandlerRejectedExecutionException extends MessageHandlingException {
public MessageHandlerRejectedExecutionException() {
super();
}
public MessageHandlerRejectedExecutionException(Throwable cause) {
super("handler rejected execution", cause);
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.bus;
package org.springframework.integration.dispatcher;
import java.util.Collection;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.bus;
package org.springframework.integration.dispatcher;
import org.springframework.integration.MessageHandlingException;

View File

@@ -0,0 +1,51 @@
/*
* 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.endpoint;
import org.springframework.util.Assert;
/**
* Metadata for configuring a pool of concurrent threads.
*
* @author Mark Fisher
*/
public class ConcurrencyPolicy implements EndpointPolicy {
private int coreConcurrency;
private int maxConcurrency;
public int getCoreConcurrency() {
return this.coreConcurrency;
}
public void setCoreConcurrency(int coreConcurrency) {
Assert.isTrue(coreConcurrency > 0, "'coreConcurrency' must be at least 1");
this.coreConcurrency = coreConcurrency;
}
public int getMaxConcurrency() {
return this.maxConcurrency;
}
public void setMaxConcurrency(int maxConcurrency) {
Assert.isTrue(maxConcurrency > 0, "'maxConcurrency' must be at least 1");
this.maxConcurrency = maxConcurrency;
}
}

View File

@@ -19,26 +19,18 @@ 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.ConsumerPolicy;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.scheduling.Schedule;
/**
* A generic endpoint implementation designed to accommodate a variety of
* strategies including:
* <ul>
* <li><i>source channel-adapter:</i> source adapter + target channel</li>
* <li><i>target channel-adapter:</i> source channel + target adapter</li>
* <li><i>one-way:</i> source + handler that returns null and no target</li>
* <li><i>request-reply:</i> source + handler and either a reply channel
* specified on the request message or a default target on the endpoint</i>
* </ul>
* Default implementation of the {@link MessageEndpoint} interface.
*
* @author Mark Fisher
*/
public class GenericMessageEndpoint implements MessageEndpoint, BeanNameAware {
public class DefaultMessageEndpoint implements MessageEndpoint, BeanNameAware {
private String name;
@@ -48,9 +40,11 @@ public class GenericMessageEndpoint implements MessageEndpoint, BeanNameAware {
private MessageHandler handler;
private ChannelRegistry channelRegistry;
private Schedule schedule;
private ConsumerPolicy consumerPolicy = new ConsumerPolicy();
private ConcurrencyPolicy concurrencyPolicy;
private ChannelRegistry channelRegistry;
public String getName() {
@@ -79,14 +73,6 @@ public class GenericMessageEndpoint implements MessageEndpoint, BeanNameAware {
return this.inputChannelName;
}
public void setConsumerPolicy(ConsumerPolicy consumerPolicy) {
this.consumerPolicy = consumerPolicy;
}
public ConsumerPolicy getConsumerPolicy() {
return this.consumerPolicy;
}
/**
* Set the name of the channel to which this endpoint can send reply messages by default.
*/
@@ -105,6 +91,22 @@ public class GenericMessageEndpoint implements MessageEndpoint, BeanNameAware {
this.handler = handler;
}
public Schedule getSchedule() {
return this.schedule;
}
public void setSchedule(Schedule schedule) {
this.schedule = schedule;
}
public ConcurrencyPolicy getConcurrencyPolicy() {
return this.concurrencyPolicy;
}
public void setConcurrencyPolicy(ConcurrencyPolicy concurrencyPolicy) {
this.concurrencyPolicy = concurrencyPolicy;
}
/**
* Set the channel registry to use for looking up channels by name.
*/
@@ -112,7 +114,6 @@ public class GenericMessageEndpoint implements MessageEndpoint, BeanNameAware {
this.channelRegistry = channelRegistry;
}
public Message handle(Message<?> message) {
if (this.handler == null) {
if (this.defaultOutputChannelName == null) {

View File

@@ -14,17 +14,13 @@
* limitations under the License.
*/
package org.springframework.integration.bus;
import org.springframework.context.Lifecycle;
package org.springframework.integration.endpoint;
/**
* Strategy interface for dispatching messages.
* A marker interface for endpoint metadata.
*
* @author Mark Fisher
*/
public interface MessageDispatcher extends Lifecycle {
int dispatch();
public interface EndpointPolicy {
}

View File

@@ -16,9 +16,9 @@
package org.springframework.integration.endpoint;
import org.springframework.integration.bus.ConsumerPolicy;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.scheduling.Schedule;
/**
* Base interface for message endpoints.
@@ -37,8 +37,10 @@ public interface MessageEndpoint extends MessageHandler {
String getDefaultOutputChannelName();
ConsumerPolicy getConsumerPolicy();
void setChannelRegistry(ChannelRegistry channelRegistry);
Schedule getSchedule();
ConcurrencyPolicy getConcurrencyPolicy();
}

View File

@@ -33,9 +33,9 @@ public abstract class AbstractMessagingTaskScheduler implements MessagingTaskSch
* Submit a task to be run once.
*/
public void execute(Runnable task) {
this.schedule(new DefaultMessagingTask(task));
this.schedule(task);
}
public abstract ScheduledFuture<?> schedule(MessagingTask task);
public abstract ScheduledFuture<?> schedule(Runnable task);
}

View File

@@ -1,61 +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.scheduling;
/**
* A wrapper for {@link Runnable Runnables} that provides a schedule and also
* captures any error that may be thrown from the run() method.
*
* @author Mark Fisher
*/
public class DefaultMessagingTask implements MessagingTask {
private Runnable runnable;
private Schedule schedule;
private Throwable lastError;
public DefaultMessagingTask(Runnable runnable) {
this(runnable, null);
}
public DefaultMessagingTask(Runnable runnable, Schedule schedule) {
this.runnable = runnable;
this.schedule = schedule;
}
public Schedule getSchedule() {
return this.schedule;
}
public Throwable getLastError() {
return this.lastError;
}
public void run() {
try {
this.runnable.run();
}
catch (Throwable t) {
this.lastError = t;
}
}
}

View File

@@ -0,0 +1,68 @@
/*
* 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.scheduling;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.ErrorMessage;
import org.springframework.integration.util.ErrorHandler;
/**
* {@link ErrorHandler} implementation that sends an {@link ErrorMessage} to a
* {@link MessageChannel}.
*
* @author Mark Fisher
*/
public class MessagePublishingErrorHandler implements ErrorHandler {
private Log logger = LogFactory.getLog(this.getClass());
private MessageChannel errorChannel;
private long sendTimeout = 1000;
public MessagePublishingErrorHandler() {
}
public MessagePublishingErrorHandler(MessageChannel errorChannel) {
this.errorChannel = errorChannel;
}
public void setErrorChannel(MessageChannel errorChannel) {
this.errorChannel = errorChannel;
}
public void handle(Throwable t) {
if (this.errorChannel != null) {
try {
if(this.errorChannel.send(new ErrorMessage(t), this.sendTimeout)) {
return;
}
}
catch (Throwable ignore) { // message will be logged only
}
}
if (logger.isWarnEnabled()) {
logger.warn("failure occurred in messaging task", t);
}
}
}

View File

@@ -26,8 +26,4 @@ public interface MessagingTask extends Runnable {
Schedule getSchedule();
Throwable getLastError();
void run();
}

View File

@@ -18,6 +18,7 @@ package org.springframework.integration.scheduling;
import java.util.concurrent.ScheduledFuture;
import org.springframework.context.Lifecycle;
import org.springframework.scheduling.SchedulingTaskExecutor;
/**
@@ -25,8 +26,8 @@ import org.springframework.scheduling.SchedulingTaskExecutor;
*
* @author Mark Fisher
*/
public interface MessagingTaskScheduler extends SchedulingTaskExecutor {
public interface MessagingTaskScheduler extends SchedulingTaskExecutor, Lifecycle {
ScheduledFuture<?> schedule(MessagingTask task);
ScheduledFuture<?> schedule(Runnable task);
}

View File

@@ -0,0 +1,29 @@
/*
* 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.scheduling;
/**
* Callback interface for components that require the
* {@link MessagingTaskScheduler}.
*
* @author Mark Fisher
*/
public interface MessagingTaskSchedulerAware {
void setMessagingTaskScheduler(MessagingTaskScheduler scheduler);
}

View File

@@ -16,12 +16,17 @@
package org.springframework.integration.scheduling;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.util.ErrorHandler;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import org.springframework.util.Assert;
/**
@@ -36,6 +41,18 @@ public class SimpleMessagingTaskScheduler extends AbstractMessagingTaskScheduler
private int corePoolSize = 10;
private ThreadFactory threadFactory;
private String threadNamePrefix = this.getClass().getSimpleName() + "-";
private ErrorHandler errorHandler;
private Set<Runnable> pendingTasks = new CopyOnWriteArraySet<Runnable>();
private volatile boolean running;
private Object lifecycleMonitor = new Object();
public void setExecutor(ScheduledExecutorService executor) {
Assert.notNull(executor, "'executor' must not be null");
@@ -47,47 +64,108 @@ public class SimpleMessagingTaskScheduler extends AbstractMessagingTaskScheduler
this.corePoolSize = corePoolSize;
}
public void setThreadFactory(ThreadFactory threadFactory) {
this.threadFactory = threadFactory;
}
public void setThreadNamePrefix(String threadNamePrefix) {
Assert.notNull(threadNamePrefix, "'threadNamePrefix' must not be null");
this.threadNamePrefix = threadNamePrefix;
}
public void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
public void afterPropertiesSet() {
if (this.executor == null) {
this.executor = new ScheduledThreadPoolExecutor(this.corePoolSize);
if (this.threadFactory == null) {
this.threadFactory = new CustomizableThreadFactory(this.threadNamePrefix);
}
this.executor = new ScheduledThreadPoolExecutor(this.corePoolSize, this.threadFactory);
}
}
public boolean isRunning() {
return this.running;
}
public void start() {
if (this.executor == null) {
this.afterPropertiesSet();
}
synchronized (this.lifecycleMonitor) {
this.running = true;
for (Runnable task : this.pendingTasks) {
this.schedule(task);
}
this.pendingTasks.clear();
}
}
public void stop() {
if (this.isRunning()) {
this.running = false;
this.executor.shutdownNow();
}
}
@Override
public ScheduledFuture<?> schedule(MessagingTask task) {
if (this.executor == null) {
this.afterPropertiesSet();
public ScheduledFuture<?> schedule(Runnable task) {
if (!this.isRunning()) {
this.pendingTasks.add(task);
return null;
}
Schedule schedule = task.getSchedule();
Schedule schedule = (task instanceof MessagingTask) ? ((MessagingTask) task).getSchedule() : null;
MessagingTaskRunner runner = new MessagingTaskRunner(task);
if (schedule == null) {
return this.executor.schedule(task, 0, TimeUnit.MILLISECONDS);
return this.executor.schedule(runner, 0, TimeUnit.MILLISECONDS);
}
if (schedule instanceof PollingSchedule) {
PollingSchedule ps = (PollingSchedule) schedule;
if (ps.getPeriod() <= 0) {
return this.executor.schedule(new RepeatingTask(task), ps.getInitialDelay(), ps.getTimeUnit());
runner.setShouldRepeat(true);
return this.executor.schedule(runner, ps.getInitialDelay(), ps.getTimeUnit());
}
if (ps.getFixedRate()) {
return this.executor.scheduleAtFixedRate(task, ps.getInitialDelay(), ps.getPeriod(), ps.getTimeUnit());
return this.executor.scheduleAtFixedRate(runner, ps.getInitialDelay(), ps.getPeriod(), ps.getTimeUnit());
}
return this.executor.scheduleWithFixedDelay(task, ps.getInitialDelay(), ps.getPeriod(), ps.getTimeUnit());
return this.executor.scheduleWithFixedDelay(runner, ps.getInitialDelay(), ps.getPeriod(), ps.getTimeUnit());
}
throw new UnsupportedOperationException(this.getClass().getName() + " does not support schedule type '"
+ schedule.getClass().getName() + "'");
}
private class RepeatingTask implements Runnable {
private class MessagingTaskRunner implements Runnable {
private MessagingTask task;
private Runnable task;
RepeatingTask(MessagingTask task) {
private boolean shouldRepeat;
public MessagingTaskRunner(Runnable task) {
this.task = task;
}
public void setShouldRepeat(boolean shouldRepeat) {
this.shouldRepeat = shouldRepeat;
}
public void run() {
task.run();
executor.execute(new RepeatingTask(task));
try {
this.task.run();
}
catch (Throwable t) {
if (errorHandler != null) {
errorHandler.handle(t);
}
}
if (this.shouldRepeat) {
MessagingTaskRunner runner = new MessagingTaskRunner(this.task);
runner.setShouldRepeat(true);
executor.execute(runner);
}
}
}

View File

@@ -14,21 +14,15 @@
* limitations under the License.
*/
package org.springframework.integration.channel;
package org.springframework.integration.util;
/**
* Enumeration of the different types of message consumer.
* Strategy for handling a {@link Throwable}.
*
* @author Mark Fisher
*/
public enum ConsumerType {
public interface ErrorHandler {
EVENT_DRIVEN,
FIXED_RATE,
FIXED_DELAY,
SCHEDULED
void handle(Throwable t);
}