Author: buildbot Date: Tue Jun 5 16:18:29 2012 New Revision: 820361 Log: Production update by buildbot for camel
Modified: websites/production/camel/content/cache/main.pageCache websites/production/camel/content/loan-broker-example.html Modified: websites/production/camel/content/cache/main.pageCache ============================================================================== Binary files - no diff available. Modified: websites/production/camel/content/loan-broker-example.html ============================================================================== --- websites/production/camel/content/loan-broker-example.html (original) +++ websites/production/camel/content/loan-broker-example.html Tue Jun 5 16:18:29 2012 @@ -83,7 +83,7 @@ The JMS version which leverages the message queue to connect the credit agency and bank loan quote processors together, it just uses the InOnly exchange pattern to handle the message asynchronously; <br clear="none"> the webservice version which shows how to integrate the credit agency and bank web services together by using the InOut exchange pattern synchronously.</p> -<h2><a shape="rect" name="LoanBrokerExample-Implementationwithmessagequeue%28JMS%29"></a>Implementation with message queue (JMS)</h2> +<h2><a shape="rect" name="LoanBrokerExample-Examplewithmessagequeue%28JMS%29"></a>Example with message queue (JMS)</h2> <p>The <a shape="rect" class="external-link" href="https://svn.apache.org/repos/asf/camel/trunk/examples/camel-example-loan-broker/src/main/java/org/apache/camel/loanbroker/queue/version">queue version of loan broker</a> is based on the camel-jms component, and it shows how to using the message queue to connect the different service models (such as the credit agency , and banks). </p> @@ -98,114 +98,8 @@ mvn exec:java -PQueue.Client <p>To stop the example hit ctrl + c</p> -<p>let's take a look how this service modules are put together.</p> -<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20</span> </div> - -<p>The CreditAgency , Bank and Translator are all the implementation of Processor interface. We implement the business logical in the void process(Exchange exchange) method.</p> - -<p>CreditAgency</p> - -<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20</span> </div> - -<p>Bank</p> - -<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20</span> </div> - -<p>Translator</p> - -<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20</span> </div> - - -<p>You may found we set a custom aggregation strategy to find out the lowest loan rate from bank response message.</p> - -<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> -<pre class="code-java"><span class="code-keyword">public</span> class BankResponseAggregationStrategy <span class="code-keyword">implements</span> AggregationStrategy { - - @Override - <span class="code-keyword">public</span> Exchange aggregate(Exchange oldExchange, Exchange newExchange) { - <span class="code-comment">// the first time we only have the <span class="code-keyword">new</span> exchange -</span> <span class="code-keyword">if</span> (oldExchange == <span class="code-keyword">null</span>) { - <span class="code-keyword">return</span> newExchange; - } - - <span class="code-object">Double</span> oldQuote = oldExchange.getIn().getHeader(Constants.PROPERTY_RATE, <span class="code-object">Double</span>.class); - <span class="code-object">Double</span> newQuote = newExchange.getIn().getHeader(Constants.PROPERTY_RATE, <span class="code-object">Double</span>.class); - - <span class="code-comment">// <span class="code-keyword">return</span> the winner with the lowest rate -</span> <span class="code-keyword">if</span> (oldQuote.doubleValue() <= newQuote.doubleValue()) { - <span class="code-keyword">return</span> oldExchange; - } <span class="code-keyword">else</span> { - <span class="code-keyword">return</span> newExchange; - } - } - -} -</pre> -</div></div> - -<p>We start the loan broker after we start up the ActiveMq broker and the connection factory of Camel-JMS component.</p> - -<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> -<pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">static</span> void main(<span class="code-object">String</span>... args) <span class="code-keyword">throws</span> Exception { - <span class="code-comment">// setup an embedded JMS broker -</span> JmsBroker broker = <span class="code-keyword">new</span> JmsBroker(); - broker.start(); - - <span class="code-comment">// create a camel context -</span> CamelContext context = <span class="code-keyword">new</span> DefaultCamelContext(); - - <span class="code-comment">// Set up the ActiveMQ JMS Components -</span> ConnectionFactory connectionFactory = <span class="code-keyword">new</span> ActiveMQConnectionFactory(<span class="code-quote">"tcp:<span class="code-comment">//localhost:51616"</span>); -</span> <span class="code-comment">// Note we can explicitly name the component -</span> context.addComponent(<span class="code-quote">"jms"</span>, JmsComponent.jmsComponentAutoAcknowledge(connectionFactory)); - - <span class="code-comment">// add the route -</span> context.addRoutes(<span class="code-keyword">new</span> LoanBrokerRoute()); - - <span class="code-comment">// start Camel -</span> context.start(); - <span class="code-object">System</span>.out.println(<span class="code-quote">"Server is ready"</span>); - - <span class="code-comment">// let it run <span class="code-keyword">for</span> 5 minutes before shutting down -</span> <span class="code-object">Thread</span>.sleep(5 * 60 * 1000); - context.stop(); - <span class="code-object">Thread</span>.sleep(1000); - broker.stop(); -} -</pre> -</div></div> - -<p>Now we can send the request from client and pull the response message back</p> - -<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> -<pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">final</span> class Client { - - <span class="code-keyword">private</span> Client() { - } - - <span class="code-keyword">public</span> <span class="code-keyword">static</span> void main(<span class="code-object">String</span> args[]) <span class="code-keyword">throws</span> Exception { - CamelContext context = <span class="code-keyword">new</span> DefaultCamelContext(); - <span class="code-comment">// Set up the ActiveMQ JMS Components -</span> ConnectionFactory connectionFactory = <span class="code-keyword">new</span> ActiveMQConnectionFactory(<span class="code-quote">"tcp:<span class="code-comment">//localhost:51616"</span>); -</span> <span class="code-comment">// Note we can explicit name of the component -</span> context.addComponent(<span class="code-quote">"jms"</span>, JmsComponent.jmsComponentAutoAcknowledge(connectionFactory)); - context.start(); - - ProducerTemplate template = context.createProducerTemplate(); - - <span class="code-object">String</span> out = template.requestBodyAndHeader(<span class="code-quote">"jms:queue:loan"</span>, <span class="code-keyword">null</span>, Constants.PROPERTY_SSN, <span class="code-quote">"Client-A"</span>, <span class="code-object">String</span>.class); - <span class="code-object">System</span>.out.println(out); - - template.stop(); - context.stop(); - } - -} -</pre> -</div></div> - -<h2><a shape="rect" name="LoanBrokerExample-Implementationwithwebservice"></a>Implementation with web service</h2> +<h2><a shape="rect" name="LoanBrokerExample-Examplewithwebservice"></a>Example with web service</h2> <p>The <a shape="rect" class="external-link" href="https://svn.apache.org/repos/asf/camel/trunk/examples/camel-example-loan-broker/src/main/java/org/apache/camel/loanbroker/webservie/version">web service version of loan broker</a> is based on the camel-cxf component which can produce and consume the SOAP message on the wire. It uses the InOut Message exchange pattern, when the client send out the message to the router , it can get the response message back from the same endpoint.<br clear="none"> When we send out the quote message to the three different banks, we could choice to call the bank service one by one or send out the message parallelly(one request thread per request).<br clear="none"> You can compare the response time after you run the sample.</p> @@ -219,150 +113,7 @@ mvn exec:java -PWS.Client </pre> </div></div> -<p>To stop the example hit ctrl + c</p> - -<p>First, let's go through the SEI (Service Endpoint Interface) for LoanBroker, CreditAgency and Bank.</p> - -<p>LoanBroker</p> - -<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> -<pre class="code-java"> -<span class="code-comment">// This SEI has no @WebService annotation, we use the simple frontend API to create client and server -</span><span class="code-keyword">public</span> <span class="code-keyword">interface</span> LoanBrokerWS { - - <span class="code-object">String</span> getLoanQuote(<span class="code-object">String</span> ssn, <span class="code-object">Double</span> loanAmount, <span class="code-object">Integer</span> loanDuration); - -} -</pre> -</div></div> - -<p>CreditAgency</p> - -<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> -<pre class="code-java">@WebService -<span class="code-keyword">public</span> <span class="code-keyword">interface</span> CreditAgencyWS { - - <span class="code-object">int</span> getCreditScore(<span class="code-object">String</span> ssn); - - <span class="code-object">int</span> getCreditHistoryLength(<span class="code-object">String</span> ssn); - -} -</pre> -</div></div> - -<p>Bank</p> - -<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> -<pre class="code-java"><span class="code-comment">// Since we use @WebServices here, please make sure to use JaxWs frontend API create the client and server -</span>@WebService -<span class="code-keyword">public</span> <span class="code-keyword">interface</span> BankWS { - - <span class="code-object">String</span> getBankName(); - - BankQuote getQuote(<span class="code-object">String</span> ssn, <span class="code-object">double</span> loanAmount, <span class="code-object">int</span> loanDuration, <span class="code-object">int</span> creditHistory, <span class="code-object">int</span> creditScore); - -} -</pre> -</div></div> - -<p>Here are two routing rules in DSL , one is for routing the request to bank sequentially, the other is for calling the bank service parallely.</p> - -<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20</span> </div> - -<p>We use the CreditScoreProcessor to send two request to credit agency to get the credit history length and the credit score and prepare the request message for the bank.</p> - -<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20</span> </div> - -<p>Now we implement the Bank and CreditAgency SEI with the business logical codes.</p> - -<p>Bank</p> - -<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> -<pre class="code-java"><span class="code-keyword">public</span> class Bank <span class="code-keyword">implements</span> BankWS { - <span class="code-keyword">private</span> <span class="code-object">String</span> bankName; - <span class="code-keyword">private</span> <span class="code-object">double</span> primeRate; - - <span class="code-keyword">public</span> Bank(<span class="code-object">String</span> name) { - bankName = name; - primeRate = 3.5; - } - - <span class="code-keyword">public</span> <span class="code-object">String</span> getBankName() { - <span class="code-keyword">return</span> bankName; - } - - <span class="code-keyword">public</span> BankQuote getQuote(<span class="code-object">String</span> ssn, <span class="code-object">double</span> loanAmount, <span class="code-object">int</span> loanDuration, <span class="code-object">int</span> creditHistory, <span class="code-object">int</span> creditScore) { - <span class="code-object">Double</span> rate = primeRate + (<span class="code-object">double</span>)(loanDuration / 12) / 10 + <span class="code-object">Math</span>.random() * 10 / 10; - <span class="code-comment">// Wait <span class="code-keyword">for</span> a <span class="code-keyword">while</span> -</span> <span class="code-keyword">try</span> { - <span class="code-object">Thread</span>.sleep(1000); - } <span class="code-keyword">catch</span> (InterruptedException e) { - <span class="code-comment">// <span class="code-keyword">do</span> nothing here -</span> } - BankQuote result = <span class="code-keyword">new</span> BankQuote(bankName, ssn, rate); - <span class="code-keyword">return</span> result; - } - -} -</pre> -</div></div> - -<p>CreditAgency</p> - -<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> -<pre class="code-java"><span class="code-keyword">public</span> class CreditAgency <span class="code-keyword">implements</span> CreditAgencyWS { - - <span class="code-keyword">public</span> <span class="code-object">int</span> getCreditHistoryLength(<span class="code-object">String</span> ssn) { - <span class="code-object">int</span> creditScore = (<span class="code-object">int</span>)(<span class="code-object">Math</span>.random() * 600 + 300); - <span class="code-keyword">return</span> creditScore; - } - - <span class="code-keyword">public</span> <span class="code-object">int</span> getCreditScore(<span class="code-object">String</span> ssn) { - <span class="code-object">int</span> creditHistoryLength = (<span class="code-object">int</span>)(<span class="code-object">Math</span>.random() * 19 + 1); - <span class="code-keyword">return</span> creditHistoryLength; - } - -} -</pre> -</div></div> - -<p>The below codes show how the start the loan broker.</p> - -<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20</span> </div> - -<p>We can send the request by creating a client proxy with the LoanBroker SEI in the client code. BTW, you can compare the two different routing rule's performance by running the client.</p> - -<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> -<pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">final</span> class Client { - - <span class="code-keyword">private</span> <span class="code-keyword">static</span> <span class="code-object">String</span> url = <span class="code-quote">"http:<span class="code-comment">//localhost:9008/loanBroker"</span>; -</span> - <span class="code-keyword">private</span> Client() { - } - - <span class="code-keyword">public</span> <span class="code-keyword">static</span> LoanBrokerWS getProxy(<span class="code-object">String</span> address) { - <span class="code-comment">// Now we use the simple front API to create the client proxy -</span> ClientProxyFactoryBean proxyFactory = <span class="code-keyword">new</span> ClientProxyFactoryBean(); - ClientFactoryBean clientBean = proxyFactory.getClientFactoryBean(); - clientBean.setAddress(address); - clientBean.setServiceClass(LoanBrokerWS.class); - clientBean.setBus(BusFactory.getDefaultBus()); - <span class="code-keyword">return</span> (LoanBrokerWS) proxyFactory.create(); - } - - <span class="code-keyword">public</span> <span class="code-keyword">static</span> void main(<span class="code-object">String</span>[] args) { - LoanBrokerWS loanBroker = getProxy(url); - - StopWatch watch = <span class="code-keyword">new</span> StopWatch(); - <span class="code-object">String</span> result = loanBroker.getLoanQuote(<span class="code-quote">"SSN"</span>, 5000.00, 24); - - <span class="code-object">System</span>.out.println(<span class="code-quote">"Took "</span> + watch.stop() + <span class="code-quote">" milliseconds to call the loan broker service"</span>); - <span class="code-object">System</span>.out.println(result); - } - -} -</pre> -</div></div></div> +<p>To stop the example hit ctrl + c</p></div> </td> <td valign="top"> <div class="navigation">