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.queue.version;
018
019 import javax.jms.ConnectionFactory;
020
021 import org.apache.activemq.ActiveMQConnectionFactory;
022 import org.apache.camel.CamelContext;
023 import org.apache.camel.Exchange;
024 import org.apache.camel.builder.RouteBuilder;
025 import org.apache.camel.component.jms.JmsComponent;
026 import org.apache.camel.impl.DefaultCamelContext;
027
028 /**
029 * The LoanBroker is a RouteBuilder which builds the whole loan message routing rules
030 *
031 * @version $Revision: 16095 $
032 */
033 public class LoanBroker extends RouteBuilder {
034
035 /**
036 * A main() so we can easily run these routing rules in our IDE
037 */
038 // START SNIPPET: starting
039 public static void main(String... args) throws Exception {
040
041 CamelContext context = new DefaultCamelContext();
042 JmsBroker broker = new JmsBroker();
043 broker.start();
044 // Set up the ActiveMQ JMS Components
045 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:51616");
046
047 // Note we can explicitly name the component
048 context.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
049
050 context.addRoutes(new LoanBroker());
051 // Start the loan broker
052 context.start();
053 System.out.println("Server is ready");
054
055 Thread.sleep(5 * 60 * 1000);
056 context.stop();
057 Thread.sleep(1000);
058 broker.stop();
059
060 }
061 // END SNIPPET: starting
062
063 /**
064 * Lets configure the Camel routing rules using Java code...
065 */
066 public void configure() {
067 // START SNIPPET: dsl
068 // Put the message from loanRequestQueue to the creditRequestQueue
069 from("jms:queue:loanRequestQueue").to("jms:queue:creditRequestQueue");
070
071 // Now we can let the CreditAgency process the request, then the message will be put into creditResponseQueue
072 from("jms:queue:creditRequestQueue").process(new CreditAgency()).to("jms:queue:creditResponseQueue");
073
074 // Here we use the multicast pattern to send the message to three different bank queues
075 from("jms:queue:creditResponseQueue").multicast().to("jms:queue:bank1", "jms:queue:bank2", "jms:queue:bank3");
076
077 // Each bank processor will process the message and put the response message into the bankReplyQueue
078 from("jms:queue:bank1").process(new Bank("bank1")).to("jms:queue:bankReplyQueue");
079 from("jms:queue:bank2").process(new Bank("bank2")).to("jms:queue:bankReplyQueue");
080 from("jms:queue:bank3").process(new Bank("bank3")).to("jms:queue:bankReplyQueue");
081
082 // Now we aggregate the response message by using the Constants.PROPERTY_SSN header.
083 // The aggregation will be complete when all the three bank responses are received
084 // In Camel 2.0 the we use AGGERATED_SIZE instead of AGGERATED_COUNT as the header
085 // name of the aggregated message size.
086 from("jms:queue:bankReplyQueue")
087 .aggregate(header(Constants.PROPERTY_SSN), new BankResponseAggregationStrategy())
088 .completionPredicate(header(Exchange.AGGREGATED_SIZE).isEqualTo(3))
089
090 // Here we do some translation and put the message back to loanReplyQueue
091 .process(new Translator()).to("jms:queue:loanReplyQueue");
092
093 // END SNIPPET: dsl
094
095 // START SNIPPET: dsl-2
096 // CreditAgency will get the request from parallelLoanRequestQueue
097 from("jms:queue2:parallelLoanRequestQueue").process(new CreditAgency())
098 // Set the aggregation strategy for aggregating the out message
099 .multicast(new BankResponseAggregationStrategy())
100 // Send out the request to three different banks in parallel
101 .parallelProcessing().to("jms:queue2:bank1", "jms:queue2:bank2", "jms:queue2:bank3");
102
103 // Each bank processor will process the message and put the response message back
104 from("jms:queue2:bank1").process(new Bank("bank1"));
105 from("jms:queue2:bank2").process(new Bank("bank2"));
106 from("jms:queue2:bank3").process(new Bank("bank3"));
107 // END SNIPPET: dsl-2
108 }
109 }