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.ExchangePattern;
025    import org.apache.camel.Processor;
026    import org.apache.camel.ProducerTemplate;
027    import org.apache.camel.builder.RouteBuilder;
028    import org.apache.camel.component.jms.JmsComponent;
029    import org.apache.camel.impl.DefaultCamelContext;
030    //START SNIPPET: client
031    public class Client extends RouteBuilder {
032    
033        public static void main(String args[]) throws Exception {
034            CamelContext context = new DefaultCamelContext();
035            // Set up the ActiveMQ JMS Components
036            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:51616");
037            // Note we can explicit name of the component
038            context.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
039    
040            context.addRoutes(new Client());
041    
042            ProducerTemplate template = context.createProducerTemplate();
043    
044            context.start();
045    
046            // send out the request message
047            for (int i = 0; i < 2; i++) {
048                template.sendBodyAndHeader("jms:queue:loanRequestQueue",
049                                           "Quote for the lowerst rate of loaning bank",
050                                           Constants.PROPERTY_SSN, "Client-A" + i);
051                Thread.sleep(100);
052            }
053            // wait for the response
054            Thread.sleep(2000);
055            
056            // send the request and get the response from the same queue       
057            Exchange exchange = template.send("jms:queue2:parallelLoanRequestQueue", new Processor() {
058                public void process(Exchange exchange) throws Exception {
059                    exchange.setPattern(ExchangePattern.InOut);
060                    exchange.getIn().setBody("Quote for the lowerst rate of loaning bank");
061                    exchange.getIn().setHeader(Constants.PROPERTY_SSN, "Client-B");
062                }
063            });
064            
065            String bank = (String)exchange.getOut().getHeader(Constants.PROPERTY_BANK);
066            Double rate = (Double)exchange.getOut().getHeader(Constants.PROPERTY_RATE);
067            String ssn = (String)exchange.getOut().getHeader(Constants.PROPERTY_SSN);
068            System.out.println("Loan quotion for Client " + ssn + "."
069                               + " The lowest rate bank is " + bank + ", the rate is " + rate);
070            
071            // Wait a while before stop the context
072            Thread.sleep(1000 * 5);
073            context.stop();
074    
075        }
076    
077        /**
078         * Lets configure the Camel routing rules using Java code to pull the response message
079         */
080        public void configure() {
081    
082            from("jms:queue:loanReplyQueue").process(new Processor() {
083    
084                public void process(Exchange exchange) throws Exception {
085                    // Print out the response message
086                    System.out.println(exchange.getIn().getBody());
087    
088                }
089    
090            });
091    
092        }
093    
094    }
095    // END SNIPPET: client