通过蓝图覆盖camel http4组件中的默认属性

时间:2016-07-18 13:34:59

标签: apache-camel

我想通过蓝图更改camel http4组件中的maxTotalConnectionsconnectionsPerRoute

有人可以告诉我是否可以这样做,或者我必须将其作为URI选项发送?

我在骆驼2.16.3

1 个答案:

答案 0 :(得分:1)

Camel确实让你setup routes using OSGi Blueprint。它还允许您使用Property Placeholders with Blueprint values。但是您需要将这些值放在URI中。

所以,你可以使用类似的东西:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
    <cm:property-placeholder persistent-id="my-placeholders" update-strategy="reload">
      <cm:default-properties>
          <cm:property name="maxTotalConnections" value="200"/>
          <cm:property name="connectionsPerRoute" value="20"/>
      </cm:default-properties>
    </cm:property-placeholder>

    <camelContext xmlns="http://camel.apache.org/schema/blueprint">
        <route>
            <from uri="timer:test" />
            <to uri="http4:localhost:80/resource?maxTotalConnections={{maxTotalConnections}}&amp;connectionsPerRoute={{connectionsPerRoute}}" />
        </route>
    </camelContext>

</blueprint>

使用该组件创建第一个路径时,将设置组件选项。查看代码,在creating a new endpoint时设置maxTotalConnections和connectionsPerRoute:

HttpClientConnectionManager localConnectionManager = clientConnectionManager;
if (localConnectionManager == null) {
    // need to check the parameters of maxTotalConnections and connectionsPerRoute
    int maxTotalConnections = getAndRemoveParameter(parameters, "maxTotalConnections", int.class, 0);
    int connectionsPerRoute = getAndRemoveParameter(parameters, "connectionsPerRoute", int.class, 0);
    localConnectionManager = createConnectionManager(createConnectionRegistry(x509HostnameVerifier, sslContextParameters), maxTotalConnections, connectionsPerRoute);
}

设置第一个路由后,将设置clientConnectionManager。对于在第一个之后设置的任何其他路由,因为clientConnectionManager绑定到Http4组件的单个实例,所以看起来这些选项被忽略。我会在所有路线上使用相同的组件选项。

您可以通过创建新bean并为其提供id来实例化新的HTTP4组件。您可以使用它来创建具有不同组件选项的多个http4组件。

<bean id="http4-foo" class="org.apache.camel.component.http4.HttpComponent"/>
<bean id="http4-bar" class="org.apache.camel.component.http4.HttpComponent"/>

然后在设置端点时使用这些ID。

<to uri="http4-foo:localhost:80/resource?maxTotalConnections=300"/>