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 java.util.concurrent.CountDownLatch;
020 import java.util.concurrent.ExecutorService;
021 import java.util.concurrent.Executors;
022 import java.util.concurrent.TimeUnit;
023
024 import org.apache.camel.ProducerTemplate;
025 import org.springframework.context.ApplicationContext;
026 import org.springframework.context.support.ClassPathXmlApplicationContext;
027
028 /**
029 * Client that uses the {@link ProducerTemplate} to easily exchange messages with the Server.
030 * <p/>
031 * Requires that the JMS broker is running, as well as CamelServer
032 */
033 public final class CamelClient {
034
035 private static final int SIZE = 10000;
036 private static final int POOL = 100;
037
038 private CamelClient() {
039 // Helper class
040 }
041
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
047 // get the camel template for Spring template style sending of messages (= producer)
048 final ProducerTemplate producer = (ProducerTemplate) context.getBean("camelTemplate");
049
050 // now send a lot of messages
051 System.out.println("Sending ...");
052
053 final CountDownLatch latch = new CountDownLatch(POOL);
054
055 ExecutorService executors = Executors.newFixedThreadPool(POOL);
056 for (int i = 0; i < POOL; i++) {
057 final Integer idx = i;
058 executors.execute(new Runnable() {
059 public void run() {
060 try {
061 for (int j = 0; j < SIZE / POOL; j++) {
062 producer.sendBody("jms:queue:inbox", "Message " + idx.intValue() * j + j);
063 }
064 } finally {
065 latch.countDown();
066 }
067 }
068 });
069 }
070
071 latch.await(300, TimeUnit.SECONDS);
072 System.out.println("... Send " + SIZE + " message to JMS broker");
073
074 System.exit(0);
075 }
076
077 }