INT-3512: Add <advice-chain> to the Aggregator

JIRA: https://jira.spring.io/browse/INT-3512

INT-3512: Fix typos

INT-3512: add `expire-` prefix to the advice sub-elements

INT-3512: Mark `AggregatorWithCustomReleaseStrategyTests` as `LONG_RUNNING_TEST`

Doc Polishing
This commit is contained in:
Artem Bilan
2014-09-12 19:29:41 +03:00
committed by Gary Russell
parent 113716effd
commit d5f1bb6516
10 changed files with 291 additions and 17 deletions

View File

@@ -24,9 +24,11 @@ import java.util.UUID;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.locks.Lock;
import org.aopalliance.aop.Advice;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
@@ -116,6 +118,10 @@ public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageP
private volatile Expression groupTimeoutExpression;
private volatile List<Advice> forceReleaseAdviceChain;
private MessageGroupProcessor forceReleaseProcessor = new ForceReleaseMessageGroupProcessor();
private EvaluationContext evaluationContext;
private volatile ApplicationEventPublisher applicationEventPublisher;
@@ -157,7 +163,7 @@ public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageP
store.registerMessageGroupExpiryCallback(new MessageGroupCallback() {
@Override
public void execute(MessageGroupStore messageGroupStore, MessageGroup group) {
forceComplete(group);
forceReleaseProcessor.processMessageGroup(group);
}
});
}
@@ -177,6 +183,11 @@ public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageP
this.groupTimeoutExpression = groupTimeoutExpression;
}
public void setForceReleaseAdviceChain(List<Advice> forceReleaseAdviceChain) {
Assert.notNull(forceReleaseAdviceChain, "forceReleaseAdviceChain must not be null");
this.forceReleaseAdviceChain = forceReleaseAdviceChain;
}
@Override
public void setIntegrationEvaluationContext(EvaluationContext evaluationContext) {
this.evaluationContext = evaluationContext;
@@ -226,6 +237,20 @@ public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageP
* (checked in the setter).
*/
this.lockRegistrySet = true;
this.forceReleaseProcessor = createGroupTimeoutProcessor();
}
private MessageGroupProcessor createGroupTimeoutProcessor() {
MessageGroupProcessor processor = new ForceReleaseMessageGroupProcessor();
if (this.groupTimeoutExpression != null && !CollectionUtils.isEmpty(this.forceReleaseAdviceChain)) {
ProxyFactory proxyFactory = new ProxyFactory(processor);
for (Advice advice : this.forceReleaseAdviceChain) {
proxyFactory.addAdvice(advice);
}
return (MessageGroupProcessor) proxyFactory.getProxy(getApplicationContext().getClassLoader());
}
return processor;
}
public void setDiscardChannel(MessageChannel discardChannel) {
@@ -409,7 +434,7 @@ public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageP
@Override
public void run() {
try {
AbstractCorrelatingMessageHandler.this.forceComplete(messageGroup);
forceReleaseProcessor.processMessageGroup(messageGroup);
}
catch (MessageDeliveryException e) {
if (logger.isDebugEnabled()) {
@@ -427,7 +452,7 @@ public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageP
this.expireGroupScheduledFutures.put(UUIDConverter.getUUID(messageGroup.getGroupId()), scheduledFuture);
}
else {
forceComplete(messageGroup);
this.forceReleaseProcessor.processMessageGroup(messageGroup);
}
}
}
@@ -735,4 +760,14 @@ public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageP
}
private class ForceReleaseMessageGroupProcessor implements MessageGroupProcessor {
@Override
public Object processMessageGroup(MessageGroup group) {
forceComplete(group);
return null;
}
}
}

View File

