简体   繁体   中英

Camel Interceptor per Endpoint, not per CamelContext

Is it a possible to declare Interceptor per Endpoint, not per CamelContext? In my case I've lots of CamelContexts and I want to extract common code with endpoints declarations and interceptors to one file: camel-common-context.xml. There is absolutely no problem with extracting endpoints, but how to extract interceptors? Is it a possible or they just should be inside every CamelContext where I want to use an endpoint? In my case they're not visible in related camel contexts after modification which I posted below:

my camel-common-context.xml:

<camelContext id="esb" xmlns="http://camel.apache.org/schema/spring">
        <endpoint id="externalSystem" uri="${external.system.url}?throwExceptionOnFailure=false" />

        <interceptFrom>
            <convertBodyTo type="java.lang.String" />
            <when>
                <xpath>not(/soapenv:Envelope)</xpath>
                <process ref="soapFaultProcessor" />
            </when>
            <otherwise>
                <convertBodyTo type="java.lang.String" />
            </otherwise>
        </interceptFrom>

        <interceptSendToEndpoint uri="ref:externalSystem">
            <setHeader headerName="SYSTEM">
                <constant>EXTERNAL</constant>
            </setHeader>
            <convertBodyTo type="java.lang.String" />
            <to uri="log:wSSProcessor_external_system_request?level=DEBUG" />
        </interceptSendToEndpoint>
</camelContext>

Other camel contexts (I'm only importing resource):

<import resource="camel-common-context.xml" />

I've no problems when I'm declaring each endpoint with interceptor in every camel-context, but it's a wrong pattern with lots of copy-paste.

In this question I see that there is an information that " I dont think this is possible using InterceptorStrategy since that expects it is running in the same camel context". What's more I think that using multiple contexts with one application is a bad pattern


UPDATE

I was trying to change my architecture to one Camel Context with more Route Contexts follow up @Lukasz N answer, but I hit a snag CamelBugWithRouteContexts . It looks quite impossible to resolve my problem but it should works well on versions of camel higher than 2.8.

Write a processor in Java DSL or Spring DSL and include in every intercept,

<camel:interceptFrom>
    <camel:process ref="InterceptProcessor"></camel:process>
</camel:interceptFrom>

Where the processor will do logging or any other you require.

You can write one camel-context with endpoints, interceptors declarations etc. and multiple route-context and import them in your camel-context.

check how import routes to camel-context

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM