INT-4192: Improve ExprEvaluatingReleaseStrategy
JIRA: https://jira.spring.io/browse/INT-4192 To avoid implicit overhead for loading messages from persistent message store, use entire `MessageGroup` as root object for expression evaluation context Also see https://github.com/spring-projects/spring-integration/wiki/Spring-Integration-4.3-to-5.0-Migration-Guide
This commit is contained in:
committed by
Gary Russell
parent
b3f461e139
commit
d3fb8b8f9e
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -16,26 +16,37 @@
|
||||
|
||||
package org.springframework.integration.aggregator;
|
||||
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.integration.store.MessageGroup;
|
||||
import org.springframework.integration.util.AbstractExpressionEvaluator;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A {@link ReleaseStrategy} that evaluates an expression.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class ExpressionEvaluatingReleaseStrategy extends ExpressionEvaluatingMessageListProcessor implements
|
||||
ReleaseStrategy {
|
||||
public class ExpressionEvaluatingReleaseStrategy extends AbstractExpressionEvaluator implements ReleaseStrategy {
|
||||
|
||||
private final Expression expression;
|
||||
|
||||
public ExpressionEvaluatingReleaseStrategy(String expression) {
|
||||
super(expression, Boolean.class);
|
||||
Assert.hasText(expression, "'expression' must not be empty");
|
||||
this.expression = EXPRESSION_PARSER.parseExpression(expression);
|
||||
}
|
||||
|
||||
public ExpressionEvaluatingReleaseStrategy(Expression expression) {
|
||||
Assert.notNull(expression, "'expression' must not be null");
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate the expression provided on the messages (a collection) in the group and return the result (must
|
||||
* be boolean).
|
||||
* Evaluate the expression provided on the {@link MessageGroup}
|
||||
* and return the result (must be boolean).
|
||||
*/
|
||||
public boolean canRelease(MessageGroup messages) {
|
||||
return (Boolean) process(messages.getMessages());
|
||||
return evaluateExpression(this.expression, messages, Boolean.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -31,6 +31,7 @@ import org.springframework.integration.store.SimpleMessageGroup;
|
||||
* @author Alex Peters
|
||||
* @author Dave Syer
|
||||
* @author Gary Russell
|
||||
* @author Artem Bilan
|
||||
*
|
||||
*/
|
||||
public class ExpressionEvaluatingReleaseStrategyTests {
|
||||
@@ -56,14 +57,14 @@ public class ExpressionEvaluatingReleaseStrategyTests {
|
||||
|
||||
@Test
|
||||
public void testCompletedWithFilterSpelEvaluated() throws Exception {
|
||||
strategy = new ExpressionEvaluatingReleaseStrategy("!?[payload==5].empty");
|
||||
strategy = new ExpressionEvaluatingReleaseStrategy("!messages.?[payload==5].empty");
|
||||
strategy.setBeanFactory(mock(BeanFactory.class));
|
||||
assertThat(strategy.canRelease(messages), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompletedWithFilterSpelReturnsNotCompleted() throws Exception {
|
||||
strategy = new ExpressionEvaluatingReleaseStrategy("!?[payload==6].empty");
|
||||
strategy = new ExpressionEvaluatingReleaseStrategy("!messages.?[payload==6].empty");
|
||||
strategy.setBeanFactory(mock(BeanFactory.class));
|
||||
assertThat(strategy.canRelease(messages), is(false));
|
||||
}
|
||||
|
||||
@@ -43,13 +43,13 @@
|
||||
<aggregator input-channel="groupTimeoutExpressionAggregatorInput" output-channel="output" discard-channel="discard"
|
||||
send-partial-result-on-expiry="true"
|
||||
group-timeout-expression="size() ge 2 ? 100 : -1"
|
||||
release-strategy-expression="[0].headers.sequenceNumber == [0].headers.sequenceSize"/>
|
||||
release-strategy-expression="messages[0].headers.sequenceNumber == messages[0].headers.sequenceSize"/>
|
||||
|
||||
<aggregator input-channel="zeroGroupTimeoutExpressionAggregatorInput" output-channel="output" discard-channel="discard"
|
||||
send-partial-result-on-expiry="true"
|
||||
send-timeout="10"
|
||||
group-timeout-expression="@bool.getAndSet(true) ? 0 : 10"
|
||||
release-strategy-expression="[0].headers.sequenceNumber == [0].headers.sequenceSize"/>
|
||||
release-strategy-expression="messages[0].headers.sequenceNumber == messages[0].headers.sequenceSize"/>
|
||||
|
||||
<beans:bean id="bool" class="java.util.concurrent.atomic.AtomicBoolean" />
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
<!--Auction scenario-->
|
||||
<scatter-gather input-channel="inputAuction" output-channel="output" scatter-channel="auctionChannel">
|
||||
<gatherer release-strategy-expression="^[payload gt 5] != null or size() == 3"/>
|
||||
<gatherer release-strategy-expression="messages.^[payload gt 5] != null or size() == 3"/>
|
||||
</scatter-gather>
|
||||
|
||||
<task:executor id="threadPoolTaskExecutor" pool-size="10"/>
|
||||
@@ -34,7 +34,7 @@
|
||||
<recipient channel="distribution2Channel"/>
|
||||
<recipient channel="distribution3Channel"/>
|
||||
</scatterer>
|
||||
<gatherer release-strategy-expression="^[payload gt 5] != null or size() == 3"/>
|
||||
<gatherer release-strategy-expression="messages.^[payload gt 5] != null or size() == 3"/>
|
||||
</scatter-gather>
|
||||
|
||||
<channel id="gatherChannel">
|
||||
@@ -55,7 +55,7 @@
|
||||
<gateway id="gateway" default-request-channel="gatewayAuction"/>
|
||||
|
||||
<scatter-gather input-channel="gatewayAuction" scatter-channel="auctionChannel">
|
||||
<gatherer release-strategy-expression="^[payload gt 5] != null or size() == 3"/>
|
||||
<gatherer release-strategy-expression="messages.^[payload gt 5] != null or size() == 3"/>
|
||||
</scatter-gather>
|
||||
|
||||
<chain input-channel="scatterGatherWithinChain" output-channel="output">
|
||||
|
||||
Reference in New Issue
Block a user