@@ -22,6 +22,7 @@ import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.aggregator.AbstractCorrelatingMessageHandler;
import org.springframework.integration.config.IntegrationConfigUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
/**
* Base class for parsers that create an instance of {@link AbstractCorrelatingMessageHandler}
@@ -58,7 +59,8 @@ public abstract class AbstractCorrelatingMessageHandlerParser extends AbstractCo
private static final String SEND_PARTIAL_RESULT_ON_EXPIRY_ATTRIBUTE = "send-partial-result-on-expiry";
protected void doParse(BeanDefinitionBuilder builder, Element element, BeanMetadataElement processor, ParserContext parserContext){
protected void doParse(BeanDefinitionBuilder builder, Element element, BeanMetadataElement processor,
ParserContext parserContext) {
this.injectPropertyWithAdapter(CORRELATION_STRATEGY_REF_ATTRIBUTE, CORRELATION_STRATEGY_METHOD_ATTRIBUTE,
CORRELATION_STRATEGY_EXPRESSION_ATTRIBUTE, CORRELATION_STRATEGY_PROPERTY, "CorrelationStrategy",
element, builder, processor, parserContext);
@@ -72,12 +74,19 @@ public abstract class AbstractCorrelatingMessageHandlerParser extends AbstractCo
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "lock-registry");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, SEND_TIMEOUT_ATTRIBUTE);
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, SEND_PARTIAL_RESULT_ON_EXPIRY_ATTRIBUTE);
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "empty-group-min-timeout", "minimumTimeoutForEmptyGroups");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "empty-group-min-timeout",
"minimumTimeoutForEmptyGroups");
BeanDefinition expressionDef =
IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression("group-timeout", "group-timeout-expression",
parserContext, element, false);
IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression("group-timeout",
"group-timeout-expression", parserContext, element, false);
builder.addPropertyValue("groupTimeoutExpression", expressionDef);
Element txElement = DomUtils.getChildElementByTagName(element, "expire-transactional");
Element adviceChainElement = DomUtils.getChildElementByTagName(element, "expire-advice-chain");
IntegrationNamespaceUtils.configureAndSetAdviceChainIfPresent(adviceChainElement, txElement,
builder.getRawBeanDefinition(), parserContext, "forceReleaseAdviceChain");
}
protected void injectPropertyWithAdapter(String beanRefAttribute, String methodRefAttribute,
@@ -92,8 +101,8 @@ public abstract class AbstractCorrelatingMessageHandlerParser extends AbstractCo
final boolean hasExpression = StringUtils.hasText(expression);
if (hasBeanRef && hasExpression) {
parserContext.getReaderContext().error("Exactly one of the '" + beanRefAttribute + "' or '" + expressionAttribute +
"' attribute is allowed.", element);
parserContext.getReaderContext().error("Exactly one of the '" + beanRefAttribute + "' or '"
+ expressionAttribute + "' attribute is allowed.", element);
}
BeanMetadataElement adapter = null;
@@ -126,4 +135,5 @@ public abstract class AbstractCorrelatingMessageHandlerParser extends AbstractCo
}
return builder.getBeanDefinition();
}
}

View File

@@ -46,7 +46,6 @@ import org.springframework.messaging.MessagingException;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
/**
@@ -237,7 +236,7 @@ public class DelayHandler extends AbstractReplyProducingMessageHandler implement
for (Advice advice : delayedAdviceChain) {
proxyFactory.addAdvice(advice);
}
return (MessageHandler) proxyFactory.getProxy(ClassUtils.getDefaultClassLoader());
return (MessageHandler) proxyFactory.getProxy(getApplicationContext().getClassLoader());
}
return releaseHandler;
}

View File

@@ -3435,6 +3435,15 @@
<xsd:complexType name="correlating-message-handler-type">
<xsd:complexContent>
<xsd:extension base="innerEndpointDefinitionAware">
<xsd:choice>
<xsd:annotation>
<xsd:documentation>
'transactional' or 'advice-chain' are applied only to 'forceComplete' operation.
</xsd:documentation>
</xsd:annotation>
<xsd:element name="expire-transactional" type="transactionalType" minOccurs="0" maxOccurs="1" />
<xsd:element name="expire-advice-chain" type="adviceChainType" minOccurs="0" maxOccurs="1" />
</xsd:choice>
<xsd:attribute name="correlation-strategy" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>

View File

@@ -21,7 +21,12 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -37,6 +42,40 @@ import org.springframework.messaging.MessageChannel;
*/
public class AggregatorWithCustomReleaseStrategyTests {
@Rule
public TestWatcher longTests = new TestWatcher() {
private static final String RUN_LONG_PROP = "RUN_LONG_INTEGRATION_TESTS";
private boolean shouldRun;
{
for(String value: new String[]{System.getenv(RUN_LONG_PROP), System.getProperty(RUN_LONG_PROP)}) {
if ("true".equalsIgnoreCase(value)) {
this.shouldRun = true;
break;
}
}
}
@Override
public Statement apply(Statement base, Description description) {
if (!this.shouldRun) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Assume.assumeTrue(false);
}
};
}
else {
return super.apply(base, description);
}
}
};
private static ExecutorService executor = Executors.newCachedThreadPool();
@AfterClass

