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.
8 Answers
Use the set-payload
message processor and a MEL expression:
<set-payload value="#[Thread.currentThread().getContextClassLoader().getResourceAsStream('my-file.abc')]" />

- 33,403
- 4
- 38
- 72
-
2Actually...this seems to set the payload to be an InputStream. Is there an easy way to make it a String? – Jeff Nov 03 '12 at 00:21
-
4Flank it by an `object-to-string-transformer`. – David Dossot Nov 03 '12 at 01:00
-
6I was also able to do this...
– Jeff Nov 04 '12 at 02:00
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.
-
1I also was not having success with David Dossot's answer. Also, this answer is cleaner and more readable. It should really be the accepted answer. – rhuffstedtler Nov 07 '14 at 16:49
As of Mule 3.4, use the parse-template transformer
<parse-template location="my-resurce.txt" doc:name="Load resource as a String"/>
This will make things MUCH easier.
You may need to still set the mime-type though depending on how you are going to use the template.
Common errors include using the full path in the transformer like c:\users\myusers\mule\myfile.txt That won't compile well.
You can also use Mule Expression in the parse template and Rich text for example.
http://www.mulesoft.org/documentation/display/current/Parse+Template+Reference

- 96
- 1
- 3
The cleanest solution seems to be using the dedicated getResource()
method as described in the
MUnit documentation.

- 4,254
- 1
- 29
- 46
<scripting:component doc:name="Get xls File">
<scripting:script engine="Groovy"><![CDATA[new File('C:/project/src/main/resources/account.xls').getText('UTF-8')]]></scripting:script>
</scripting:component>

- 5,918
- 10
- 53
- 90
<parse-template location="my-resurce.txt" doc:name="Load resource as a String"/>
The file can perfectly contain no MEL expression whatsoever, in which case it's actually loaded as it is, and it becomes the payload.

- 341
- 5
- 18
I am trying to throw Java class transformer so you can use the following
note : the path is the direct package that contain the file you want to read am store the path inside mule variable and then read the file content and stor its value into Payload
.
public class PayloadFileReader extends AbstractMessageTransformer {
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
String result = "";
try {
result = readFileTest(message.getInvocationProperty("path")
.toString());
} catch (Exception e) {
e.printStackTrace();
}
message.setPayload(result);
return message;
}
public String readFileTest(String path) throws FileNotFoundException,
IOException, Exception {
ClassLoader classLoader = getClass().getClassLoader();
"+classLoader.getResource("samples/v3/addVal-request.sample").getFile());
File file = new File(classLoader.getResource(path).getFile());
FileReader fileReader = new FileReader(file);
BufferedReader bufferReader = null;
StringBuilder stringBuffer = new StringBuilder();
String line;
try {
bufferReader = new BufferedReader(fileReader);
while ((line = bufferReader.readLine()) != null) {
stringBuffer.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferReader != null) {
try {
bufferReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stringBuffer.toString();
}

- 2,297
- 5
- 29
- 43

- 261
- 1
- 9
You can create a "Spring Bean" which tries to "import" the file (Suppose Mule XML conefiguration file). Sample code below:
<spring:beans>
<spring:import resource="classpath:sample-mule-integration.xml"/>
</spring:beans>
Or else you can try by placing the file in a property placeholder as below
<context:property-placeholder location="file:E:\NewMuleWorkSpace\springbeanproperties\src\main\resources\property.properties"/>
Or else you can even read the file by using a Groovy component with sample code:
File file = new File("C://Users//schiraboina//Desktop//123.txt")
payload=file.getText()

- 127
- 1
- 7