ApacheCamel—我使用SpringXML定义我的camel上下文。是否可以在运行时在Java中获得相同的camelcontext对象?

ApacheCamel—我使用SpringXML定义我的camel上下文。是否可以在运行时在Java中获得相同的camelcontext对象?,java,spring,apache-camel,Java,Spring,Apache Camel,我使用SpringXML定义骆驼上下文。是否可以在运行时在Java中获得相同的camelcontext对象?我需要此对象添加更多数据并将其发送到现有路由。我无法在XML中执行此操作,需要对象,因为我将侦听错误,并且必须在触发事件侦听器时执行操作 <?xml version="1.0" encoding="UTF-8"?> <!-- Configures the Camel Context--> <camelContext id="ovcOutboundCam

我使用SpringXML定义骆驼上下文。是否可以在运行时在Java中获得相同的camelcontext对象?我需要此对象添加更多数据并将其发送到现有路由。我无法在XML中执行此操作,需要对象,因为我将侦听错误,并且必须在触发事件侦听器时执行操作

<?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>

您是否考虑过从spring应用程序上下文中获取名为“ovcOutboundCamelContext”的Bean

虽然我认为你的问题可以更容易解决:

在camel的文档中,我们有:

CamelContextAware 如果您想在POJO中注入CamelContext,只需实现CamelContextAware接口;然后,当Spring创建POJO时,上下文将被注入到POJO中。关于进一步的注入,请参见Bean集成

您可以在Spring中创建一个实现CamelContextAware的bean,如下所示:

    @Service
    public class RouteManager implements CamelContextAware { 

    protected CamelContext camelContext;

    public CamelContext getCamelContext() {
     return camelContext;
    }

    public void setCamelContext(CamelContext camelContext) {
     this.camelContext = camelContext;
    }
}
如果不使用注释,则可以使用:

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


获取上下文后,您可以使用代码,但无需启动或停止上下文。

谢谢Stefan。我相信这也应该很好,但我发现上面的方法更好。如果我在应用程序中有多个camel上下文,哪一个将CamelContextAware注入?
<bean id="RouteManager " class="myPackage.RouteManager" />