Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mule read single file from classpath during flow

Tags:

mule

Is there an easy way to configure a Flow to read a single file from the classpath one time? I don't need to poll for a file. I just need to read a known file and set its contents as the message payload.

like image 272
Jeff Avatar asked Nov 02 '12 21:11

Jeff


People also ask

How do I read a file during a flow in mule?

Mule supports reading files at the beginning of a flow and we can use the File Connector to do this as well, but this connector is not suitable for loading the content of a file in the middle of a flow. Sometimes, though, you need to read a file during a flow.

How to process files on a strict schedule in mule?

You have to process files on a strict schedule, not as soon as they arrive. You have to read the file name from the DB and then you have to start processing. You need to retry file processing from the beginning of the file. To achieve this goal we need to use the mule-module-requester.

What does the mule module requester do?

This allows you to get data in the middle of your flow from endpoints that can usually only start a flow (like a file receiver). The intention of this post is to describe, step-by-step, how to install and use the Mule module requester. We will see a working example of this as well.

Does mulestudio include the /classes directory within the app?

When I run my application from MuleStudio and dump the java.class.path the /classes directory within the app is included. When I run it on my Linux server it is not. This is causing problems with my application accessing my resource files.


2 Answers

Use the set-payload message processor and a MEL expression:

<set-payload value="#[Thread.currentThread().getContextClassLoader().getResourceAsStream('my-file.abc')]" />
like image 131
David Dossot Avatar answered Oct 11 '22 09:10

David Dossot


For some reason I cannot make the solution proposed by David Dossot to work. I was inspired by this answer and came up with another solution

<spring:bean id="myResource" class="org.apache.commons.io.IOUtils" factory-method="toString">
    <spring:constructor-arg value="classpath:path/to/myResource.txt" type="java.io.InputStream"/>
</spring:bean>

and then you can use set-payload in the following way

<set-payload value="#[app.registry.myResource]" doc:name="Set Payload"/>

which will result in setting the payload with the content of the file as a String.

This method has the advantage that the content of the resource file is loaded only once into a bean of type String. So if your set-payload statement is executed frequently, this could improve performance.

like image 24
Guido Avatar answered Oct 11 '22 11:10

Guido