Merge pull request #491 from olegz/INT-2594

This commit is contained in:
Gary Russell
2012-06-13 16:06:31 -04:00
3 changed files with 66 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 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,6 +16,10 @@
package org.springframework.integration.aggregator;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelParserConfiguration;
@@ -28,11 +32,13 @@ import org.springframework.util.Assert;
* {@link CorrelationStrategy} implementation that evaluates an expression.
*
* @author Dave Syer
* @author Oleg Zhurakousky
*/
public class ExpressionEvaluatingCorrelationStrategy implements CorrelationStrategy {
public class ExpressionEvaluatingCorrelationStrategy implements CorrelationStrategy, BeanFactoryAware, InitializingBean{
private static final ExpressionParser expressionParser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
private volatile BeanFactory beanFactory;
private final ExpressionEvaluatingMessageProcessor<Object> processor;
@@ -47,9 +53,18 @@ public class ExpressionEvaluatingCorrelationStrategy implements CorrelationStrat
this.processor = new ExpressionEvaluatingMessageProcessor<Object>(expression, Object.class);
}
public Object getCorrelationKey(Message<?> message) {
return processor.processMessage(message);
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public void afterPropertiesSet() throws Exception {
if (this.beanFactory != null){
this.processor.setBeanFactory(this.beanFactory);
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Copyright 2002-2012 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.
@@ -14,17 +14,25 @@
package org.springframework.integration.aggregator;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.support.MessageBuilder;
/**
* @author Alex Peters
* @author Oleg Zhurakousky
*/
public class ExpressionEvaluatingCorrelationStrategyTests {
@@ -51,4 +59,22 @@ public class ExpressionEvaluatingCorrelationStrategyTests {
assertThat(correlationKey, is(String.class));
assertThat((String) correlationKey, is("b"));
}
@Test
public void testCorrelationStrategyWithAtBeanExpression() throws Exception {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("expression-evaluating-correlation-with-bf.xml", this.getClass());
MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class);
QueueChannel outputChannel = context.getBean("outputChannel", QueueChannel.class);
Message<?> message = MessageBuilder.withPayload("foo").setSequenceNumber(1).setSequenceSize(1).build();
inputChannel.send(message);
Message<?> reply = outputChannel.receive(0);
assertNotNull(reply);
}
public static class CustomCorrelator {
public Object correlate(Object o){
return o;
}
}
}

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
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-2.2.xsd">
<int:aggregator input-channel="inputChannel" output-channel="outputChannel"
correlation-strategy-expression="@correlator.correlate(payload)"/>
<int:channel id="outputChannel">
<int:queue/>
</int:channel>
<bean id="correlator"
class="org.springframework.integration.aggregator.ExpressionEvaluatingCorrelationStrategyTests.CustomCorrelator"/>
</beans>