Search code examples
apache-camelapache-servicemixblueprint-osgi

How is Apache Camel's Enrich EIP Supposed to Work with File Endpoints?


I am trying to learn how to use Apache Camel by writing simple routes that use the various EIPs. My mentor has suggested Apache ServiceMix as a good server for testing simple routes, so I'm using Apache ServiceMix 5.1.0. Currently, I'm trying to make a route that reads a request from a file, replaces the contents of the request with a response from a second file, and writes that response to a third file.

Ideally, the second file wouldn't be deleted; I would eventually want to use this pattern in other self-tutorial routes, and the second file would mimic the response of a webservice. However, I'm just trying to get this to work right now, so I'm not complicating things with options like noop.

If I've read the Apache Camel Wiki page for the Content Enricher correctly, omitting the aggregation strategy from an enrich EIP will make Camel discard the contents of the message and replace it with the body obtained from the resource. So, I thought this Camel Route would do what I want:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:camel="http://camel.apache.org/schema/blueprint" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
       http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

    <camelContext xmlns="http://camel.apache.org/schema/blueprint">
        <route>
            <from uri="file:camel/inMessage/?fileName=replaceMe.txt" />
            <enrich uri="file:camel/storedResponse/?fileName=withThis.txt" />
            <to uri="file:camel/outMessage/?fileName=output.txt" />
        </route>
    </camelContext>

</blueprint>

What actually happens is that the contents of camel/storedResponse/withThis.txt is replaced with the contents of camel/inMessage/replaceMe.txt. The file camel/outmessage/output.txt is created, but it contains the contents of replaceMe.txt, not withThis.txt.

Since I'm still new to Camel, I assume the problem is just me misreading something in the documentation, or overlooking an obvious configuration problem.

For what it's worth, here are the contents of the files before starting the route.

replaceMe.txt
This is the message sent. It should not appear in the response.

withThis.txt
This is the file stored in the server. The response should contain this text.

output.txt does not yet exist.

Here are the contents of the files after the route has completed.

replaceMe.txt has been deleted.

withThis.txt
This is the message sent. It should not appear in the response.

output.txt
This is the message sent. It should not appear in the response.

Thank you for your time.


Solution

  • From the Camel doc:

    enrich uses a Producer to obtain the additional data. It is usually used for Request Reply messaging, for instance to invoke an external web service. pollEnrich on the other hand uses a Polling Consumer to obtain the additional data. It is usually used for Event Message messaging, for instance to read a file or download a FTP file.

    If using enrich, then the content of withThis.txt is replaced with the content of replaceMe.txt, and this is not what you want. Use pollEnrich instead:

     <pollEnrich uri="file:camel/storedResponse/?fileName=withThis.txt" />