0

My application listens on some directory and it's sub directories. For listening on directory I use JNotify. When new file is created on directory application checks the files and processes it in some way. Below is code:

import net.contentobjects.jnotify.JNotify; import net.contentobjects.jnotify.JNotifyListener; public class JNotifyDemo { public void sample() throws Exception { // path to watch //String path = System.getProperty("user.home"); String path = "/folder"; System.out.println(path); // watch mask, specify events you care about, // or JNotify.FILE_ANY for all events. int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED; // watch subtree? boolean watchSubtree = true; // add actual watch int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener()); // sleep a little, the application will exit if you // don't (watching is asynchronous), depending on your // application, this may not be required Thread.sleep(1000000); // to remove watch the watch boolean res = JNotify.removeWatch(watchID); if (!res) { // invalid watch ID specified. } } class Listener implements JNotifyListener { public void fileRenamed(int wd, String rootPath, String oldName, String newName) { print("renamed " + rootPath + " : " + oldName + " -> " + newName); } public void fileModified(int wd, String rootPath, String name) { print("modified " + rootPath + " : " + name); } public void fileDeleted(int wd, String rootPath, String name) { print("deleted " + rootPath + " : " + name); } public void fileCreated(int wd, String rootPath, String name) { print("created " + rootPath + " : " + name); //check file whether it is xml or not //validate xml //do some internal processing of file // and do other jobs like inserting into database } void print(String msg) { System.err.println(msg); } } public static void main(String[] args) throws Exception { new JNotifyDemo().sample(); } } 

As you can see from code, application processes one file per time. Any advice speeding up this application like using threading or anything else?

3
  • What needs to be speeded up? Don't optimize before knowing if it should be optimized, and what should be optimized. Commented Dec 14, 2012 at 9:09
  • i cannot see any bottle neck here because you haven't done anything to your codes. it is just a watcher code from the demo. Commented Dec 14, 2012 at 9:09
  • 1
    Maybe synchronized print? And then a postponing message queue. Commented Dec 14, 2012 at 9:18

2 Answers 2

1

I'd recomand you to use java.nio.file.Path which extends Watchable interface, it may be registered with a watch service so that it can be watched for changes and events.

This event-driven approach speed-up your application. Take a look of example.

Path and Watchable both included in java 7.

Sign up to request clarification or add additional context in comments.

1 Comment

there are quite a few problem with the watcher service in java 7, e.g. stackoverflow.com/questions/8109382/…
0

how much processing is actually going on? keep in mind that IO is very slow, and, unless your processing takes considerable amount of time, multithreaded solution will be bottlenecked on reading the file.

anyways, quick way to implement parallel processing is to start an ExecutorService and submit it a Runnable with file path as a parameter.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.