Moved MessageSelector interface and implementations into a new 'org.springframework.integration.selector' package (instead of a sub-package under 'message'). Also added support for "Strategy" enum in MessageSelector with the following values available [ALL, ANY, AT_LEAST_HALF, MORE_THAN_HALF] (INT-308).

This commit is contained in:
Mark Fisher
2008-10-14 16:16:55 +00:00
parent 9286f1fdaa
commit 109343d14e
37 changed files with 372 additions and 142 deletions

View File

@@ -20,7 +20,7 @@ import java.util.ArrayList;
import java.util.List;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.integration.selector.MessageSelector;
import org.springframework.util.Assert;
/**

View File

@@ -26,7 +26,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.integration.selector.MessageSelector;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;

View File

@@ -22,7 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.integration.selector.MessageSelector;
/**
* A channel implementation that essentially behaves like "/dev/null".

View File

@@ -19,7 +19,7 @@ package org.springframework.integration.channel;
import java.util.List;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.integration.selector.MessageSelector;
/**
* @author Mark Fisher

View File

@@ -23,7 +23,7 @@ import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.integration.selector.MessageSelector;
import org.springframework.util.Assert;
/**

View File

@@ -22,7 +22,7 @@ import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.integration.selector.MessageSelector;
/**
* A channel implementation that stores messages in a thread-bound queue. In

View File

@@ -22,7 +22,7 @@ import java.util.List;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageDeliveryException;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.integration.selector.MessageSelector;
/**
* A {@link org.springframework.integration.channel.ChannelInterceptor} that

View File

@@ -24,7 +24,7 @@ import org.springframework.integration.channel.BlockingChannel;
import org.springframework.integration.channel.ChannelInterceptor;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.integration.selector.MessageSelector;
import org.springframework.util.Assert;
/**

View File

@@ -27,7 +27,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.channel.interceptor.MessageSelectingInterceptor;
import org.springframework.integration.message.selector.PayloadTypeSelector;
import org.springframework.integration.selector.PayloadTypeSelector;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;

View File

@@ -24,7 +24,7 @@ import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.integration.message.selector.MessageSelectorChain;
import org.springframework.integration.selector.MessageSelectorChain;
/**
* Parser for the <selector-chain/> element.

View File

@@ -29,7 +29,7 @@ import org.springframework.integration.message.MessageHeaders;
import org.springframework.integration.message.MessageProducer;
import org.springframework.integration.message.MessageRejectedException;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.integration.selector.MessageSelector;
import org.springframework.util.Assert;
/**

View File

@@ -19,7 +19,7 @@ package org.springframework.integration.filter;
import org.springframework.integration.endpoint.AbstractReplyProducingMessageConsumer;
import org.springframework.integration.endpoint.ReplyMessageHolder;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.integration.selector.MessageSelector;
import org.springframework.util.Assert;
/**

View File

@@ -20,7 +20,7 @@ import java.lang.reflect.Method;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageMappingMethodInvoker;
import org.springframework.integration.message.selector.MessageSelector;
import org.springframework.integration.selector.MessageSelector;
import org.springframework.util.Assert;
/**

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.message.selector;
package org.springframework.integration.selector;
import org.springframework.integration.message.Message;

View File

@@ -14,12 +14,13 @@
* limitations under the License.
*/
package org.springframework.integration.message.selector;
package org.springframework.integration.selector;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.springframework.integration.message.Message;
import org.springframework.util.Assert;
/**
* A message selector implementation that passes incoming messages through a
@@ -30,9 +31,19 @@ import org.springframework.integration.message.Message;
*/
public class MessageSelectorChain implements MessageSelector {
public static enum Strategy { ALL, MORE_THAN_HALF, AT_LEAST_HALF, ANY };
private volatile Strategy strategy = Strategy.ALL;
private final List<MessageSelector> selectors = new CopyOnWriteArrayList<MessageSelector>();
public void setStrategy(Strategy strategy) {
Assert.notNull(strategy, "strategy must not be null");
this.strategy = strategy;
}
/**
* Add a selector to the end of the chain.
*/
@@ -51,8 +62,11 @@ public class MessageSelectorChain implements MessageSelector {
* Initialize the selector chain. Removes any existing selectors.
*/
public void setSelectors(List<MessageSelector> selectors) {
this.selectors.clear();
this.selectors.addAll(selectors);
Assert.notEmpty(selectors, "selectors must not be empty");
synchronized (this.selectors) {
this.selectors.clear();
this.selectors.addAll(selectors);
}
}
/**
@@ -61,12 +75,39 @@ public class MessageSelectorChain implements MessageSelector {
* If all selectors accept, this method will return 'true'.
*/
public final boolean accept(Message<?> message) {
int count = 0;
int accepted = 0;
for (MessageSelector next : this.selectors) {
if (!next.accept(message)) {
count++;
if (next.accept(message)) {
if (this.strategy.equals(Strategy.ANY)) {
return true;
}
accepted++;
}
else if (this.strategy.equals(Strategy.ALL)) {
return false;
}
}
return true;
return this.decide(accepted, count);
}
private boolean decide(int accepted, int total) {
if (accepted == 0) {
return false;
}
switch (this.strategy) {
case ANY:
return true;
case ALL:
return (accepted == total);
case MORE_THAN_HALF:
return (2 * accepted) > total;
case AT_LEAST_HALF:
return (2 * accepted) >= total;
default:
throw new IllegalArgumentException("unsupported strategy " + this.strategy);
}
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.message.selector;
package org.springframework.integration.selector;
import java.util.ArrayList;
import java.util.List;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.message.selector;
package org.springframework.integration.selector;
import org.springframework.integration.message.Message;