以编程方式配置Apache Camel camel-config.xml?

时间:2014-04-29 07:18:41

标签: java apache-camel applicationcontext

我正在使用Apache Camel来构建代理。我能够运行提供的样本。但是我必须手动配置camel-config.xml文件。我想知道我们是否可以通过编程方式配置此文件以在运行时托管/配置代理?

这是原始的camel-config.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--
    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.
-->

<!-- START SNIPPET: e1 -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:cxf="http://camel.apache.org/schema/cxf"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
       http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd">

  <!-- spring property placeholder, ignore resource not found as the file resource is for unit testing -->
  <context:property-placeholder location="classpath:incident.properties,file:target/custom.properties"
                                ignore-resource-not-found="true"/>

  <!-- Use a bean to start and stop the real web service (is not Camel specific) -->
  <!-- In a real use-case the real web service would be probably located on another server
       but we simulate this in the same JVM -->
  <bean id="realWebService" class="org.apache.camel.example.cxf.proxy.RealWebServiceBean"
        init-method="start" destroy-method="stop">
    <!-- url of the real web service we have proxied -->
    <property name="url" value="http://localhost:${real.port}/real-webservice"/>
  </bean>

  <!-- bean that enriches the SOAP request -->
  <bean id="enrichBean" class="org.apache.camel.example.cxf.proxy.EnrichBean"/>

  <!-- this is the CXF web service we use as the front end -->
  <cxf:cxfEndpoint id="reportIncident"
                   address="http://localhost:${proxy.port}/camel-example-cxf-proxy/webservices/incident"
                   endpointName="s:ReportIncidentEndpoint"
                   serviceName="s:ReportIncidentEndpointService"
                   wsdlURL="etc/report_incident.wsdl"
                   xmlns:s="http://reportincident.example.camel.apache.org"/>

  <!-- this is the Camel route which proxies the real web service and forwards SOAP requests to it -->
  <camelContext xmlns="http://camel.apache.org/schema/spring">

    <!-- property which contains port number -->
    <propertyPlaceholder id="properties" location="classpath:incident.properties,file:target/custom.properties"/>

    <endpoint id="callRealWebService" uri="http://localhost:${real.port}/real-webservice?throwExceptionOnFailure=false"/>

    <route>
      <!-- CXF consumer using MESSAGE format -->
      <from uri="cxf:bean:reportIncident?dataFormat=MESSAGE"/>
      <!-- log input received -->
      <to uri="log:input"/>
      <!-- enrich the input by ensure the incidentId parameter is set -->
      <to uri="bean:enrichBean"/>
      <!-- send proxied request to real web service -->
      <to ref="callRealWebService"/>
      <!-- log answer from real web service -->
      <to uri="log:output"/>
    </route>

  </camelContext>

</beans>
<!-- END SNIPPET: e1 -->

此camel-config文件用于设置ApplicationContextUri,如下所示:

Main main = new Main();
main.setApplicationContextUri("/ApacheCamelTest2/camel-config.xml");
main.start();

如何在运行时以编程方式配置camel-config.xml?

1 个答案:

答案 0 :(得分:0)

使用DSL并在运行时创建它们。 https://camel.apache.org/java-dsl.html 这样您就可以完全控制路径,并且可以在运行时以编程方式添加/修改/删除它们。