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.ProducerTemplate;
020    import org.apache.camel.component.mock.MockEndpoint;
021    import org.apache.camel.example.reportincident.InputReportIncident;
022    import org.springframework.context.ApplicationContext;
023    import org.springframework.context.support.ClassPathXmlApplicationContext;
024    
025    /**
026     * Client that uses the {@link ProducerTemplate} to easily exchange messages with the Server.
027     * <p/>
028     * Requires that the CamelServer is already running
029     */
030    public final class CamelClient {
031    
032        private static final int SIZE = 100;
033    
034        private CamelClient() {
035            // Helper class
036        }
037    
038        public static void main(final String[] args) throws Exception {
039            System.out.println("Notice this client requires that the CamelServer is already running!");
040    
041            ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/camel-client.xml");
042    
043            // get the camel template for Spring template style sending of messages (= producer)
044            final ProducerTemplate producer = (ProducerTemplate) context.getBean("camelTemplate");
045    
046            final MockEndpoint mock = (MockEndpoint) context.getBean("result");
047            mock.expectedMessageCount(SIZE);
048            // expect no duplicate bodies
049            mock.expectsNoDuplicates().body();
050    
051            // now send a lot of messages
052            System.out.println("Sending ...");
053            for (int i = 0; i < SIZE; i++) {
054                InputReportIncident input = new InputReportIncident();
055                input.setIncidentId("" + i);
056                input.setIncidentDate("20091116");
057                input.setGivenName("Claus");
058                input.setFamilyName("Ibsen");
059                input.setSummary("Camel rocks");
060                input.setDetails("More bla");
061                input.setEmail("davsclaus@apache.org");
062                input.setPhone("55512345678");
063    
064                // send our input to the Camel route
065                producer.sendBody("direct:start", input);
066            }
067            System.out.println("... Send done");
068    
069            System.out.println("Asserting ...");
070            mock.assertIsSatisfied();
071            System.out.println("... Asserting done");
072    
073            System.exit(0);
074        }
075    
076    }