How to set system modified date on file in Mule - file

I have a requirement like transferring a file from a inbound directory to a outbound directory using file connector in Mule. While transferring the file it is processed in a working directory configured in the input file connector.
Now, my requirement is , if I place an old file in the file input directory, the file in the working directory should have the current time stamp on the system date modified.
It is something similar like "Touch" command used in Unix to set the system modified date.
Please not I don't want to use any Groovy Script method or any other hack method that can affect the performance in order to achieve this.
Following is my Mule mflow:-
<file:connector name="File" autoDelete="true" streaming="true" validateConnections="true" doc:name="File" outputAppend="true"/>
<file:connector name="File1" autoDelete="false" streaming="false" validateConnections="true" doc:name="File"/>
<flow name="FileReadandDeleteFlow1" doc:name="FileReadandDeleteFlow1">
<file:inbound-endpoint responseTimeout="10000" doc:name="File" connector-ref="File" moveToDirectory="E:\backup\test_workingDir" path="E:\backup\test" moveToPattern="processingFile.xml">
</file:inbound-endpoint>
<file:outbound-endpoint path="E:\backup\test_out" outputPattern="Finaloutput.txt" responseTimeout="10000" connector-ref="File1" doc:name="File"/>
Thanks in advance

You can #[function:dateStamp] or #[function:datestamp:dd-MM-yy] to achieve this as described in this link
An example would be :
<file:outbound-endpoint path="E:\backup\test_out" outputPattern="Finaloutput_[function:dateStamp].txt" responseTimeout="10000" connector-ref="File1" doc:name="File"/>
EDIT:
To always show the current timestamp to your files in working firectory, you can create another flow which reads files from working directory at a specific interval, and just copy them to same directory using file:outbound-endpoint

we have mel using this we cal achieve current date and time
[server.dateTime.format("yyyyMMddhhmmss")].txt
The format u like we can set in the expression.

This worked for me
<file:outbound-endpoint path="YOUR_PATH" outputPattern="#[function:datestamp:yyyyMMdd-HHmmssSSSSSS]
_#[message.inboundProperties.originalFilename]" responseTimeout="10000" doc:name="Backup In Mule"/>

Related

Anypoint Studio console execution does not go past deployed

Below is the configuration xml of my small program in Anypoint Studio. What i am trying to do is copying one text file data(pipe delimited) to another text file. The execution goes well but stops at status as "Deployed". I have tried other transformations as well but the result is same. Help is highly appreciable. Thanks in advance.
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd">
<flow name="texttoexcelFlow" doc:id="42aaa83a-e26a-4f6d-8d2f-da3613a8d232" initialState="started">
<file:read doc:name="Read" doc:id="89fa46c9-aa14-4a79-b7ab-e609b9fad501" path="D:\Mulesoft Input\Name.txt" outputMimeType="application/json" outputEncoding="UTF-8">
<repeatable-in-memory-stream />
</file:read>
<ee:transform doc:name="Transform Message" doc:id="86dc86b8-99ed-4bee-b5bc-e07616e44431" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/csv headerLineNumber = 0 , header = false , separator = "|"
---
payload map ( payload01 , indexOfPayload01 ) -> {
FirstName: payload01.FirstName ,
LastName: payload01.LastName
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<file:write doc:name="Write" doc:id="3884725e-3870-4ef1-9e05-b10a2274dfa6" path="C:\Users\aseem\Desktop\Mulesoft Output\Excel.txt">
</file:write>
</flow>
</mule>
"
You need something to trigger the flow to run. file:read doesn’t do this automatically.
All flows need a ‘Source’ to trigger them unless you are calling them from other flows using flow-ref (or from dataweave using a lookup()).
If you know the exact file you want then you can put a scheduler before your file:read to trigger the flow:
<scheduler>
<scheduling-strategy>
<fixed-frequency startDelay="5" frequency="10" timeUnit="SECONDS"/>
</scheduling-strategy>
</scheduler>
Or you can use a file:listener to listen for new files in a directory etc as the source directly:
<flow name="onNewFile">
<file:listener config-ref="file" directory="test-data/in" autoDelete="true">
<scheduling-strategy>
<fixed-frequency frequency="1000"/>
</scheduling-strategy>
</file:listener>
...
</flow>
You can use fixed frequency or cron. More details here: https://docs.mulesoft.com/mule-runtime/4.1/scheduler-xml-reference

Is it possible to get file name while reading a file using WSO2 ESB File Connector 2

I am doing a simple file read using WSO2 ESB File connection 2. I try to read a .OUT file from the given directory. It reads the first file (alphabetical order?) well but I have no idea which file is read. Is there a (transport?) property that gets populated when a file is read?
Here is my code in a proxy service
<fileconnector.read>
<source>file:///D:/temp</source>
<filePattern>.*\.OUT</filePattern>
<contentType>text/plain</contentType>
</fileconnector.read>
<log level="full"/>
I get the following response
INFO - LogMediator To: /services/SampleProxy.SampleProxyHttpSoap12Endpoint, WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: urn:uuid:f5737693-6a51-4044-9134-95cd61eaeaa4, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><text xmlns="http://ws.apache.org/commons/ns/payload">abc,123
qwe,456
</text></soapenv:Body></soapenv:Envelope>
Give it a try like this. (Just a suggestion. I didn't try.)
<log level="custom">
<property name="FileName" expression="$trp:FILE_NAME" />
</log>
If you want to read a particular file, you can pass the full path as a value in source and you can skip the file pattern value.
<fileconnector.read>
<source>file:///D:/temp/abc.OUT</source>
<filePattern></filePattern>
<contentType>text/plain</contentType>
</fileconnector.read>
This is a valid requirement. We will consider this in our future release. Please use [1] to track the progress.
[1] https://wso2.org/jira/browse/ESBCONNECT-192

Moving file from one location to another in Mule

I am new to Mule. I have to do the following task.
A file is at some location. I need to move that file to some other location. The criteria to select the location is based on file name.
Suppose, the file name is 'abc_loc1'. Then this file is to be moved into folder Location1. If the file name is 'abc_loc2', it should be moved into Location2.
You can use the Mule file transport with inbound and outbound endpoints to move files, and either set a dynamic path attribute for the outbound, or use choice routing based on the original filename. You will have the original file name available as #[message.inboundProperties.originalFilename].
UPDATE (example flow):
<file:connector name="File"/>
<flow name="exampleFlow">
<file:inbound-endpoint connector-ref="File" path="/tmp/1" responseTimeout="10000" />
<set-variable variableName="myPath" value="#[message.inboundProperties['originalFilename'].substring(message.inboundProperties['originalFilename'].indexOf('_')+1)]" />
<file:outbound-endpoint path="/tmp/#[flowVars['myPath']]" responseTimeout="10000" connector-ref="File" outputPattern="error#[message.inboundProperties['originalFilename']]"/>
</flow>
UPDATE 2:
to use choice routing replace the above file-outbound with something like this:
<choice>
<when expression="#[flowVars['myPath'] == '1']">
<file:outbound-endpoint path="/tmp/1" responseTimeout="10000" connector-ref="File" outputPattern="error#[message.inboundProperties['originalFilename']]"/>
</when>
<when expression="#[flowVars['myPath'] == '2']">
<file:outbound-endpoint path="/tmp/2" responseTimeout="10000" connector-ref="File" outputPattern="error#[message.inboundProperties['originalFilename']]"/>
</when>
</choice>

Unable to create dynamic file inbound endpoint in mule

I get a file path as an input to mule inside xml. Using XPATH expression, I am able to extract the path. I want to read a particular file from that path. I tried to define file inbound endpoint as below. But it doesn't seem to be working.
<flow name="flow1">
....
....
<set-session-variable variableName="filePath" value="#[xpath://filePath]" />
<flow-ref name="fileFlow"/>
</flow>
<flow name="fileFlow">
<file:inbound-endpoint path="#[header:SESSION:filePath]" />
</flow>
My understanding here is that no code can be placed before an inbound-endpoint. Hence I defined it in another flow. Please suggest if there is a way to read the file from a specified path.
Unfortunately, you cannot programmatically call an inbound-endpoint like that.
However the same functionality can be achieved using the Mule requester module:
Example:
<flow name="RequestFile" doc:name="RequestFile">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="requestfile" doc:name="HTTP"/>
<mulerequester:request config-ref="Mule_Requester" resource="file:///s/tmp/demorequester/read/#[message.inboundProperties['filename']]" returnClass="java.lang.String" doc:name="Request a file"/>
</flow>
Instructions here: https://github.com/mulesoft/mule-module-requester and https://blogs.mulesoft.com/dev/mule-dev/introducing-the-mule-requester-module/

Mule smtp not sending a message

I am using Mule 3.4 and I try to send a file from a folder in the mail.
The console displays:
connector.file.mule.default.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file: C:\Users\bekbol\Documents\smtp\test.txt
My config file is below:
<flow name="outcomingSmtp" doc:name="outcomingSmtp">
<file:inbound-endpoint path="${file.outcomingSmtp}" responseTimeout="100000" doc:name="File" pollingFrequency="10000" moveToDirectory="${file.outcomingBackupSmtp}">
<file:filename-wildcard-filter
pattern="*.txt" />
<file:file-to-string-transformer doc:name="File to String"/>
</file:inbound-endpoint>
<object-to-byte-array-transformer doc:name="Object to Byte Array"/>
<smtp:outbound-endpoint host="${smtp.host}" port="${smtp.port}" user="${email.username}" password="${email.password}" to="${header.to}" from="${header.from}" subject="${header.subject}" responseTimeout="100000" mimeType="text/plain" doc:name="SMTP">
<email:string-to-email-transformer doc:name="String to Email"/>
</smtp:outbound-endpoint>
</flow>
I don't think you need
<file:file-to-string-transformer doc:name="File to String"/>
nested inside your <file:inbound-endpoint>. Move it outside right after you close </file:inbound-endpoint> and
remove <object-to-byte-array-transformer doc:name="Object to Byte Array"/>

Resources