simplifying Loan Broker sample app

This commit is contained in:
Mark Fisher
2010-10-04 18:35:11 -04:00
parent 1a9ad90f95
commit 2d730d4472
15 changed files with 83 additions and 89 deletions

View File

@@ -1 +1,2 @@
target
.settings

View File

@@ -16,11 +16,10 @@
package org.springframework.integration.samples.loanbroker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.integration.Message;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.samples.loanbroker.domain.LoanQuote;
/**
@@ -37,17 +36,13 @@ public class LoanQuoteAggregator {
/**
* Aggregates LoanQuote Messages to return a single reply Message.
*
* @param messages Messages received from upstream lenders.
* @param quotes list of loan quotes received from upstream lenders
* @param responseType header that indicates the response type
* @return the best {@link LoanQuote} if the 'RESPONSE_TYPE' header value is 'BEST' else all quotes
*/
public Object aggregateQuotes(List<Message<LoanQuote>> messages) {
ArrayList<LoanQuote> payloads = new ArrayList<LoanQuote>(messages.size());
for (Message<LoanQuote> message : messages) {
payloads.add(message.getPayload());
}
Collections.sort(payloads);
String responseType = messages.get(0).getHeaders().get("RESPONSE_TYPE", String.class);
return ("BEST".equals(responseType)) ? payloads.get(0) : payloads;
public Object aggregateQuotes(List<LoanQuote> quotes, @Header("RESPONSE_TYPE") String responseType) {
Collections.sort(quotes);
return ("BEST".equals(responseType)) ? quotes.get(0) : quotes;
}
}

View File

