How to Read a File and Copy File Using Mulesoft
Read a File Using the File Connector
Using Anypoint Connector for File (File Connector), the transport can read a file at any point in the flow, rather than only every bit a result of entering endpoint polling in a Mule iii implementation.
The merely required parameter for the Read
operation is the path to the file to be read. The path can exist either the full path to the file or a relative path. The path uses the working directory value divers in the configuration referenced by the config-ref
parameter. In Anypoint Studio, y'all tin can fix the path in the File path field. In XML, use the path aspect in the <file:read
statement to specify the path.
If no configuration is referenced, the working directory defaults to the value of the user.abode
system property. If the organisation holding is not fix, the connector fails to initialize.
This example reads a file using a file configuration and a relative path:
<file:read config-ref="File_Config" path="relativePath"/>
This example reads a file using the total path:
<file:read path="fullPath"/>
In Studio, click the browse button (…) to locate the file, for example:
/Users/me/myfile
In XML, utilize the path attribute to specify the path to the file, for example:
<file:read path="/Users/me/myfile" />
Render Information
The Read
operation returns a message with:
-
The file's content as the payload
-
The file's metadata in the message attributes, metadata (such as name, cosmos time, size, and so on) in the message attributes.
If the file does non be, the Read
operation throws a FILE:ILLEGAL_PATH
error. Note that the operation does non read directories.
The |
Configure a Relative Path
If numerous Read
operations (or whatever other file operations) in the same Mule app share part of their path, their base path can exist extracted into a file configuration for meliorate reuse.
For example, to access two files whose full paths are /Users/Documents/Examples/transmission.txt
and /Users/Documents/Examples/readme.txt
, y'all can define the part of the path that is the aforementioned in a configuration, for case:
<file:config name="File_Config" > <file:connection workingDir="/Users/Documents/Examples" /> </file:config>
The resulting Read
operations that reference the configuration would exist:
<menstruum> <file:read config-ref="File_Config" path="manual.txt" /> <file:read config-ref="File_Config" path="readme.txt" /> </menses>
MIME Types and Encoding
Each connector tries to decide a file's MIME type from its extension. You lot can forcefulness the MIME blazon to a different value with the MIME Type (outputMimeType
) parameter.
The same process works for encoding. Past default, the connector assumes that the default encoding in the Mule runtime matches the MIME type of the file, withal, y'all can gear up the encoding through the Encoding (outputEncoding
) parameter.
Why MIME Types Affair
DataWeave expressions are embeddable inside operations that generate payloads and other values. Having the correct MIME type prepare helps DataWeave auto-assign types and too generate the correct outputs. Also, maximizing the use of DataSense'south functionality improves the user experience.
Access Files Defined in a Mule Awarding
While developing Mule apps, you lot can salve folders and files in the resource
folder. When the app is deployed, those folders and files are copied to the directory divers by the ${app.home}
property.
For example, if you want to define a folder named MyFiles
in the resources
folder, and a file named Example.txt
within of the MyFiles
folder, employ this project structure:
In the runtime, the path to access the file is: ${app.habitation}/MyFiles/Example.txt
To read the file, define the following configuration:
<file:config name="File_Config" > <file:connexion workingDir="${app.dwelling}" /> </file:config>
Configure the Read
operation similar this:
<file:read config-ref="File_Config" path="MyFiles/Example.txt" />
As mentioned before, each time the awarding is deployed, the contents from the src/main/resources folder are copied to the ${app.home} directory each fourth dimension the application is deployed, any changes made to those files during runtime are lost in one case the application is stopped or redeployed. For this reason, it is recommended that you put merely the files needed for reading during the application'southward execution in the resource folder. |
File Locking
Yous can set the lock
parameter to true
to foreclose other processes from accessing a file while it is beingness read. However, because information technology is provided past the host file organization, the lock's behavior might change depending on the mounted drive and the operating arrangement on which Mule is running. You must have that into consideration before relying on this lock.
If the file is already locked by someone else, the connector will non be able to lock it, and the operation throws a FILE:FILE_LOCK
error.
The lock is automatically released when ane of the post-obit occurs:
-
The Mule flow that locked the file ends.
-
The file's content is fully read.
The following is an instance of locking:
<file:read config-ref="File_Config" path="relativePath" lock="true" />
Modify the Output MIME Blazon and Encoding
Y'all tin use the outputEncoding
and outputMimeType
parameters to alter the MIME type or the encoding of the payload that the Read
operation outputs. This does not transform the file's content, information technology just overrides the MIME type or encoding information, for instance:
<file:read config-ref="File_Config" path="relativePath" outputEncoding="UTF-8" outputMimeType="application/csv; headers=imitation" />
Time Betwixt Size Checks Parameter
If you lot desire to read the file contents returned past the Read
operation, but you are not certain if the file is nevertheless beingness written to, you tin can utilise the TimeBetweenSizeChecks
parameter, which works with the TimeBetweenSizeCheckUnit
parameter. Combined, these two parameters perform two size checks:
-
One when the menses reaches a signal where the stream is virtually to be consumed. The fourth dimension cheque is not done in the
Read
operation becauseRead
returns a lazy stream pointing to the file'due south content, so it makes sense to do the size check just when the stream is about to exist consumed. -
Another check is performed after the wait time set by the
TimeBetweenSizeChecks
andTimeBetweenSizeCheckUnit
parameters
If both size checks return the same file size, and then the stream is consumed successfully. If the two size checks return different file sizes, so the file is nonetheless being written to, so the stream is not consumed and the functioning fails with an error.
You tin can use the Read
operation with size cheque parameters and then consume the output stream in a Logger component.
<file:read config-ref="File_Config" path="filePath" timeBetweenSizeCheck="xv" timeBetweenSizeCheckUnit="SECONDS"> <logger level="INFO" bulletin="#[payload]"/> </file:read>
Note that the 15 seconds check happens when the logger
component executes, not at the Read
operation.
Repeatable Streams
The List performance makes use of the repeatable streams functionality introduced in Mule 4. The operation returns a list of messages, where each bulletin represents a file in the list and holds a stream to the file. A stream is repeatable by default.
rodriguezwairespleet.blogspot.com
Source: https://docs.mulesoft.com/file-connector/1.3/file-read
0 Response to "How to Read a File and Copy File Using Mulesoft"
إرسال تعليق