INT-684 Added support for a Comparator to the FailOverDispatcher
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2009 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.
|
||||
@@ -16,24 +16,51 @@
|
||||
|
||||
package org.springframework.integration.dispatcher;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.OrderComparator;
|
||||
import org.springframework.integration.message.MessageHandler;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link AbstractUnicastDispatcher} that will try it's handlers in the
|
||||
* {@link AbstractUnicastDispatcher} that will try its handlers in the
|
||||
* same order every dispatch.
|
||||
*
|
||||
* <p>The {@link Comparator} that determines the order may be provided via the
|
||||
* {@link #setComparator(Comparator)} method. If none is provided, the default
|
||||
* will be an instance of {@link OrderComparator}, and any {@link MessageHandler}
|
||||
* that implements {@link org.springframework.core.Ordered} will be ordered
|
||||
* accordingly. Any other handlers will be placed at the end of the list.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Iwein Fuld
|
||||
*/
|
||||
public class FailOverDispatcher extends AbstractUnicastDispatcher {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Comparator<Object> comparator = new OrderComparator();
|
||||
|
||||
|
||||
/**
|
||||
* Provide a {@link Comparator} that will determine the sorting order of
|
||||
* the handler list and hence the order of the failover chain. If no
|
||||
* comparator is configured, the default will be an instance of
|
||||
* {@link OrderComparator}.
|
||||
*/
|
||||
public void setComparator(Comparator<Object> comparator) {
|
||||
Assert.notNull(comparator, "comparator must not be null");
|
||||
this.comparator = comparator;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Iterator<MessageHandler> getHandlerIterator(List<MessageHandler> handlers) {
|
||||
return handlers.iterator();
|
||||
List<MessageHandler> handlerList = new ArrayList<MessageHandler>(handlers);
|
||||
Collections.sort(handlerList, this.comparator);
|
||||
return handlerList.iterator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2009 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.
|
||||
@@ -13,6 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.dispatcher;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -43,8 +44,8 @@ public class RoundRobinDispatcher extends AbstractUnicastDispatcher {
|
||||
protected Iterator<MessageHandler> getHandlerIterator(List<MessageHandler> handlers) {
|
||||
int size = handlers.size();
|
||||
int nextHandlerStartIndex = getNextHandlerStartIndex(size);
|
||||
List<MessageHandler> reorderedHandlers = new ArrayList<MessageHandler>(handlers.subList(nextHandlerStartIndex,
|
||||
size));
|
||||
List<MessageHandler> reorderedHandlers = new ArrayList<MessageHandler>(
|
||||
handlers.subList(nextHandlerStartIndex, size));
|
||||
reorderedHandlers.addAll(handlers.subList(0, nextHandlerStartIndex));
|
||||
return reorderedHandlers.iterator();
|
||||
}
|
||||
@@ -58,4 +59,5 @@ public class RoundRobinDispatcher extends AbstractUnicastDispatcher {
|
||||
int indexTail = currentHandlerIndex.getAndIncrement() % size;
|
||||
return indexTail < 0 ? indexTail + size : indexTail;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user