View File

@@ -308,7 +308,7 @@ public class JdbcMessageStore extends AbstractMessageGroupStore implements Messa
@Override
@ManagedAttribute
public long getMessageCount() {
return jdbcTemplate.queryForObject(getQuery(Query.GET_MESSAGE_COUNT), Integer.class, region);
return jdbcTemplate.queryForObject(getQuery(Query.GET_MESSAGE_COUNT), Long.class, region);
}
@Override
@@ -334,7 +334,7 @@ public class JdbcMessageStore extends AbstractMessageGroupStore implements Messa
final long createdDate = System.currentTimeMillis();
Message<T> result = this.getMessageBuilderFactory().fromMessage(message).setHeader(SAVED_KEY, Boolean.TRUE)
.setHeader(CREATED_DATE_KEY, new Long(createdDate)).build();
.setHeader(CREATED_DATE_KEY, createdDate).build();
Map innerMap = (Map) new DirectFieldAccessor(result.getHeaders()).getPropertyValue("headers");
// using reflection to set ID since it is immutable through MessageHeaders
@@ -452,7 +452,7 @@ public class JdbcMessageStore extends AbstractMessageGroupStore implements Messa
}
long timestamp = createDate.get().getTime();
boolean complete = completeFlag.get().booleanValue();
boolean complete = completeFlag.get();
SimpleMessageGroup messageGroup = new SimpleMessageGroup(messages, groupId, timestamp, complete);
messageGroup.setLastModified(updateDate.get().getTime());
@@ -710,8 +710,9 @@ public class JdbcMessageStore extends AbstractMessageGroupStore implements Messa
@Override
public Message<?> mapRow(ResultSet rs, int rowNum) throws SQLException {
Message<?> message = (Message<?>) deserializer.convert(lobHandler.getBlobAsBytes(rs, "MESSAGE_BYTES"));
return message;
return (Message<?>) deserializer.convert(lobHandler.getBlobAsBytes(rs, "MESSAGE_BYTES"));
}
}
}

View File

@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/integration"
xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/integration/jdbc
http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="int-derby.properties"/>
<jdbc:embedded-database id="dataSource" type="DERBY"/>
<jdbc:initialize-database data-source="dataSource"
ignore-failures="DROPS">
<jdbc:script location="${int.drop.script}"/>
<jdbc:script location="${int.schema.script}"/>
</jdbc:initialize-database>
<int-jdbc:message-store id="messageStore" data-source="dataSource"/>
<beans:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<beans:property name="dataSource" ref="dataSource"/>
</beans:bean>
<aggregator input-channel="transactionalAggregatorInput" output-channel="transactionalAggregatorOutput"
message-store="messageStore"
send-partial-result-on-expiry="true"
group-timeout="100">
<expire-transactional/>
</aggregator>
<service-activator input-channel="transactionalAggregatorOutput" ref="exceptionHandler"/>
<beans:bean id="exceptionHandler"
class="org.springframework.integration.jdbc.AggregatorIntegrationTests$ExceptionMessageHandler"/>
</beans:beans>

View File

