2012-12-14 7 views
0

Mon application écoute sur certains répertoires et sous-répertoires. Pour écouter sur le répertoire j'utilise JNotify. Lorsqu'un nouveau fichier est créé dans l'annuaire, l'application vérifie les fichiers et les traite d'une manière ou d'une autre. Ci-dessous est le code:Accélération de l'application Java

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(); 
    } 
} 

Comme vous pouvez le voir à partir du code, l'application traite un fichier par temps. Des conseils pour accélérer cette application comme l'utilisation du filetage ou autre?

+0

Qu'est-ce qui doit être accéléré? Ne pas optimiser avant de savoir si elle devrait être optimisée, et ce qui devrait être optimisé. –

+0

Je ne vois pas de bouteille ici parce que vous n'avez rien fait pour vos codes. c'est juste un code d'observateur de la démo. – gigadot

+1

Peut-être «impression synchronisée»? Et puis une file d'attente de messages de report. –

Répondre

1

Je vous recommande d'utiliser java.nio.file.Path qui étend interface Watchable, il peut être enregistré avec un service de surveillance afin qu'il puisse être surveillé pour les changements et les événements.

Cette approche axée sur les événements accélère votre application. Jetez un oeil de example.

Path et Watchable tous deux inclus dans Java 7.

+0

il y a un certain nombre de problèmes avec le service de surveillance dans Java 7, par exemple. http://stackoverflow.com/questions/8109382/java-nio-filesystem-watcher-locks-directories-deletion-becomes-impossible – gigadot

0

combien le traitement se passe réellement? Gardez à l'esprit que IO est très lent, et, à moins que votre traitement ne prenne beaucoup de temps, la solution multithread sera bloquée à la lecture du fichier. De toute façon, un moyen rapide d'implémenter le traitement parallèle est de commencer un ExecutorService et de lui soumettre un Runnable avec le chemin du fichier en tant que paramètre.