@@ -19,25 +19,25 @@ package org.springframework.integration.samples.loanbroker.domain;
/**
* @author Oleg Zhurakousky
*/
public class CreditScore {
public class CreditReport {
private final int score;
private String creditHistory;
public CreditScore(int score){
this.score = score;
}
public String getCreditHistory() {
return creditHistory;
}
private volatile String history;
public void setCreditHistory(String creditHistory) {
this.creditHistory = creditHistory;
public CreditReport(int score) {
this.score = score;
}
public int getScore() {
return score;
return this.score;
}
public void setHistory(String history) {
this.history = history;
}
public String getHistory() {
return this.history;
}
}

View File

@@ -16,7 +16,6 @@
package org.springframework.integration.samples.loanbroker.domain;
import java.text.DecimalFormat;
import java.util.Date;
/**
@@ -79,17 +78,6 @@ public class LoanQuote implements Comparable<LoanQuote>{
this.rate = rate;
}
public String toString(){
return "\n====== Loan Quote =====\n" +
"Lender: " + this.lender + "\n" +
"Loan amount: " + new DecimalFormat("$###,###.###").format(this.amount) + "\n" +
"Quote Date: " + this.quoteDate + "\n" +
"Expiration Date: " + this.expirationDate + "\n" +
"Term: " + this.term + " years" + "\n" +
"Rate: " + this.rate + "%\n" +
"=======================\n";
}
public int compareTo(LoanQuote other) {
if (this.rate > other.rate) {
return 1;
@@ -100,4 +88,8 @@ public class LoanQuote implements Comparable<LoanQuote>{
return 0;
}
public String toString() {
return this.lender + ":\t" + this.rate;
}
}

View File

@@ -24,20 +24,20 @@ public class LoanRequest {
private Customer customer;
private float loanAmount;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public float getLoanAmount() {
return loanAmount;
public Customer getCustomer() {
return customer;
}
public void setLoanAmount(float loanAmount) {
this.loanAmount = loanAmount;
}
public float getLoanAmount() {
return loanAmount;
}
}

View File

@@ -27,6 +27,18 @@ import org.springframework.integration.samples.loanbroker.domain.LoanRequest;
*/
public class BankStub {
private volatile String name;
private float baseRate = 6.0f;
public void setName(String name) {
this.name = name;
}
public void setBaseRate(float baseRate) {
this.baseRate = baseRate;
}
/**
* @param loanRequest the loan request
* @return a LoanQuote for the given request
@@ -38,10 +50,10 @@ public class BankStub {
loanQuote.setQuoteDate(calendar.getTime());
calendar.add(Calendar.DAY_OF_YEAR, random.nextInt(25));
loanQuote.setExpirationDate(calendar.getTime());
loanQuote.setRate(random.nextFloat() + 5);
loanQuote.setRate(random.nextFloat() + this.baseRate);
loanQuote.setTerm(10 + random.nextInt(10));
loanQuote.setAmount(250000 + random.nextInt(40000));
loanQuote.setLender("StubBank-" + random.nextInt(30));
loanQuote.setLender(this.name);
return loanQuote;
}

View File

@@ -20,7 +20,7 @@ import java.util.Random;
import org.apache.log4j.Logger;
import org.springframework.integration.samples.loanbroker.domain.CreditScore;
import org.springframework.integration.samples.loanbroker.domain.CreditReport;
import org.springframework.integration.samples.loanbroker.domain.LoanRequest;
/**
@@ -32,13 +32,12 @@ public class CreditBureauStub {
/**
* @param loanRequest the loan request
* @return the CreditScore for the given loan request
* @return the CreditReport for the given loan request
*/
public CreditScore getCreditScore(LoanRequest loanRequest){
Random random = new Random();
int creditScore = 700 + random.nextInt(150);
public CreditReport getCreditReport(LoanRequest loanRequest) {
int creditScore = 600 + new Random().nextInt(250);
logger.info("Credit Score: " + creditScore);
return new CreditScore(creditScore);
return new CreditReport(creditScore);
}
}

View File

@@ -1,16 +0,0 @@
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<util:map id="banks">
<entry key="abcBankChannel" value="premier" />
<entry key="efgBankChannel" value="premier" />
<entry key="hijBankChannel" value="secondary" />
<entry key="xyzBankChannel" value="secondary" />
<entry key="fooBankChannel" value="secondary" />
</util:map>
</beans>

View File

@@ -5,7 +5,6 @@
<import resource="classpath:META-INF/spring/integration/stub-services-config.xml" />
<import resource="classpath:META-INF/spring/integration/loan-broker-config.xml" />
<import resource="classpath:META-INF/spring/integration/bank-channel-mappings-config.xml" />
<import resource="classpath:META-INF/spring/integration/shark-detector-config.xml"/>
</beans>

View File

@@ -5,6 +5,5 @@
<import resource="classpath:META-INF/spring/integration/stub-services-config.xml" />
<import resource="classpath:META-INF/spring/integration/loan-broker-config.xml" />
<import resource="classpath:META-INF/spring/integration/bank-channel-mappings-config.xml" />
</beans>

View File

@@ -11,26 +11,29 @@
http://www.springframework.org/schema/integration/spring-integration.xsd">
<gateway id="loanBrokerGateway"
default-request-channel="loanBrokerPreProcessingChannel"
default-request-channel="loanRequestsChannel"
service-interface="org.springframework.integration.samples.loanbroker.LoanBrokerGateway">
<method name="getBestLoanQuote">
<header name="RESPONSE_TYPE" value="BEST"/>
</method>
</gateway>
<chain input-channel="loanBrokerPreProcessingChannel">
<chain input-channel="loanRequestsChannel">
<header-enricher>
<header name="CREDIT_SCORE" ref="creditBureau" method="getCreditScore"/>
<header name="creditScore" expression="@creditBureau.getCreditReport(payload).score"/>
</header-enricher>
<router expression="headers['CREDIT_SCORE'].score > 780
? @banks.?[value.equals('premier')].keySet()
: @banks.?[value.equals('secondary')].keySet()"
apply-sequence="true"/>
<recipient-list-router apply-sequence="true">
<recipient selector-expression="headers.creditScore > 800" channel="exclusiveBankChannel"/>
<recipient selector-expression="headers.creditScore > 750" channel="premiereBankChannel"/>
<recipient selector-expression="headers.creditScore > 700" channel="qualityBankChannel"/>
<recipient selector-expression="headers.creditScore > 650" channel="friendlyBankChannel"/>
<recipient channel="easyBankChannel"/>
</recipient-list-router>
</chain>
<!-- Messages are sent to the banks via bank channels and will be received and processed by an aggregator -->
<aggregator input-channel="quotesAggregationChannel" method="aggregateQuotes">
<aggregator input-channel="loanQuotesChannel" method="aggregateQuotes">
<beans:bean class="org.springframework.integration.samples.loanbroker.LoanQuoteAggregator"/>
</aggregator>

View File

@@ -7,7 +7,7 @@
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip-2.0.xsd">
<channel id="quotesAggregationChannel">
<channel id="loanQuotesChannel">
<interceptors>
<wire-tap channel="loanSharkDetectorChannel"/>
</interceptors>

View File

@@ -2,23 +2,32 @@
<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"
xmlns:p="http://www.springframework.org/schema/p"
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">
<bean id="creditBureau" class="org.springframework.integration.samples.loanbroker.stubs.CreditBureauStub" />
<bean id="bankStub" class="org.springframework.integration.samples.loanbroker.stubs.BankStub"/>
<!-- Bank Endpoints (to be rewired to remoting adapters) -->
<int:service-activator input-channel="abcBankChannel" output-channel="quotesAggregationChannel" ref="bankStub"/>
<int:service-activator input-channel="exclusiveBankChannel" output-channel="loanQuotesChannel">
<bean class="org.springframework.integration.samples.loanbroker.stubs.BankStub" p:name="exclusiveBank" p:baseRate="3.5"/>
</int:service-activator>
<int:service-activator input-channel="efgBankChannel" output-channel="quotesAggregationChannel" ref="bankStub"/>
<int:service-activator input-channel="premiereBankChannel" output-channel="loanQuotesChannel">
<bean class="org.springframework.integration.samples.loanbroker.stubs.BankStub" p:name="premiereBank" p:baseRate="4.0"/>
</int:service-activator>
<int:service-activator input-channel="qualityBankChannel" output-channel="loanQuotesChannel">
<bean class="org.springframework.integration.samples.loanbroker.stubs.BankStub" p:name="qualityBank" p:baseRate="4.5"/>
</int:service-activator>
<int:service-activator input-channel="hijBankChannel" output-channel="quotesAggregationChannel" ref="bankStub"/>
<int:service-activator input-channel="friendlyBankChannel" output-channel="loanQuotesChannel">
<bean class="org.springframework.integration.samples.loanbroker.stubs.BankStub" p:name="friendlyBank" p:baseRate="5.0"/>
</int:service-activator>
<int:service-activator input-channel="xyzBankChannel" output-channel="quotesAggregationChannel" ref="bankStub"/>
<int:service-activator input-channel="fooBankChannel" output-channel="quotesAggregationChannel" ref="bankStub"/>
<int:service-activator input-channel="easyBankChannel" output-channel="loanQuotesChannel">
<bean class="org.springframework.integration.samples.loanbroker.stubs.BankStub" p:name="easyBank"/>
</int:service-activator>
<!-- end Bank Endpoints -->
</beans>

View File

@@ -46,9 +46,10 @@ public class LoanBrokerDemo {
LoanRequest loanRequest = new LoanRequest();
loanRequest.setCustomer(new Customer());
LoanQuote loan = broker.getBestLoanQuote(loanRequest);
logger.info("\n********* Best Quote: " + loan);
logger.info("********* Best Quote *********\n" + loan);
System.out.println("==============================");
List<LoanQuote> loanQuotes = broker.getAllLoanQuotes(loanRequest);
logger.info("\n********* All Quotes: ");
logger.info("********* All Quotes *********");
for (LoanQuote loanQuote : loanQuotes) {
logger.info(loanQuote);
}

View File

@@ -6,7 +6,7 @@
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
<param name="ConversionPattern" value="%m%n" />
</layout>
</appender>
<logger name="org.springframework.integration">