com.dbs.posttrade.camel.client.CamelClientRemoting.java Source code

Java tutorial

Introduction

Here is the source code for com.dbs.posttrade.camel.client.CamelClientRemoting.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
 * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
 * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
 * applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.dbs.posttrade.camel.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dbs.posttrade.domain.ReportType;
import com.dbs.posttrade.repository.support.GenericRepositoryInterface;

/**
 * Client that uses Camel Spring Remoting for very easy integration with the server.
 * <p/>
 * Requires that the JMS broker is running, as well as CamelServer
 */
public final class CamelClientRemoting {

    private static final Logger _log = LoggerFactory.getLogger(CamelClientRemoting.class);

    private CamelClientRemoting() {
        // Helper class
    }

    // START SNIPPET: e1
    public static void main(final String[] args) throws Exception {
        _log.debug("Notice this client requires that the CamelServer is already running!");

        ApplicationContext context = new ClassPathXmlApplicationContext(
                "spring/camel-applicationContext-client.xml");

        GenericRepositoryInterface<ReportType, Long> genericRepositoryInterface = context
                .getBean("reportTypeRepository", GenericRepositoryInterface.class);

        ReportType a = genericRepositoryInterface.getById(1L);
        System.out.println("... the result is: " + a);

        System.exit(0);
    }
    // END SNIPPET: e1

    /*
     * public static void anotherEndPointCall(){ } public static void endPointCall2() throws Exception{
     * AbstractApplicationContext context = new
     * ClassPathXmlApplicationContext("spring/camel-applicationContext-client.xml" ); CamelContext camelContext =
     * context.getBean("camel-client", CamelContext.class); //Endpoint endpoint =
     * camel.getEndpoint("jms:queue:document.queue"); MethodCaller caller=new MethodCaller();
     * caller.setMethodContaingClass(Multiplier.class); Class [] type =new Class[]{Integer.TYPE,Integer.TYPE};
     * caller.setArgumentType(type); caller.setMethodToBeCalled("multiply"); caller.setReturnType(Integer.TYPE);
     * caller.setRoutingSlip(RouteConstant.R_MULTI); PostTradeEndPoint postTrade=new PostTradeEndPoint(camelContext,
     * "jms:queue:document.queue"); int response = postTrade.process(caller, Integer.TYPE);
     * System.out.println("... the result is: " + response); } public static void endPointCall() throws Exception{
     * AbstractApplicationContext context = new
     * ClassPathXmlApplicationContext("spring/camel-applicationContext-client.xml" ); CamelContext camel =
     * context.getBean("camel-client", CamelContext.class); // get the endpoint from the camel context Endpoint endpoint
     * = camel.getEndpoint("jms:queue:document.queue2"); // create the exchange used for the communication // we use the
     * in out pattern for a synchronized exchange where we expect a response Exchange exchange =
     * endpoint.createExchange(ExchangePattern.InOut); // set the input on the in body // must be correct type to match
     * the expected type of an Integer object Integer[] args = new Integer[] {11,22}; Class [] type =new
     * Class[]{Integer.TYPE,Integer.TYPE}; BeanInvocation invocation=new BeanInvocation();
     * invocation.setMethod(Multiplier.class.getMethod("multiply", type)); invocation.setArgs(args);
     * exchange.getIn().setHeader("Slip", RouteConstant.R_MULTI); exchange.getIn().setBody(invocation); // to send the
     * exchange we need an producer to do it for us Producer producer = endpoint.createProducer(); // start the producer
     * so it can operate producer.start(); // let the producer process the exchange where it does all the work in this
     * oneline of code System.out.println("Invoking the multiply with 11"); producer.process(exchange); // get the
     * response from the out body and cast it to an integer int response = exchange.getOut().getBody(Integer.class);
     * System.out.println("... the result is: " + response); // stopping the JMS producer has the side effect of the
     * "ReplyTo Queue" being properly // closed, making this client not to try any further reads for the replies from
     * the server producer.stop(); // we're done so let's properly close the application context //
     * IOHelper.close(context); }
     */

}