@@ -0,0 +1,102 @@
/*
* Copyright 2014 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.jdbc;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.IntegrationMessageHeaderAccessor;
import org.springframework.integration.store.MessageGroupStore;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;
/**
* @author Artem Bilan
* @since 4.1
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
public class AggregatorIntegrationTests {
@Autowired
private MessageChannel transactionalAggregatorInput;
@Autowired
private MessageGroupStore messageGroupStore;
@Test
public void testTransactionalAggregatorGroupTimeout() throws InterruptedException {
this.transactionalAggregatorInput.send(new GenericMessage<Integer>(1, stubHeaders(1, 2, 1)));
assertTrue(RollbackTxSync.latch.await(20, TimeUnit.SECONDS));
//As far as we have been within TX, the message group should still be in the MessageStore
assertEquals(1, this.messageGroupStore.messageGroupSize(1));
}
private Map<String, Object> stubHeaders(int sequenceNumber, int sequenceSize, int correlationId) {
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, sequenceNumber);
headers.put(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE, sequenceSize);
headers.put(IntegrationMessageHeaderAccessor.CORRELATION_ID, correlationId);
return headers;
}
@SuppressWarnings("unused")
private static class ExceptionMessageHandler implements MessageHandler {
public void handleMessage(Message<?> message) throws MessagingException {
TransactionSynchronizationManager.registerSynchronization(new RollbackTxSync());
throw new RuntimeException("intentional");
}
}
private static class RollbackTxSync extends TransactionSynchronizationAdapter {
public static CountDownLatch latch = new CountDownLatch(1);
@Override
public void afterCompletion(int status) {
if (TransactionSynchronization.STATUS_ROLLED_BACK == status) {
latch.countDown();
}
}
}
}

View File

@@ -376,7 +376,10 @@ then you should simply provide an implementation of the <classname>ReleaseStrate
group-timeout-expression="size() ge 2 ? 100 : -1" ]]><co id="aggxml21" linkends="aggxml21-txt" /><![CDATA[
expire-groups-on-timeout="true" ]]><co id="aggxml22" linkends="aggxml22-txt" /><![CDATA[
scheduler="taskScheduler" /> ]]><co id="aggxml23" linkends="aggxml23-txt" /><![CDATA[
scheduler="taskScheduler" > ]]><co id="aggxml23" linkends="aggxml23-txt" /><![CDATA[
<expire-transactional/> ]]><co id="aggxml24" linkends="aggxml24-txt" /><![CDATA[
<expire-advice-chain/> ]]><co id="aggxml25" linkends="aggxml25-txt" /><![CDATA[
</aggregator>
<int:channel id="outputChannel"/>
@@ -610,6 +613,26 @@ then you should simply provide an implementation of the <classname>ReleaseStrate
<code>group-timeout-expression</code> is not specified.
</para>
</callout>
<callout arearefs="aggxml24" id="aggxml24-txt">
<para>
Since <emphasis>version 4.1</emphasis>. Allows a transaction to be started for the
<code>forceComplete</code> operation. It is initiated from a
<code>group-timeout(-expression)</code> or by a <classname>MessageGroupStoreReaper</classname> and
is not applied to the normal <code>add/release/discard</code> operations. Only this sub-element or
<code>&lt;expire-advice-chain/&gt;</code> is allowed.
</para>
</callout>
<callout arearefs="aggxml25" id="aggxml25-txt">
<para>
Since <emphasis>version 4.1</emphasis>. Allows the configuration of any
<interfacename>Advice</interfacename> for the
<code>forceComplete</code> operation. It is initiated from a
<code>group-timeout(-expression)</code> or by a <classname>MessageGroupStoreReaper</classname> and
is not applied to the normal <code>add/release/discard</code> operations. Only this sub-element or
<code>&lt;expire-transactional/&gt;</code> is allowed. A transaction <interfacename>Advice</interfacename>
can also be configured here using the Spring <code>tx</code> namespace.
</para>
</callout>
</calloutlist>

View File

@@ -203,5 +203,14 @@
See <xref linkend="async-gateway"/>.
</para>
</section>
<section id="4.1-aggregator-advice-chain">
<title>Aggregator Advice Chain</title>
<para>
<code>Aggregator</code>s and <code>Resequencer</code>s now support an
<code>&lt;expire-advice-chain/&gt;</code> and <code>&lt;expire-transactional/&gt;</code> sub-elements
to <emphasis>advise</emphasis> the <code>forceComplete</code> operation.
See <xref linkend="aggregator-config"/> for more information.
</para>
</section>
</section>
</chapter>