Skip to content

Commit

Permalink
simplifying Loan Broker sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
markfisher committed Oct 4, 2010
1 parent 1a9ad90 commit 2d730d4
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 87 deletions.
1 change: 1 addition & 0 deletions applications/loan-broker/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target
.settings
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@
/**
* @author Oleg Zhurakousky
*/
public class CreditScore {
public class CreditReport {

private final int score;
private String creditHistory;
public CreditScore(int score){
private volatile String history;

public CreditReport(int score) {
this.score = score;
}
public String getCreditHistory() {
return creditHistory;

public int getScore() {
return this.score;
}

public void setCreditHistory(String creditHistory) {
this.creditHistory = creditHistory;
public void setHistory(String history) {
this.history = history;
}

public int getScore() {
return score;
public String getHistory() {
return this.history;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.integration.samples.loanbroker.domain;

import java.text.DecimalFormat;
import java.util.Date;

/**
Expand Down Expand Up @@ -79,17 +78,6 @@ public void setRate(float rate) {
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;
Expand All @@ -100,4 +88,8 @@ else if (this.rate < other.rate) {
return 0;
}

public String toString() {
return this.lender + ":\t" + this.rate;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
*/
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
Expand All @@ -38,10 +50,10 @@ public LoanQuote quote(LoanRequest loanRequest) {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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;

/**
Expand All @@ -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);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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="efgBankChannel" 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="hijBankChannel" 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="xyzBankChannel" 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="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>
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ public void runDemo() {
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);
}
Expand Down
2 changes: 1 addition & 1 deletion applications/loan-broker/src/test/resources/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down

0 comments on commit 2d730d4

Please sign in to comment.