3
votes

I use Spring XML to define my Camel Context. Is it possible to get the same camelcontext object in Java at runtime? I need this object to add more data and send it to an existing route. I cannot do it in XML and need the object as I will be listening for errors and have to act when the event listener is triggered.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Configures the Camel Context-->

   <camelContext id="ovcOutboundCamelContext" 
                 errorHandlerRef="deadLetterErrorHandler" 
                 xmlns="http://camel.apache.org/schema/spring" 
                 useMDCLogging="true">

       <route id="processAuctionMessageRoute">
            <from uri="direct:processAuctionMessage"/>
            <to uri="bean:CustomerService?method=processAuctionMessage"/>
            <to uri="direct:splitMessage"/>
        </route>

        <route id="splitMessagesRoute">
        <from uri="direct:splitMessage"/>
        <split parallelProcessing="true" executorServiceRef="auctionsSplitThreadPoolProfile" id="splitId">
            <simple>${body}</simple>
            <to uri="bean:CustomerService?method=transformCompanyMessageForAuction"/>
            <to uri="bean:CustomerService?method=processCompanyMessageForAuction"/>
        </split>
        <to uri="direct:end"/>           
    </route>

    <route id="endProcessor">
        <from uri="direct:end"/>
        <log loggingLevel="INFO" message="End of route ${threadName}"/>
    </route>
</camelContext>

I am trying to get this context with the already existing routes in Java, but it did not work. Please help.

public  void test() throws Exception {      
    CamelContext context = new DefaultCamelContext();
    context.start();
    System.out.println("context:" + context.getRoutes().size());
    context.getRoute("direct:gotoExistingProcess");     
    addRoutesToCamelContext(context);
    System.out.println("context:" + context.getRoutes().size());
    context.stop();
}
2

2 Answers

3
votes

In the documentation of camel, we have this:

CamelContextAware If you want to be injected with the CamelContext in your POJO just implement the CamelContextAware interface; then when Spring creates your POJO the CamelContext will be injected into your POJO. Also see the Bean Integration for further injections.

You can create a bean in Spring that implements CamelContextAware, something like this:

    @Service
    public class RouteManager implements CamelContextAware { 

    protected CamelContext camelContext;

    public CamelContext getCamelContext() {
     return camelContext;
    }

    public void setCamelContext(CamelContext camelContext) {
     this.camelContext = camelContext;
    }
}

If you don't use the annotation you can use:

<bean id="RouteManager " class="myPackage.RouteManager" />

After get the context you can user your code, but you dont have to start or stop the context.

0
votes

Have you considered to get the Bean named "ovcOutboundCamelContext" from the spring application context.

Though I think your problem could be solved easier: http://camel.apache.org/loading-routes-from-xml-files.html