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:
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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".
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user