Last active
May 12, 2020 15:30
-
-
Save abn/e6064840bc6c0450976a to your computer and use it in GitHub Desktop.
Apache VFS file listener PoC Quickstart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Licensed to the Apache Software Foundation (ASF) under one or more | |
* contributor license agreements. See the NOTICE file distributed with this | |
* work for additional information regarding copyright ownership. The ASF | |
* licenses this file to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
* License for the specific language governing permissions and limitations under | |
* the License. | |
*/ | |
import java.io.File; | |
import java.io.IOException; | |
import org.apache.commons.vfs2.FileChangeEvent; | |
import org.apache.commons.vfs2.FileListener; | |
import org.apache.commons.vfs2.FileObject; | |
import org.apache.commons.vfs2.FileSystemManager; | |
import org.apache.commons.vfs2.VFS; | |
import org.apache.commons.vfs2.impl.DefaultFileMonitor; | |
/** | |
* Apache VFS file listener PoC Quickstart | |
* | |
* Dependencies: org.apache.commons:commons-vfs2:2.0 | |
* | |
* @author Arun Babu Neelicattu (abn) | |
* | |
*/ | |
public class SimpleFileMonitor { | |
private static void monitor(File file) throws IOException { | |
// source: http://stackoverflow.com/a/7977428/1874604 | |
// setup monitoring | |
FileSystemManager manager = VFS.getManager(); | |
FileObject fobj = manager.resolveFile(file.getPath()); | |
DefaultFileMonitor fm = new DefaultFileMonitor(new SimpleFileListener()); | |
fm.setDelay(500); | |
fm.addFile(fobj); | |
fm.start(); | |
} | |
public static void main(String[] args) throws IOException { | |
File file = File.createTempFile("simplefile", ".tmp"); | |
monitor(file); | |
file.delete(); | |
} | |
private static class SimpleFileListener implements FileListener { | |
public void fileChanged(FileChangeEvent arg0) throws Exception { | |
System.out.println("File changed"); | |
} | |
public void fileCreated(FileChangeEvent arg0) throws Exception { | |
System.out.println("File created"); | |
} | |
public void fileDeleted(FileChangeEvent arg0) throws Exception { | |
System.out.println("File deleted"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment