001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.loanbroker.webservice.version;
018    
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    import org.apache.camel.CamelContext;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.Message;
026    import org.apache.camel.Processor;
027    import org.apache.camel.builder.RouteBuilder;
028    import org.apache.camel.component.cxf.CxfConstants;
029    import org.apache.camel.impl.DefaultCamelContext;
030    import org.apache.camel.loanbroker.webservice.version.bank.BankServer;
031    import org.apache.camel.loanbroker.webservice.version.credit.CreditAgencyServer;
032    import org.apache.camel.loanbroker.webservice.version.credit.CreditAgencyWS;
033    import org.apache.cxf.BusFactory;
034    import org.apache.cxf.frontend.ClientFactoryBean;
035    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
036    
037    
038    
039    /**
040     * The LoanBroker is a RouteBuilder which builds the whole loan message routing rules
041     *
042     *
043     */
044    public class LoanBroker extends RouteBuilder {
045    
046        //START SNIPPET: server
047        public static void main(String... args) throws Exception {
048            CamelContext context = new DefaultCamelContext();
049            CreditAgencyServer creditAgencyServer = new CreditAgencyServer();
050            // Start the credit server
051            creditAgencyServer.start();
052    
053            // Start the bank server
054            BankServer bankServer = new BankServer();
055            bankServer.start();
056    
057            // Start the camel context
058            context.addRoutes(new LoanBroker());
059            context.start();
060    
061            // Start the loan broker
062            Thread.sleep(5 * 60 * 1000);
063            context.stop();
064            Thread.sleep(1000);
065            bankServer.stop();
066            creditAgencyServer.stop();
067        }
068        //END SNIPPET: server
069        /**
070         * Lets configure the Camel routing rules using Java code...
071         */
072        public void configure() {
073        // START SNIPPET: dsl
074    
075            // Router 1 to call the bank endpoints sequentially
076            from(Constants.LOANBROKER_URI)
077                // Using the CreditScoreProcessor to call the credit agency service
078                .process(new CreditScoreProcessor(Constants.CREDITAGENCY_ADDRESS))
079                    // Set the aggregation strategy on the multicast pattern
080                    .multicast(new BankResponseAggregationStrategy())
081                        // Send out the request to three different banks sequentially
082                        .to(Constants.BANK1_URI, Constants.BANK2_URI, Constants.BANK3_URI);
083    
084            // Router 2 to call the bank endpoints in parallel
085            from(Constants.PARALLEL_LOANBROKER_URI)
086                .process(new CreditScoreProcessor(Constants.CREDITAGENCY_ADDRESS))
087                    // Using the thread pool to send out messages to three different banks in parallel                
088                    .multicast(new BankResponseAggregationStrategy())
089                        // Camel will create a thread pool with the default size (10) 
090                        // for sending the message in parallel
091                        .parallelProcessing()
092                        .to(Constants.BANK1_URI, Constants.BANK2_URI, Constants.BANK3_URI);
093    
094        //END SNIPPET: dsl
095        }
096    
097        //START SNIPPET: credit
098        class CreditScoreProcessor implements Processor {
099            private String creditAgencyAddress;
100            private CreditAgencyWS proxy;
101    
102            public CreditScoreProcessor(String address) {
103                creditAgencyAddress = address;
104                proxy = getProxy();
105            }
106    
107            private CreditAgencyWS getProxy() {
108                // Here we use JaxWs front end to create the proxy
109                JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
110                ClientFactoryBean clientBean = proxyFactory.getClientFactoryBean();
111                clientBean.setAddress(creditAgencyAddress);
112                clientBean.setServiceClass(CreditAgencyWS.class);
113                clientBean.setBus(BusFactory.getDefaultBus());
114                return (CreditAgencyWS)proxyFactory.create();
115            }
116    
117            @SuppressWarnings("unchecked")
118            public void process(Exchange exchange) throws Exception {
119                Message requestMessage = exchange.getIn();
120                List<Object> request = (List<Object>) requestMessage.getBody();
121    
122                String ssn = (String)request.get(0);
123                Double amount = (Double) request.get(1);
124                Integer loanDuriation = (Integer)request.get(2);
125                int historyLength = proxy.getCreditHistoryLength(ssn);
126                int score = proxy.getCreditScore(ssn);
127    
128                // create the invocation message for Bank client
129                List<Object> bankRequest = new ArrayList<Object>();
130                bankRequest.add(ssn);
131                bankRequest.add(amount);
132                bankRequest.add(loanDuriation);
133                bankRequest.add(historyLength);
134                bankRequest.add(score);
135                exchange.getOut().setBody(bankRequest);
136                exchange.getOut().setHeader(CxfConstants.OPERATION_NAME, "getQuote");
137            }
138    
139        }
140        //END SNIPPET: credit
141    
142    }