MuleSoft file connector and properties in Mule 4

anil kumar
6 min readAug 19, 2020

The File Connector is capable of reading and writing data to a local network. This can be implemented either as an inbound endpoint or as an outbound endpoint. It can only be employed as a one-way template.

● Read files at all times, and erase, transfer, or remove the file while it is being read.

● Add the output to already existing files.

● Drag files from one directory into the next.

● Read files for data, and save files for backup.

● Create files with names specific to them.

An Inbound Endpoint device connection

File Connector will serve as an inbound endpoint if you place the endpoint of the file at flow initialization. After a certain interval of time, it reads and processes the directory for new files. It uses streaming, by default. Input File Stream is Payload.

● Place the endpoint of the file in the flow source section.

● After some time interval, it will search for a file in the directory. This can be configured to your specific needs.

● When a file is received it triggers the flow.

● It reads the file into the payload and sends in the flow message to the next processor.

Message source

The path is a directory of destinations on a connected file system. This is basically the location on a connected system where the file will be placed in Flow to be read by Message Source.

When we need to back up or move a file to any other directory, moving to that directory is used. You can specify the path you need to back up or move your input file in here.

Move to the pattern is used when moving a file with the property Move to Directory. For instance, if we’re using

# [message.inboundProperties.originalFilename].backup pattern, you’re adding the.backup to Original File Name. If your name for the file is student.csv, a new FileName student.csv.backup will transfer the file to the directory.

The frequency of polling specifies how frequently the endpoint should check for incoming files. The default value for this is 1000ms.

A filename regex filter is used to create a database to restrict the processed data. For eg, if you set the.*csv template, the inbound endpoint will only select the file with the.csv extension and ignore the rest of the file like.txt,.xlsx, etc.

File Connector as an Exit point

File Connector serves as an outbound endpoint if you place a build block at the middle or end of the Mule 4 flow (basically in the Message Processor Region), transferring the file to the linked file system.

Place the File Connector to serve as an Outbound Endpoint in the Flow message processor.

It can create new files, or add the message to the existing file.

You can set File Name to runtime.

Path

The path is the destination directory on the linked file system where we have to create new files or add data to existing files.

Using filename/pattern to define a filename or pattern for naming files that are sent to the linked file system from the File Endpoint. If not set, it will use the same pattern of file-name used for incoming files by default.

<? Version xml=”1.0 “=” UTF-8? >

< Mool

xmlns: file=”http:/www.mulesoft.org / file / schema /

xmlns=”http:/www.mulesoft.org / Mule 4 / core

xmlns: doc=”http:/www.mulesoft.org / schema / Mule 4 / docs

xmlns: spring=”http:/www.springframework.org

Instance xmlns: xsi=”http:/www.w3.org/2001 / XMLSchema

xsi: schemaLocation=’http:/www.springframework.org / schema / beans http:/www.springfratruct.org/schema/beans/spring-beans-current.xsd

http:/www.mulesoft.org/schema/Mule 4/core http:/www.mulesoft.org/schema/kernel/Mule 4.xsd

http:/www.mulesoft.org/schema/Mule 4/file http:/www.mulesoft.org/scheme/Mule 4/file/current/Mule 4-file.xsd “> >

< flow name = “demoFlow file connector” >

<file:inbound-endpoint path=’src/test/resources/input’ moveToPattern=’#[message.inboundProperties.originalFilename].backup’ moveToDirectory=’src/test/resources/backup’ responseTimeout=’10000" doc:name=’file’>

< file: pattern file name-regex-filter=.”*csv “caseSensitive=”true”/ >

< /file: enter-endpoint >

Level < logger=”INFO “doc: name=”Logger”/ >

Path=’src / test / resources / output ‘responseTimeout=’10000' doc: name=’File’/ >

< /drive >

I hope this article will help you understand how to use File Connector as both the inbound and outbound endpoints.

One specific feature we used to read a properties file in Mule 4 flows in our implementation of APIs is.

