Renamed MessageSelectorChain's enum from Strategy to VotingStrategy and changed MORE_THAN_HALF and AT_LEAST_HALF to MAJORITY and MAJORITY_OR_TIE respectively.
This commit is contained in:
@@ -27,9 +27,7 @@ import org.springframework.beans.factory.support.ManagedList;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.selector.MessageSelectorChain;
|
||||
import org.springframework.integration.selector.MessageSelectorChain.Strategy;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Parser for the <selector-chain/> element.
|
||||
@@ -50,10 +48,7 @@ public class SelectorChainParser extends AbstractSingleBeanDefinitionParser {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void parseSelectorChain(BeanDefinitionBuilder builder, Element element, ParserContext parserContext) {
|
||||
String strategy = element.getAttribute("strategy");
|
||||
if (StringUtils.hasText(strategy)) {
|
||||
builder.addPropertyValue("strategy", Strategy.valueOf(strategy));
|
||||
}
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "voting-strategy");
|
||||
ManagedList selectors = new ManagedList();
|
||||
NodeList childNodes = element.getChildNodes();
|
||||
for (int i = 0; i < childNodes.getLength(); i++) {
|
||||
|
||||
@@ -295,18 +295,18 @@
|
||||
<xsd:element ref="selector-chain"/>
|
||||
</xsd:choice>
|
||||
<xsd:attribute name="id" type="xsd:ID"/>
|
||||
<xsd:attribute name="strategy" default="ALL">
|
||||
<xsd:attribute name="voting-strategy" default="ALL">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Enumeration with values that match the MessageSelectorChain.Strategy enum.
|
||||
Enumeration with values that match the MessageSelectorChain.VotingStrategy enum.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:simpleType>
|
||||
<xsd:restriction base="xsd:string">
|
||||
<xsd:enumeration value="ALL"/>
|
||||
<xsd:enumeration value="ANY"/>
|
||||
<xsd:enumeration value="AT_LEAST_HALF"/>
|
||||
<xsd:enumeration value="MORE_THAN_HALF"/>
|
||||
<xsd:enumeration value="MAJORITY"/>
|
||||
<xsd:enumeration value="MAJORITY_OR_TIE"/>
|
||||
</xsd:restriction>
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
|
||||
@@ -16,9 +16,8 @@
|
||||
|
||||
package org.springframework.integration.endpoint;
|
||||
|
||||
|
||||
/**
|
||||
* Base interface for message endpoints.
|
||||
* Base interface for Message Endpoints.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
|
||||
@@ -24,24 +24,29 @@ import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A message selector implementation that passes incoming messages through a
|
||||
* chain of selectors. The chain will be broken by any selector that returns
|
||||
* <em>false</em>.
|
||||
* chain of selectors. Whether the Message is {@link #accept(Message) accepted}
|
||||
* is based upon the tallied results of the individual selectors' responses in
|
||||
* accordance with this chain's {@link VotingStrategy}.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class MessageSelectorChain implements MessageSelector {
|
||||
|
||||
public static enum Strategy { ALL, MORE_THAN_HALF, AT_LEAST_HALF, ANY };
|
||||
public static enum VotingStrategy { ALL, ANY, MAJORITY, MAJORITY_OR_TIE };
|
||||
|
||||
|
||||
private volatile Strategy strategy = Strategy.ALL;
|
||||
private volatile VotingStrategy votingStrategy = VotingStrategy.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;
|
||||
/**
|
||||
* Specify the voting strategy for this selector chain.
|
||||
* <p>The default is {@link VotingStrategy#ALL}.
|
||||
*/
|
||||
public void setVotingStrategy(VotingStrategy votingStrategy) {
|
||||
Assert.notNull(votingStrategy, "votingStrategy must not be null");
|
||||
this.votingStrategy = votingStrategy;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,9 +75,10 @@ public class MessageSelectorChain implements MessageSelector {
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass the message through the selector chain. As soon as a
|
||||
* selector returns 'false', this method will return 'false'.
|
||||
* If all selectors accept, this method will return 'true'.
|
||||
* Pass the message through the selector chain. Whether the Message is
|
||||
* {@link #accept(Message) accepted} is based upon the tallied results of
|
||||
* the individual selectors' responses in accordance with this chain's
|
||||
* {@link VotingStrategy}.
|
||||
*/
|
||||
public final boolean accept(Message<?> message) {
|
||||
int count = 0;
|
||||
@@ -80,12 +86,12 @@ public class MessageSelectorChain implements MessageSelector {
|
||||
for (MessageSelector next : this.selectors) {
|
||||
count++;
|
||||
if (next.accept(message)) {
|
||||
if (this.strategy.equals(Strategy.ANY)) {
|
||||
if (this.votingStrategy.equals(VotingStrategy.ANY)) {
|
||||
return true;
|
||||
}
|
||||
accepted++;
|
||||
}
|
||||
else if (this.strategy.equals(Strategy.ALL)) {
|
||||
else if (this.votingStrategy.equals(VotingStrategy.ALL)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -96,17 +102,17 @@ public class MessageSelectorChain implements MessageSelector {
|
||||
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);
|
||||
switch (this.votingStrategy) {
|
||||
case ANY:
|
||||
return true;
|
||||
case ALL:
|
||||
return (accepted == total);
|
||||
case MAJORITY:
|
||||
return (2 * accepted) > total;
|
||||
case MAJORITY_OR_TIE:
|
||||
return (2 * accepted) >= total;
|
||||
default:
|
||||
throw new IllegalArgumentException("unsupported voting strategy " + this.votingStrategy);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user