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.example.client;
018
019 import org.apache.camel.CamelContext;
020 import org.apache.camel.Endpoint;
021 import org.apache.camel.Exchange;
022 import org.apache.camel.ExchangePattern;
023 import org.apache.camel.Producer;
024 import org.springframework.context.ApplicationContext;
025 import org.springframework.context.support.ClassPathXmlApplicationContext;
026
027 /**
028 * Client that uses the <a href="http://camel.apache.org/message-endpoint.html">Mesage Endpoint</a>
029 * pattern to easily exchange messages with the Server.
030 * <p/>
031 * Notice this very same API can use for all components in Camel, so if we were using TCP communication instead
032 * of JMS messaging we could just use <code>camel.getEndpoint("mina:tcp://someserver:port")</code>.
033 * <p/>
034 * Requires that the JMS broker is running, as well as CamelServer
035 */
036 public final class CamelClientEndpoint {
037 private CamelClientEndpoint() {
038 //Helper class
039 }
040
041 // START SNIPPET: e1
042 public static void main(final String[] args) throws Exception {
043 System.out.println("Notice this client requires that the CamelServer is already running!");
044
045 ApplicationContext context = new ClassPathXmlApplicationContext("camel-client.xml");
046 CamelContext camel = (CamelContext) context.getBean("camel-client");
047
048 // get the endpoint from the camel context
049 Endpoint endpoint = camel.getEndpoint("jms:queue:numbers");
050
051 // create the exchange used for the communication
052 // we use the in out pattern for a synchronized exchange where we expect a response
053 Exchange exchange = endpoint.createExchange(ExchangePattern.InOut);
054 // set the input on the in body
055 // must you correct type to match the expected type of an Integer object
056 exchange.getIn().setBody(11);
057
058 // to send the exchange we need an producer to do it for us
059 Producer producer = endpoint.createProducer();
060 // start the producer so it can operate
061 producer.start();
062
063 // let the producer process the exchange where it does all the work in this oneline of code
064 System.out.println("Invoking the multiply with 11");
065 producer.process(exchange);
066
067 // get the response from the out body and cast it to an integer
068 int response = exchange.getOut().getBody(Integer.class);
069 System.out.println("... the result is: " + response);
070
071 // stop and exit the client
072 producer.stop();
073 System.exit(0);
074 }
075 // END SNIPPET: e1
076
077 }