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 co
The cleanest solution seems to be using the dedicated getResource()
method as described in the
MUnit documentation.
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()
Use the set-payload
message processor and a MEL expression:
<set-payload value="#[Thread.currentThread().getContextClassLoader().getResourceAsStream('my-file.abc')]" />
<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>
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
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();
}