In Mule 4 certification flows there is a lot of documentation showing users various ways to read a properties script. Three strategies on how you can do this are here.

● Reading a properties file with the expression ${Key}

● Using a properties file to read! Expression: [p[‘Key’]]

I’ll present the first two approaches in this article.

Approach # 1: Reading a Properties File using the expression ${Key}

The most efficient way to read from a flow a Mule 4 properties file is by using the expression ${Key}.

This technique would work well for reading data from the properties file of a Mule 4 training program. Here we must first store the data in the properties file of the Mule 4 app, as shown below, and then use the “Key” to read the value.

< br/ > < br / > >

Form Policy = AUTO < br/ >

Directives.code = COMAUTO < br/ >

< set-variable Name=”PoliciesType “value=”${policies.type}”-doc: name=”variable”/><br/ >

Form Policy = AUTO

Software Policy = COMAUTO

< set — variable Name=”Type policy “value=”${policies.type}” doc: name=”Variable”/ >

This method would also work for reading a file of custom properties, protected from a Mule 4 flow as background property placeholder. Here, we first keep the properties file under src / main / resource in the project folder, and then define the placeholder property using the placeholder context property, as shown below.

We’ll then store the data and read the file value of the properties using “Key,” similar to the way we did it above.

< br/ > < br / > >

< context: the position of the property-placeholder=” config.properties”/><br/ >

< context: placeholder = “config.properties”/ >

The ${key} expression can be used to read Mule 4 properties files from activities such as Variable, Groovy, Java, etc. (Mule 4 App properties and custom properties described using context). The big benefit of this strategy is that it works well.

It also works well for reading the latest file of properties, particularly when the data in the file of properties sometimes changes. But this method can not be used directly from DataWeave to read a properties script. In order to access a DataWeave property value using ${key}, we must first store the value in some flow variable, as mentioned above, and then use this flow variable in DataWeave for transformation.

Approach # 2: Reading a List of Properties using a! Expression: [p[‘Key’]].

Now let’s get on with the second approach. This approach works to read a value from a Mule 4 properties file (with Mule 4 app properties and custom properties placed under src / main / resources) which is specified using a placeholder expression property, as mentioned below.

< br/ > < br / > >

< expression language: position of the property-placeholder = “Mule 4-app.properties”/><br/ >

< expression-language: location of the property-placeholder = “config.properties”/><br/ >

< expression — language: property — location of the placeholder=’Mule 4-app.properties’/ >

< expression — language: property — location of the placeholder=’config.properties’/ >

First, with the above expression, we must define the property placeholder, and then store the data in the properties file as shown below. We’re going to use the ! [P[‘Key’] expression for the value reading.

< br/ > < br / > >

Host = abc.com < br/>api.host

About api.port=80 < br/ >

< http: config-request name=”HTTP Request Configuration “host=”! [‘api.host’]”port=! [p[‘api.port’]]” doc:name=”Configuration of HTTP requests”/><br/>

Email < logger = “value:-! [p[‘api.host’]’niveau=”INFO “doc: name=”Logger”/><br/ >

With api.host = abc.com

Around api.port=80

< http: request — config name=”host=”HTTP Request Configuration! [‘api.host’]”port=! [p[‘api.port’]” doc:name=”Configuration of HTTP requests”/>

Email < logger = “value:-! [p[‘api.host’]niveau=”INFO “doc: name=”Logger”/ >

The! [p[‘Key’] expression can be used to read from activities such as HTTP listeners/requesters, WS consumers, loggers, etc. the Mule 4 properties file. The main advantage of this approach is that performance works great and it also allows users to read the latest properties file. As with the above approach, this approach does not work directly from DataWeave for reading a properties file.

Ultimately the two approaches above are the best ways to read the data in Mule 4 flows from a properties file. The only drawback is that it prevents users from reading the file value of a Mule 4 DataWeave transformation directly from a property.

Conclusion

I hope you reach a conclusion about Mule 4 file properties and connectors. You can learn more from Mulesoft training in Hyderabad.

--

--