
Questions: 23,767 //
Answers: 52,373 //
Contributing Members: 18,028
Hi,
Have to process the custom file by ascending order.
The scenario is below
properties/global.properties:
file.name=Test_%y%y%y%y%M%M%d%d.txt
The file name convention is Test_yyyyMMdd.txt
The folder contains the list of files below
Test_20180105.txt
Test_20180104.txt
Test_20180103.txt
<file:connector name="File" autoDelete="true" streaming="true" validateConnections="true" doc:name="File">
<service-overrides messageReceiver="com.boi.file.receiver.BOIFileMessageReceiver"/>
</file:connector>
<flow name="sample-fileFlow">
<<file:inbound-endpoint path="D:\temp\input" connector-ref="File" responseTimeout="10000" doc:name="File" comparator="com.boi.file.comparator.FileComparator" reverseOrder="false"/>
<logger message="#[groovy:message.getPayloadAsString()]" level="INFO" doc:name="Logger"/>
</flow>
public class BOIFileMessageReceiver extends FileMessageReceiver {
public BOIFileMessageReceiver(Connector connector, FlowConstruct flowConstruct, InboundEndpoint endpoint,
String readDir, String moveDir, String moveToPattern, long frequency) throws CreateException {
super(connector, flowConstruct, endpoint, readDir, moveDir, moveToPattern, frequency);
}
protected void basicListFiles(File currentDirectory, List<File> discoveredFiles) {
File[] files = currentDirectory.listFiles(new MyFileNameFilter());
if (files == null) {
return;
}
for (File file : files) {
if (!file.isDirectory()) {
discoveredFiles.add(file);
} else {
if (((FileConnector) connector).isRecursive()) {
this.basicListFiles(file, discoveredFiles);
}
}
}
}
public static class MyFileNameFilter implements FilenameFilter {
String l_sFilePattern = BOIUtils.getFileName();
@Override
public boolean accept(File dir, String name) {
return name.matches(l_sFilePattern);
}
}
}
public class BOIUtils {
/**
*
* @return
*/
public static String getFileName() {
String l_sFileName = "";
try {
Object l_objObject = new Object();
Properties l_objProperties = new Properties();
InputStream l_objInputStream = IOUtils.getResourceAsStream("properties/global.properties",
l_objObject.getClass());
l_objProperties.load(l_objInputStream);
l_sFileName = (String) l_objProperties.get("file.name");
l_sFileName = resolveFileName(l_sFileName);
} catch (Exception e) {
org.apache.log4j.Logger.getLogger("Vijaya Ragavan Sangili ").log(org.apache.log4j.Priority.INFO,
" Error Occured :::", e);
}
return l_sFileName;
}
/**
*
* @param pFileName
* @return
*/
public static String resolveFileName(String pFileName) {
java.util.Calendar l_Calender = Calendar.getInstance();
l_Calender.setTime((new Date()));
String l_sOrgFileName = pFileName;
if (pFileName == null || pFileName.length() == 0) {
return "";
}
try {
String l_sDay = String.valueOf((l_Calender.get(Calendar.DAY_OF_MONTH)));
String l_sMonth = String.valueOf((l_Calender.get(Calendar.MONTH) + 1));
String l_sYear = String.valueOf((l_Calender.get(Calendar.YEAR)));
if (l_sDay.trim().length() < 2) {
l_sDay = "0" + l_sDay;
}
if (l_sMonth.trim().length() < 2) {
l_sMonth = "0" + l_sMonth;
}
l_sOrgFileName = l_sOrgFileName.replaceAll("%d%d", l_sDay);
l_sOrgFileName = l_sOrgFileName.replaceAll("%M%M", l_sMonth);
l_sOrgFileName = l_sOrgFileName.replaceAll("%y%y%y%y", l_sYear);
l_sOrgFileName = l_sOrgFileName.replaceAll("%y%y", l_sYear.substring(2, 4));
} catch (Exception e) {
org.apache.log4j.Logger.getLogger("Vijaya Ragavan Sangili ").log(org.apache.log4j.Priority.INFO,
" Error Occured :::", e);
return pFileName;
}
return l_sOrgFileName;
}
}
When i use the below piece of configuration the inbound file connector only picks the current date file
<service-overrides messageReceiver="com.boi.file.receiver.BOIFileMessageReceiver"/>
In the same way incase if the folder contains previous day files it has to process the file by ascending order and the order is
1.Test_20180103.txt 2.Test_20180104.txt 3.Test_20180105.txt
For that used the file comparator and the code is
public class FileComparator implements Comparator<Object> {
@Override
public int compare(Object o1, Object o2) {
File file1 = (File) o1;
File file2 = (File) o2;
long index1 = file1.lastModified();
long index2 = file2.lastModified();
if (index1 == index2) {
return 0;
} else if (index1 > index2) {
return 1;
} else {
return -1;
}
}
}
<file:inbound-endpoint path="D:\temp\input" connector-ref="File" responseTimeout="10000" doc:name="File" comparator="com.boi.file.comparator.FileComparator" reverseOrder="false"/>
The below two combinations is working
1.To process the current date file
Configuration is :
2.To process the file ascending order
configuration is :
<file:inbound-endpoint path="D:\temp\input" connector-ref="File" responseTimeout="10000" doc:name="File" **comparator="com.boi.file.comparator.FileComparator" reverseOrder="false"**/>
In my requirement it has to process the file by current date and incase if the folder contains previous day file then it has to process the file by ascending oder
But here it is not working
Could please someone help to solve this
It is not clear why you need a custom MessageReceiver. lf my understanding of your use case is correct, all you need is to use a FileComparator.
Indeed, the org.mule.transport.file.comparator.OlderFirstComparator
should work for you.