Closed dchitra33 closed 9 years ago
It is not possible as far as I remember using just one DirectoryPoller instance (I need to dig in the code to be sure. Will do that later tonight)
You could do it with two DirectoryPoller instances, where you feed them with different polling intervals (and different directories), but with same listener instance (or separate listener instance if you prefer that).
Regards, Alex
Below is a runnable example how to do it with two instances of DirectoryPoller
. Run the below code and "manually" add/remove/modify files in the two different folders. Watch the frequency of the log messages in the console window. One of them is faster than the other.
public static void main(String[] args) {
DirectoryListener listener = new DirectoryListener() {
@Override
public void fileAdded(FileAddedEvent event) {
System.out.println("+ " + event.getFileElement());
}
@Override
public void fileRemoved(FileRemovedEvent event) {
System.out.println("- " + event.getFileElement());
}
@Override
public void fileModified(FileModifiedEvent event) {
System.out.println("¤ " + event.getFileElement());
}
};
DirectoryPoller dp1 = DirectoryPoller.newBuilder()
.addPolledDirectory(new JavaIoFileAdapter(new File("c:\\temp\\dir1")))
.setPollingInterval(20, TimeUnit.SECONDS)
.addListener(listener)
.start();
DirectoryPoller dp2 = DirectoryPoller.newBuilder()
.addPolledDirectory(new JavaIoFileAdapter(new File("c:\\temp\\dir2")))
.setPollingInterval(1, TimeUnit.SECONDS)
.addListener(listener)
.start();
}
Above code will work in local file system,but not on FTP server.I have two polledDirectories(polledDirectory ,polledDirectory1 ) adding these to DirectoryPoller objects(dp,dp1).But this doesn't work.Is it possible like this?
polledDirectory = new FtpDirectory(host, workingDir1, username, password);
polledDirectory1 = new FtpDirectory(host, workingDir2, username, password);
DirectoryPoller dp = DirectoryPoller.newBuilder()
.addPolledDirectory(polledDirectory)
.enableFileAddedEventsForInitialContent()
.enableParallelPollingOfDirectories()
.setPollingInterval(10, TimeUnit.SECONDS)
.start();
TimeUnit.MINUTES.sleep(10);
dp.stop();
DirectoryPoller dp1 = DirectoryPoller.newBuilder()
.addPolledDirectory(polledDirectory1)
.enableFileAddedEventsForInitialContent()
.enableParallelPollingOfDirectories()
.setPollingInterval(30, TimeUnit.SECONDS)
.start();
TimeUnit.MINUTES.sleep(30);
dp.stop();
The problem with the above code is that dp
is stopped before dp1
is started. In other words they don't run at the same time. Also note that there is no need to enableParallelPollingOfDirectories()
when there is only one directory (per DirectoryPoller
)
Try something like this instead:
public static void main(String[] args) {
PolledDirectory polledDirectory = new FtpDirectory(host, workingDir1, username, password);
PolledDirectory polledDirectory1 = new FtpDirectory(host, workingDir2, username, password);
DirectoryPoller dp = DirectoryPoller.newBuilder()
.addPolledDirectory(polledDirectory)
.enableFileAddedEventsForInitialContent()
.setPollingInterval(10, TimeUnit.SECONDS)
.addListener(__Your_Listener__)
.start();
DirectoryPoller dp1 = DirectoryPoller.newBuilder()
.addPolledDirectory(polledDirectory1)
.enableFileAddedEventsForInitialContent()
.setPollingInterval(30, TimeUnit.SECONDS)
.addListener(__Your_Listener__)
.start();
// do something else..
// when needed then stop dp and dp1
dp.stop();
dp1.stop();
}
i want to implement addAdditionalFolders() which is responsible for adding any newly added folders to the monitors list of folders using FTM in java
something like I want to notify if new folder created in the existing folder path
I'm not sure I understand what you want to do? Can you explain further? What is FTM?
/Alex
You can recursively add new folders to the DirectoryPoller
. Something like the below example:
@Override
public void fileAdded(FileAddedEvent event) {
FileElement file = event.getFileElement();
if(file.isDirectory()){
DirectoryPoller dp = event.getDirectoryPoller();
/*
* Somehow transform your FileElement object to a PolledDirectory
* object.
*/
PolledDirectory dir = ... // transform your FileElement somehow...
/*
* Or, if your implementation implements both PolledDirectory
* and FileElement then you only need to do a simple cast:
*/
dp.addPolledDirectory((PolledDirectory) file);
}
}
NOTE! You will also need to handle the case when a folder is deleted (i.e you should remove that PolledDirectory from the DirectoryPoller instance).
Sorry not FTM it is FTP(file transfer protocal).I want something like i have folderPaths list which i am going to be moniter.If I add any additional folders(not file) into the monitors list of folders.It has to be notify.
I have public void fileAdded(FileAddedEvent event) { FtpDirectory ftpDir = (FtpDirectory)event.getPolledDirectory(); I want to save file details in to DB table.How to fetch those details from FileAddedEvent object?any suggetion
Wnenever i monitor it will come to fileAdded method here i have to save file attributes into Db table ?
how to read file using Apache commons net api in java?
To get information about added FileElement, use as follow:
@Override
public void fileAdded(FileAddedEvent event) {
FtpFile file = (FtpFile) event.getFileElement();
// implement any method needed in the FtpFile class
// and call those methods here
}
What is it that you want to do with the FtpFile? What information do you need from it? Do you want to download the file using apache commons net API?
Yes I want to retrieve the file content from FTP server and save it in the db table.I have to retrieve the .xls file content, I am trying with
Input stream input =ftpclient.retrieveFileStream("/example.xls")
First this input is returning null.second is how to read this file again?
There should be plenty of examples on google how to download files with apache commons net API. This for example: http://www.journaldev.com/974/java-ftp-download-example-using-apache-commons-net-api
how to poll multiple directories parlell at different time intervals in java file monitoring? I have set default time interval for multiple Diectories using setPollingInterval(30, TimeUnit.SECONDS). I want to set different time intervals for different directories .Is it possible?