Mule - Dynamic Path - File component - file

I use a file component in my mule flow that add the payload in that file.
The mule flow is:
HTTP Listener => Payload => File => ...
The component in XML:
<file:outbound-endpoint path="${test}" outputPattern="tested_#[function:datestamp:yyyyMMdd].log" connector-ref="File" responseTimeout="1000" doc:name="File"/>
When I generate the file, it create a folder called test in mule-esb-directory\bin but I want to create it in the project directory for example:
mule-esb-directory\app\MyMuleProject.
I use a properties file to set the path but it does not work.
Now: mule-esb-directory\bin\test
I want: mule-esb-directory\app\MymuleProject

Try using this
src/main/resources/
Or try with absolute path, like this
/opt/mule-esb-directory/app/MymuleProject

Related

Execute route only when direct component is called

I want to unzip files whenever a "direct" route is called. The fileName I am getting from some other direct route.
from("direct:unZipFile")
.from("file:C:\\MYFILES\\File\\Unzipped\\?fileName=${header.fileName}&idempotent=true")
.split(new ZipSplitter())
.streaming()
.to("file:C:\\MYFILES\\File\\Unzipped\\")
.split(body().convertToString().tokenize("\n"))
.transform()
.simple("${in.body}")
.end();
Now it is working when I call the direct component but it also keeps on scanning the directory and processes the same file. I understand that the above code allows the trigger from direct as well as file component but I just want it to execute from "direct" component and I can't remove the "file" component as from that only I am reading the file.
You can also try to use pollenrich
from("direct:unZipFile")
.pollEnrich.simple("file:C:\\MYFILES\\File\\Unzipped\\?fileName=${header.fileName}")
.split(new ZipSplitter())
.streaming()
...
You can use consumerTemplate
from("direct:unZipFile")
.process(exchange -> {
Exchange recvFiles = exchange.getContext().createConsumerTemplate().receive("file:C:\\MYFILES\\File\\Unzipped\\?fileName=${header.fileName}&idempotent=true");
exchange.getIn().setBody(recvFiles.getIn().getBody());
exchange.getIn().getHeaders().putAll(recvFiles.getIn().getHeaders());
})
.split(new ZipSplitter())
.streaming()
.to("file:C:\\MYFILES\\File\\Unzipped\\")
.split(body().convertToString().tokenize("\n"))
.transform()
.simple("${in.body}")
.end();
You can also just set the message body with a java.io.File where you compute the file name in a few lines of Java code, but yeah otherwise the pollEnrich is the EIP pattern solution for this.

Mule File Inbound - empty files are not triggered

