replaced Transformer object with a SpEL expression

This commit is contained in:
Mark Fisher
2010-05-08 22:35:42 +00:00
parent 4388226bdc
commit 58c5138ec0
3 changed files with 13 additions and 43 deletions

View File

@@ -20,7 +20,7 @@ import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
@@ -37,27 +37,32 @@ import org.springframework.integration.core.Message;
* channels if customer's credit score (retrieved from message header 'CREDIT_SCORE') is above 780.
*
* @author Oleg Zhurakousky
* @author Mark Fisher
*/
public class BanksChannelSelector {
private static Logger logger = Logger.getLogger(BanksChannelSelector.class);
private Map<String, String> bankChannelPool;
private static final ExpressionParser parser = new SpelExpressionParser();
private volatile Expression routingExpression;
private final StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
private String routingExpression;
/**
* @param bankChannelPool
*/
public BanksChannelSelector(Map<String, String> bankChannelPool) {
this.bankChannelPool = bankChannelPool;
this.evaluationContext.setVariable("banks", bankChannelPool);
}
/**
* @param routingExpression
*/
public void setRoutingExpression(String routingExpression) {
this.routingExpression = routingExpression;
this.routingExpression = parser.parseExpression(routingExpression);
}
/**
@@ -66,11 +71,7 @@ public class BanksChannelSelector {
*/
@SuppressWarnings("unchecked")
public Set<String> selectBankChannels(Message<?> message) {
EvaluationContext context = new StandardEvaluationContext(message);
context.setVariable("banks", bankChannelPool);
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression(routingExpression);
Set<String> bankChannels = (Set<String>) expression.getValue(context);
Set<String> bankChannels = (Set<String>) routingExpression.getValue(this.evaluationContext, message);
logger.debug("selected bank channels: " + bankChannels);
return bankChannels;
}

View File

@@ -1,29 +0,0 @@
/*
* 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.
*/
package org.springframework.integration.loanbroker;
import org.springframework.integration.loanbroker.domain.LoanQuote;
/**
* @author Gary Russell
*/
public class Transformer {
public String transformShark(LoanQuote quote) {
return quote.getLender() + "," + quote.getRate();
}
}

View File

@@ -22,12 +22,10 @@
<channel id="loanSharkChannel" />
<transformer ref="udpTransformer"
input-channel="loanSharkChannel"
<transformer input-channel="loanSharkChannel"
expression="payload.lender + ',' + payload.rate"
output-channel="sharkOutChannel"/>
<beans:bean id="udpTransformer" class="org.springframework.integration.loanbroker.Transformer"/>
<channel id="sharkOutChannel" />
<int-ip:outbound-channel-adapter id="udpOut"