INT-603: add configurable comparator to Resequencer

This commit is contained in:
David Syer
2010-05-06 10:49:24 +00:00
parent 5bad12c86c
commit 7f72f94cf6
6 changed files with 129 additions and 59 deletions

View File

@@ -1,17 +1,14 @@
/*
* Copyright 2002-2010 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.integration.aggregator;
@@ -37,10 +34,19 @@ import org.springframework.integration.store.MessageGroup;
*/
public class Resequencer implements ReleaseStrategy, MessageGroupProcessor {
private volatile SequenceNumberComparator sequenceNumberComparator = new SequenceNumberComparator();
private volatile Comparator<Message<?>> comparator = new SequenceNumberComparator();
private volatile boolean releasePartialSequences;
/**
* A comparator to use to order messages before processing. The default is to order by sequence number.
*
* @param comparator the comparator to use to order messages
*/
public void setComparator(Comparator<Message<?>> comparator) {
this.comparator = comparator;
}
/**
* Flag that determines if partial sequences are allowed. If true then as soon as enough messages arrive that can be
* ordered they will be released, provided they all have sequence numbers greater than those already released.
@@ -54,7 +60,7 @@ public class Resequencer implements ReleaseStrategy, MessageGroupProcessor {
public boolean canRelease(MessageGroup messages) {
if (releasePartialSequences) {
List<Message<?>> sorted = new ArrayList<Message<?>>(messages.getUnmarked());
Collections.sort(sorted, sequenceNumberComparator);
Collections.sort(sorted, comparator);
int head = sorted.get(sorted.size() - 1).getHeaders().getSequenceNumber();
int tail = sorted.get(0).getHeaders().getSequenceNumber() - 1;
return tail == messages.getMarked().size() && head - tail == sorted.size();
@@ -66,7 +72,7 @@ public class Resequencer implements ReleaseStrategy, MessageGroupProcessor {
Collection<Message<?>> messages = group.getUnmarked();
if (messages.size() > 0) {
List<Message<?>> sorted = new ArrayList<Message<?>>(messages);
Collections.sort(sorted, sequenceNumberComparator);
Collections.sort(sorted, comparator);
for (Message<?> message : sorted) {
channelTemplate.send(message, outputChannel);
}

View File

@@ -37,6 +37,7 @@ public class ResequencerParser extends AbstractConsumerEndpointParser {
BeanDefinitionBuilder processorBuilder = BeanDefinitionBuilder.genericBeanDefinition(
IntegrationNamespaceUtils.BASE_PACKAGE + ".aggregator.Resequencer");
IntegrationNamespaceUtils.setValueIfAttributeDefined(processorBuilder, element, "release-partial-sequences");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(processorBuilder, element, "comparator");
String processorRef = BeanDefinitionReaderUtils.registerWithGeneratedName(processorBuilder
.getBeanDefinition(), parserContext.getRegistry());

View File

@@ -1792,6 +1792,15 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="comparator" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="java.util.Comparator" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="release-partial-sequences" type="xsd:string" />
<xsd:attribute name="send-partial-result-on-timeout" type="xsd:string" />
</xsd:extension>