From 0ee69a78feb36fd979da3f9c29d6000681b66d9c Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Thu, 25 Jun 2009 03:04:39 +0000 Subject: [PATCH] INT-684 Added support for a Comparator to the FailOverDispatcher --- .../dispatcher/FailOverDispatcher.java | 33 +++++++++++++++++-- .../dispatcher/RoundRobinDispatcher.java | 8 +++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/dispatcher/FailOverDispatcher.java b/org.springframework.integration/src/main/java/org/springframework/integration/dispatcher/FailOverDispatcher.java index 8a5fc736df..4823e2f5e7 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/dispatcher/FailOverDispatcher.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/dispatcher/FailOverDispatcher.java @@ -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. * + *

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 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 comparator) { + Assert.notNull(comparator, "comparator must not be null"); + this.comparator = comparator; + } @Override protected Iterator getHandlerIterator(List handlers) { - return handlers.iterator(); + List handlerList = new ArrayList(handlers); + Collections.sort(handlerList, this.comparator); + return handlerList.iterator(); } } diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/dispatcher/RoundRobinDispatcher.java b/org.springframework.integration/src/main/java/org/springframework/integration/dispatcher/RoundRobinDispatcher.java index ca80b83601..f376eaa3d1 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/dispatcher/RoundRobinDispatcher.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/dispatcher/RoundRobinDispatcher.java @@ -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 getHandlerIterator(List handlers) { int size = handlers.size(); int nextHandlerStartIndex = getNextHandlerStartIndex(size); - List reorderedHandlers = new ArrayList(handlers.subList(nextHandlerStartIndex, - size)); + List reorderedHandlers = new ArrayList( + 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; } + }