2

I have an application written in camel 2.10.6 which uses the EIP aggregator. It receives as input 2 files: one ended with -information and the other ended with -report. I have to process both files (separately) and after processing I have to combine (i.e. aggregate) both together.

For achieving this I am using the EIP aggregator. I have defined the routes using spring DSL. I do NOT have a correlation expression. That is, my expression is the constant true.

        <route>
        <from uri="activemq:assemblingAll" />
        <log message="\n---------->>> @ASSEMBLEALL" />

        <aggregate strategyRef="myAggregator" completionSize="2">
            <correlationExpression>
                <constant>true</constant>
            </correlationExpression>

            <to uri="activemq:toBeValidated" />
        </aggregate>
    </route>

I would like to correlate the files which belong together, that is, files with the same prefix (prefix is the string before -information (resp. .report). I believe that this would e also possible by setting some ID in the header.

My problem is that I don't know how to do this. Up to now all my attempts have generated org.apache.camel.CamelExchangeException: Invalid correlation key.

Any hint?

Thanks in advance.

My routes (simplified) looks as follows:

<camelContext xmlns="http://camel.apache.org/schema/spring">

    <!-- switchOnFilename" -->
    <route>
        <from uri="file:/inputdir" />

        <choice>
            <when>
                <simple>${header.CamelFileName} regex '.*-information$'</simple>
                <log message="\n---------->>> -information" />
                <to uri="activemq:information" />
            </when>

            <otherwise>
                <!-- The default case -->
                <log message="\n---------->>> DEFAULT CASE (*-report)" />
                <to uri="activemq:report" />
            </otherwise>
        </choice>
    </route>

    <route>
        <from uri="activemq:information" />
        <bean ref="myExtractInformation" />
        <to uri="xslt:ExtractInformation.xsl" />                    
        <to uri="activemq:assemblingAll" />
    </route>

    <route>
        <from uri="activemq:report" />
        <bean ref="myExtractReports" />         
        <to uri="xslt:extractReports.xsl" />
        <to uri="activemq:assemblingAll" />
    </route>

    <route>
        <from uri="activemq:assemblingAll" />
        <log message="\n---------->>> @ASSEMBLEALL" />
        <aggregate strategyRef="myAggregator" completionSize="2">
            <correlationExpression>
                <constant>true</constant>
            </correlationExpression>

            <to uri="activemq:toBeValidated" />
        </aggregate>
    </route>

    <route>
        <from uri="activemq:toBeValidated" />

        <doTry> 
            <to uri="validator:validator.xsd" />
            <to uri="activemq:insertUpdateDB" />
            <doCatch>
                <exception>org.apache.camel.ValidationException</exception>
                <log message="INVALID " />

                <to uri="mock:invalid" />
            </doCatch>
            <doFinally>
                <log message="Finalizer...." />
                <to uri="mock:finally" />
                <to uri="mock:invalid" />
            </doFinally>
        </doTry>
    </route>

    <route>
        <from uri="activemq:insertUpdateDB" />
        <setHeader headerName="CamelHttpMethod">
            <constant>PUT</constant>
        </setHeader>
        <setHeader headerName="Content-Type" inheritErrorHandler="true" id="setHeader3">
            <constant>application/xml</constant>
        </setHeader>
        <setHeader headerName="Content-Encoding">
            <constant>UTF-8</constant>
        </setHeader>
        <to uri="http4://localhost:8181/rest/insertUpdateDB" />
    </route>
Luixv
  • 8,590
  • 21
  • 84
  • 121

1 Answers1

1

Perhaps use a bean reference as a correlation expression.

<correlationExpression>
    <method ref="dataCorrelationExpression" method="getPrefixString" />
</correlationExpression>

Then write a bean to do it, like so

public class DataCorrelationExpression {
    public String getPrefixString(@Header("CamelFileName") String filename) throws Exception {
        String[] parts = filename.split("\\.");
        if (parts.length != 2) {
            throw new Exception("Oh no!");
        }
        return parts[0];
    }
}

Make sure the bean is instantiated in your Spring context.

Tim T
  • 304
  • 2
  • 9