I have a scenario wherein I need to read files from a particular folder. So I had a File inbound as below, its reading all non-empty files. But empty files are not read and sits in the same location as is.
<file:inbound-endpoint path="${file.path}" responseTimeout="10000" doc:name="File" moveToDirectory="${audit.location}">
<file:filename-regex-filter pattern="file.employee(.*).xml,file.client(.*).xml"
caseSensitive="true"/>
</file:inbound-endpoint>
I removed File filter, but still it doesn't read empty files.
Is there a way to enable file inbound to read empty files too?
According the the Mule File Connector documentation:
The File connector as inbound endpoint does not process empty (0 bytes) files.
So this behavior is expected. There is no documented way to process non empty file with the File Inbound Endpoint.
However you can still write your own connector to do this, or use a workaround such as fill your "empty" file with a single character (such as a space) to make it non-empty
If you want to read a file with the size of 0 KB, then you can`t achieve this with File Connector, but we can read a file by using MuleRequester in the flow. I will share sample snippet soon. Please let me know,If you need any help.
Regards,
Sreenivas B
Mule File connector does not process empty (0 bytes) files as inbound endpoint
As per my knowledge File Inbound connector will not process (0 KB) size files.
On the File Connector, the class org.mule.transport.file.FileMessageReceiver.java in method poll has :
if (file.length() == 0)
{
if (logger.isDebugEnabled())
{
logger.debug("Found empty file '" + file.getName() + "'. Skipping file.");
}
continue;
}
that prevents it from proccessing empty files
But you can create your own CustomFileMessageReceiver.java, create your package:
package com.mycompany.mule.transport.file;
And the class that extends AbstractPollingMessageReceiver
public class CustomFileMessageReceiver extends AbstractPollingMessageReceiver
Copy the original FileMessageReceiver.java methods but comment the above lines and change FileMessageReceiver to CustomFileMessageReceiver where needed.
The call fileConnector.move(file, workFile) is a protected method from the original package, commented and beware you cannot use workdir.
In the same package create a copy of org.mule.transport.file.ReceiverFileInputStream.java
Configure your connector:
<file:connector name="FILE" readFromDirectory="${incoming.directory}" autoDelete="true" streaming="false" recursive="true" validateConnections="true" doc:name="File" writeToDirectory="${processed.directory}">
<service-overrides messageReceiver="com.mycompany.mule.transport.file.CustomFileMessageReceiver" />
</file:connector>
Or you may implement your own file connector, as stated in the above answers.

Mule - Delete files with Files Connector

This is my main mule flow:
HTTP > Payload > File (Create) > Property (File Name) JMS
And that JMS calls to other flow that starts with JMS
JMS > JSON TO XML > File (Delete by file name)
Firstly, I create a file with the JSON message.
And finally, when JSON message is transformed to XML, I try to delete the file by name calling a property that tell me the name.
But the files doesn't delete. Its output this action by log:
Writing file to: C:\errors\91fa03a0-dd33-11e5-a0d9-408d5c4c1bf3.dat
How can I do to delete it?
I tried with the option autoDelete in the Connector Configuration but it doesn't delete.
<file:connector name="File1" outputPattern="#[message.inboundProperties.fileName]" autoDelete="true" streaming="false" validateConnections="true" doc:name="File"/>
You can't use this file outbound endpoint to delete a file.
If you would use something like S3 for example you can, but those connectors are 'operation-based'.
It's meant to write an new file or append data to an existing one.
The autoDelete option is for deleting the file once it has been read by a file inbound endpoint.
More info:
https://docs.mulesoft.com/mule-user-guide/v/3.7/file-transport-reference
If you want to delete and for your usecase you should use a piece of JAVA code and use methods from the File Class:
https://docs.oracle.com/javase/7/docs/api/java/io/File.html

Mule - how to get the name of the file created in an outbound endpoint

I have a Mule application that is writing a file in an outbound endpoint, with the config below:
<file:outbound-endpoint path="${Outbound}" outputPattern="outputFile_#[function:datestamp:yyyyMMddHHmmss].csv" doc:name="Output File"/>
Following this point in the flow I need to display a log message along the lines of "Successfully created file {filename}".
The issue I'm having is that I can't find a way of displaying the name of the file I have just created. I could put: Successfully created file outputFile_#[function:datestamp:yyyyMMddHHmmss].csv, but there is a chance that the datestamp may differ by one second.
Is there a way in Mule that I can display the name of the file I've just written?
UPDATE
Following the response from #til_b, I've achieved this using the following:
<set-variable value="outputFile_#[function:datestamp:yyyyMMddHHmmss].csv" variableName="Filename" doc:name="Variable"/>
<file:outbound-endpoint path="${Outbound}" outputPattern="#[variable:Filename]" doc:name="Output File"/>
<logger level="INFO" message="Successfully created file #[variable:Filename]" doc:name="Logger" />
I dont know about mule, but when i encounter such a problem while programming i store the generated filename in a variable, and then use that variable to actually create the file and display the message.
In Pseudocode:
var filename = #yyyymmddhhMMss.csv
create_file(filename)
log_message(filename + ' successfully created')

Is it possible to put myBatis (iBatis) xml mappers outside the project?

According to the user guide i am able to use file path instead of
resource:
// Using classpath relative resources
<mappers>
<mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
</mappers>
// Using url fully qualified paths
<mappers>
<mapper url="file:///var/sqlmaps/AuthorMapper.xml"/>
</mappers>
in my project I'm trying to put my mapper xml "outside" the project
and i'm doing this:
<mapper url="file://D:/Mappers/ComponentMapper1.xml" />
The output of my log4j console:
Error building SqlSession.
The error may exist in file://D:/Mappers/ComponentMapper1.xml
Cause: org.apache.ibatis.builder.BuilderException: Error parsing
SQL Mapper Configuration. Cause: java.net.UnknownHostException: D
Is it bug or it's me doing something wrong?
You just need an additional forward slash before the drive letter.
Sql Map Config looks for mapping files relatively to the classpath, so just try adding your ComponentMapper1.xml somewhere to the classpath.
set CLASSPATH=%CLASSPATH%;D:/Mappers/
...
<mapper resource="ComponentMapper1.xml" />
You must use
<mapper url="file:///usr/local/ComponentMapper1.xml" />
Where file:///usr/local/ComponentMapper1.xmlis the path to your XML File, instead of the resource if you want use mappers outside resource dir.

Resources