INT-1119: added TimeoutCountSequenceSizeReleaseStrategy
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.aggregator;
|
||||
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.store.MessageGroup;
|
||||
|
||||
/**
|
||||
* A {@link ReleaseStrategy} that releases all messages if any of the following is true:
|
||||
*
|
||||
* <ul>
|
||||
* <li>The sequence is complete (if there is one).</li>
|
||||
* <li>There are more messages than a threshold set by the user.</li>
|
||||
* <li>The time elapsed since the earliest message, according to their timestamps, exceeds a timeout set by the user.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
public class TimeoutCountSequenceSizeReleaseStrategy implements ReleaseStrategy {
|
||||
|
||||
/**
|
||||
* Default timeout is one minute.
|
||||
*/
|
||||
public static final long DEFAULT_TIMEOUT = 60 * 1000;
|
||||
|
||||
/**
|
||||
* Default threshold is effectively infinite.
|
||||
*/
|
||||
public static final int DEFAULT_THRESHOLD = Integer.MAX_VALUE;
|
||||
|
||||
private final int threshold;
|
||||
|
||||
private final long timeout;
|
||||
|
||||
public TimeoutCountSequenceSizeReleaseStrategy() {
|
||||
this(DEFAULT_THRESHOLD, DEFAULT_TIMEOUT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param threshold the number of messages to accept before releasing
|
||||
* @param timeout the timeout for the release in milliseconds
|
||||
*/
|
||||
public TimeoutCountSequenceSizeReleaseStrategy(int threshold, long timeout) {
|
||||
this.threshold = threshold;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public boolean canRelease(MessageGroup messages) {
|
||||
long elapsedTime = System.currentTimeMillis() - findEarliestTimestamp(messages);
|
||||
return messages.isComplete() || messages.getUnmarked().size() >= threshold || elapsedTime > timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param messages the message group
|
||||
* @return the earliest timestamp or Long.MAX_VALUE
|
||||
*/
|
||||
private long findEarliestTimestamp(MessageGroup messages) {
|
||||
long result = Long.MAX_VALUE;
|
||||
for (Message<?> message : messages.getUnmarked()) {
|
||||
long timestamp = message.getHeaders().getTimestamp();
|
||||
if (timestamp < result) {
|
||||
result = timestamp;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -36,8 +36,8 @@ public class SequenceSizeReleaseStrategyTests {
|
||||
.setSequenceSize(2).build();
|
||||
MessageGroup messages = new SimpleMessageGroup("FOO");
|
||||
messages.add(message);
|
||||
SequenceSizeReleaseStrategy ReleaseStrategy = new SequenceSizeReleaseStrategy();
|
||||
assertFalse(ReleaseStrategy.canRelease(messages));
|
||||
SequenceSizeReleaseStrategy releaseStrategy = new SequenceSizeReleaseStrategy();
|
||||
assertFalse(releaseStrategy.canRelease(messages));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -49,14 +49,14 @@ public class SequenceSizeReleaseStrategyTests {
|
||||
MessageGroup messages = new SimpleMessageGroup("FOO");
|
||||
messages.add(message1);
|
||||
messages.add(message2);
|
||||
SequenceSizeReleaseStrategy ReleaseStrategy = new SequenceSizeReleaseStrategy();
|
||||
assertTrue(ReleaseStrategy.canRelease(messages));
|
||||
SequenceSizeReleaseStrategy releaseStrategy = new SequenceSizeReleaseStrategy();
|
||||
assertTrue(releaseStrategy.canRelease(messages));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyList() {
|
||||
SequenceSizeReleaseStrategy ReleaseStrategy = new SequenceSizeReleaseStrategy();
|
||||
assertTrue(ReleaseStrategy.canRelease(new SimpleMessageGroup("FOO")));
|
||||
SequenceSizeReleaseStrategy releaseStrategy = new SequenceSizeReleaseStrategy();
|
||||
assertTrue(releaseStrategy.canRelease(new SimpleMessageGroup("FOO")));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.aggregator;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.store.MessageGroup;
|
||||
import org.springframework.integration.store.SimpleMessageGroup;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class TimeoutCountSequenceSizeReleaseStrategyTests {
|
||||
|
||||
@Test
|
||||
public void testIncompleteList() {
|
||||
Message<String> message = MessageBuilder.withPayload("test1")
|
||||
.setSequenceSize(2).build();
|
||||
MessageGroup messages = new SimpleMessageGroup("FOO");
|
||||
messages.add(message);
|
||||
TimeoutCountSequenceSizeReleaseStrategy releaseStrategy = new TimeoutCountSequenceSizeReleaseStrategy();
|
||||
assertFalse(releaseStrategy.canRelease(messages));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncompleteListWithTimeout() {
|
||||
Message<String> message = MessageBuilder.withPayload("test1")
|
||||
.setSequenceSize(2).build();
|
||||
MessageGroup messages = new SimpleMessageGroup("FOO");
|
||||
messages.add(message);
|
||||
TimeoutCountSequenceSizeReleaseStrategy releaseStrategy = new TimeoutCountSequenceSizeReleaseStrategy(TimeoutCountSequenceSizeReleaseStrategy.DEFAULT_THRESHOLD, -100);
|
||||
assertTrue(releaseStrategy.canRelease(messages));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncompleteListWithCount() {
|
||||
Message<String> message = MessageBuilder.withPayload("test1")
|
||||
.setSequenceSize(2).build();
|
||||
MessageGroup messages = new SimpleMessageGroup("FOO");
|
||||
messages.add(message);
|
||||
TimeoutCountSequenceSizeReleaseStrategy releaseStrategy = new TimeoutCountSequenceSizeReleaseStrategy(1, TimeoutCountSequenceSizeReleaseStrategy.DEFAULT_TIMEOUT);
|
||||
assertTrue(releaseStrategy.canRelease(messages));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompleteList() {
|
||||
Message<String> message1 = MessageBuilder.withPayload("test1")
|
||||
.setSequenceSize(2).build();
|
||||
Message<String> message2 = MessageBuilder.withPayload("test2")
|
||||
.setSequenceSize(2).build();
|
||||
MessageGroup messages = new SimpleMessageGroup("FOO");
|
||||
messages.add(message1);
|
||||
messages.add(message2);
|
||||
SequenceSizeReleaseStrategy releaseStrategy = new SequenceSizeReleaseStrategy();
|
||||
assertTrue(releaseStrategy.canRelease(messages));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyList() {
|
||||
SequenceSizeReleaseStrategy releaseStrategy = new SequenceSizeReleaseStrategy();
|
||||
assertTrue(releaseStrategy.canRelease(new SimpleMessageGroup("FOO")));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,67 +1,67 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<channel id="outputChannel">
|
||||
<queue capacity="5"/>
|
||||
</channel>
|
||||
<channel id="discardChannel">
|
||||
<queue capacity="5"/>
|
||||
</channel>
|
||||
|
||||
<channel id="aggregatorWithReferenceInput"/>
|
||||
<aggregator id="aggregatorWithReference" ref="aggregatorBean"
|
||||
input-channel="aggregatorWithReferenceInput" output-channel="outputChannel"/>
|
||||
|
||||
<channel id="completelyDefinedAggregatorInput"/>
|
||||
<aggregator id="completelyDefinedAggregator"
|
||||
input-channel="completelyDefinedAggregatorInput"
|
||||
output-channel="outputChannel"
|
||||
discard-channel="discardChannel"
|
||||
ref="aggregatorBean"
|
||||
release-strategy="releaseStrategy"
|
||||
correlation-strategy="correlationStrategy"
|
||||
send-timeout="86420000"
|
||||
send-partial-result-on-timeout="true"
|
||||
reaper-interval="135"
|
||||
tracked-correlation-id-capacity="99"
|
||||
timeout="42"/>
|
||||
|
||||
<channel id="aggregatorWithReferenceAndMethodInput"/>
|
||||
<aggregator id="aggregatorWithReferenceAndMethod"
|
||||
ref="adderBean"
|
||||
method="add"
|
||||
input-channel="aggregatorWithReferenceAndMethodInput"
|
||||
output-channel="outputChannel"/>
|
||||
|
||||
<channel id="aggregatorWithPojoReleaseStrategyInput"/>
|
||||
<aggregator id="aggregatorWithPojoReleaseStrategy"
|
||||
input-channel="aggregatorWithPojoReleaseStrategyInput"
|
||||
output-channel="outputChannel"
|
||||
ref="adderBean"
|
||||
method="add"
|
||||
release-strategy="pojoReleaseStrategy"
|
||||
release-strategy-method="checkCompleteness"/>
|
||||
|
||||
<beans:bean id="aggregatorBean"
|
||||
class="org.springframework.integration.config.TestAggregatorBean" />
|
||||
|
||||
<beans:bean id="adderBean"
|
||||
class="org.springframework.integration.config.Adder" />
|
||||
|
||||
<beans:bean id="releaseStrategy"
|
||||
class="org.springframework.integration.config.TestReleaseStrategy" />
|
||||
|
||||
<beans:bean id="correlationStrategy" class="org.springframework.integration.config.TestCorrelationStrategy"/>
|
||||
|
||||
<beans:bean id="pojoReleaseStrategy"
|
||||
class="org.springframework.integration.config.MaxValueReleaseStrategy">
|
||||
<beans:constructor-arg value="10" />
|
||||
</beans:bean>
|
||||
|
||||
</beans:beans>
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<channel id="outputChannel">
|
||||
<queue capacity="5"/>
|
||||
</channel>
|
||||
<channel id="discardChannel">
|
||||
<queue capacity="5"/>
|
||||
</channel>
|
||||
|
||||
<channel id="aggregatorWithReferenceInput"/>
|
||||
<aggregator id="aggregatorWithReference" ref="aggregatorBean"
|
||||
input-channel="aggregatorWithReferenceInput" output-channel="outputChannel"/>
|
||||
|
||||
<channel id="completelyDefinedAggregatorInput"/>
|
||||
<aggregator id="completelyDefinedAggregator"
|
||||
input-channel="completelyDefinedAggregatorInput"
|
||||
output-channel="outputChannel"
|
||||
discard-channel="discardChannel"
|
||||
ref="aggregatorBean"
|
||||
release-strategy="releaseStrategy"
|
||||
correlation-strategy="correlationStrategy"
|
||||
send-timeout="86420000"
|
||||
send-partial-result-on-timeout="true"
|
||||
reaper-interval="135"
|
||||
tracked-correlation-id-capacity="99"
|
||||
timeout="42"/>
|
||||
|
||||
<channel id="aggregatorWithReferenceAndMethodInput"/>
|
||||
<aggregator id="aggregatorWithReferenceAndMethod"
|
||||
ref="adderBean"
|
||||
method="add"
|
||||
input-channel="aggregatorWithReferenceAndMethodInput"
|
||||
output-channel="outputChannel"/>
|
||||
|
||||
<channel id="aggregatorWithPojoReleaseStrategyInput"/>
|
||||
<aggregator id="aggregatorWithPojoReleaseStrategy"
|
||||
input-channel="aggregatorWithPojoReleaseStrategyInput"
|
||||
output-channel="outputChannel"
|
||||
ref="adderBean"
|
||||
method="add"
|
||||
release-strategy="pojoReleaseStrategy"
|
||||
release-strategy-method="checkCompleteness"/>
|
||||
|
||||
<beans:bean id="aggregatorBean"
|
||||
class="org.springframework.integration.config.TestAggregatorBean" />
|
||||
|
||||
<beans:bean id="adderBean"
|
||||
class="org.springframework.integration.config.Adder" />
|
||||
|
||||
<beans:bean id="releaseStrategy"
|
||||
class="org.springframework.integration.config.TestReleaseStrategy" />
|
||||
|
||||
<beans:bean id="correlationStrategy" class="org.springframework.integration.config.TestCorrelationStrategy"/>
|
||||
|
||||
<beans:bean id="pojoReleaseStrategy"
|
||||
class="org.springframework.integration.config.MaxValueReleaseStrategy">
|
||||
<beans:constructor-arg value="10" />
|
||||
</beans:bean>
|
||||
|
||||
</beans:beans>
|
||||
|
||||
Reference in New Issue
Block a user