IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Java Discussion :

Java watch service problème


Sujet :

Java

  1. #1
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2012
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2012
    Messages : 36
    Par défaut Java watch service problème
    Bonjour ,
    J'ai deux disques dur sur lé réseau , j'utilise le Watch Service API pour detecter le changement sur un dossier particulier , mon problème est que Watch Service fonctionne sur l'un et ne fonctionne pas sur l'autre serveur (reste en ecoute de l'évennement : elle ne le detecte pas) , les deux disques sont sur un seut serveur ubunto , la seule différence entre eux est que l'un est sécurisé et l'autre non ,
    Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    import java.io.IOException;
    import java.nio.file.WatchEvent.Modifier;
    import java.nio.file.FileSystem;
    import java.nio.file.FileSystems;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.StandardWatchEventKinds;
    import java.nio.file.WatchEvent;
    import java.nio.file.WatchKey;
    import java.nio.file.WatchService;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    public class Test {
     
    	private static FileSystem fs = FileSystems.getDefault();
    	private static WatchService ws = null;
     
    	@SuppressWarnings("restriction")
    	public static void main(String args[]) throws InterruptedException {
    		try {
     
    			try {
    				ws = fs.newWatchService();
    			} catch (IOException ex) {
    				Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    			}
    			// Répertoire fictif pour des raisons de sécurités...
    			Path PathHome = fs.getPath("\\\\***.**.**.***\\tmp\\wajdi\\");
    			PathHome.register(ws, new WatchEvent.Kind<?>[]{
    					StandardWatchEventKinds.ENTRY_CREATE,
    					StandardWatchEventKinds.ENTRY_DELETE,
    					StandardWatchEventKinds.ENTRY_MODIFY
    			}, com.sun.nio.file.ExtendedWatchEventModifier.FILE_TREE);
     
    			System.out.println("Ajout des répertoires terminés");
     
    			while(true){
    				WatchKey key = ws.take();
     
    				for (WatchEvent<?> event: key.pollEvents()) {
    					WatchEvent.Kind<?> kind = event.kind();
    					Path path = fs.getPath(key.watchable().toString(), event.context().toString());
     
    					if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
    						if (!Files.isDirectory(path)){
    							System.out.println("Created : "+path.toFile().getAbsolutePath());
    						}
    						else {
    							path.register(ws, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.OVERFLOW, StandardWatchEventKinds.ENTRY_DELETE); 
    							System.out.println(path.toFile().getAbsolutePath()+ " ajouté à la liste d'écoute");
    						}
    					}
     
     
    				}
     
    				boolean valid = key.reset();
    				if (!valid){
    					//System.out.println("la cle n'est plus enregistrée");
    				}
    			}
    		} catch (IOException ex) {
    			Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    		}
    	}
    }
    Si il vous plait avez vous une idée pour le faire fonctionner sur l'autre disque ?
    Merci

  2. #2
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2012
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2012
    Messages : 36
    Par défaut
    J'ai essayé de lire et ecrire sur les deux lecteur réseau et ca marche !! voila le code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    	public static void main(String args[]) throws InterruptedException {
        	FileSystem fs = FileSystems.getDefault();
     
    		Charset charset = Charset.forName("US-ASCII");
    		 Path path2 = fs.getPath("\\\\172.18.0.250\\Qualite\\Archive\\test.txt");
    		try (BufferedReader reader = Files.newBufferedReader(path2, charset)) {
    		    String line = null;
    		    while ((line = reader.readLine()) != null) {
    		        System.out.println(line);
    		    }
    		} catch (IOException x) {
    		    System.err.format("IOException: %s%n", x);
    		}
     
    		String s = "test testtttttttttttttt";
    		try (BufferedWriter writer = Files.newBufferedWriter(path2, charset)) {
    		    writer.write(s, 0, s.length());
    		} catch (IOException x) {
    		    System.err.format("IOException: %s%n", x);
    		}
     
        }
    j'arriive paas à saisir le probleme , l'API ne detecte pas l'evennement !!!

  3. #3
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2012
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2012
    Messages : 36
    Par défaut
    je pense que le problème est au niveau :key.pollEvents()
    en fait quand j'ai mis ce mourceau de code pour le lecteur sécurisé
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    if(key.pollEvents()==null)
            {System.err.println("empty");}
                if(key.pollEvents().size()==0)
                {System.err.println("emptyttt");}
                for (WatchEvent<?> event: key.pollEvents()) {TRAITEMENT}
    J'ai obtenu le message d'erreur "emptyttt"
    Aide SVP je suis totalement perdu !

  4. #4
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 577
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 577
    Par défaut
    Bonjour,

    le polling n'est pas géré sur un lecteur réseau.
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  5. #5
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2012
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2012
    Messages : 36
    Par défaut
    Bonjour , merci bcp pour la réponse , mais le problème c'est qu'il a marché sur un lecteur réseau et l'autre non !!!
    J'ai meme essayé d'utiliser Jnotify mais il m'envoyer la fameuse exception
    A fatal error has been detected by the Java Runtime Environment:
    #
    # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x5c17bd71, pid=1096, tid=4880
    #
    # JRE version: 7.0_07-b10
    # Java VM: Java HotSpot(TM) Client VM (23.3-b01 mixed mode, sharing windows-x86 )
    # Problematic frame:
    # V [jvm.dll+0xabd71]
    #
    # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
    #
    # An error report file with more information is saved as:
    # C:\Users\wajdi\workspace\test1\hs_err_pid1096.log
    #
    # If you would like to submit a bug report, please visit:
    # http://bugreport.sun.com/bugreport/crash.jsp
    #



    Aide svp

  6. #6
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2012
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2012
    Messages : 36
    Par défaut
    J'ai meme essayé Jpathwatch , ça marche sur les deux lecteur réseau mais il ne detecte pas l'évennement sur les sous dossiers , Voila le code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    public class test3 {
    	    static WatchService watchService = FileSystems.getDefault().newWatchService();
    	    static Path path = Paths.get("\\\\**.**.**.**\\Qualite\\Archive");
     
    	/**
             * @param args
             */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		WatchKey key = null;
    		try {
    		    key = path.register(watchService, StandardWatchEventKind.ENTRY_CREATE, StandardWatchEventKind.ENTRY_DELETE);
    		} catch (UnsupportedOperationException uox){
    		    System.err.println("file watching not supported!");
    		    // handle this error here
    		}catch (IOException iox){
    		    System.err.println("I/O errors");
    		    // handle this error here
    		}
    		for(;;){
    		    // take() will block until a file has been created/deleted
    		    WatchKey signalledKey;
    		    try {
    		        signalledKey = watchService.take();
    		    } catch (InterruptedException ix){
    		        // we'll ignore being interrupted
    		        continue;
    		    } catch (ClosedWatchServiceException cwse){
    		        // other thread closed watch service
    		        System.out.println("watch service closed, terminating.");
    		        break;
    		    }
     
    		    // get list of events from key
    		    List<WatchEvent<?>> list = signalledKey.pollEvents();
     
    		    // VERY IMPORTANT! call reset() AFTER pollEvents() to allow the
    		    // key to be reported again by the watch service
    		    signalledKey.reset();
     
    		    // we'll simply print what has happened; real applications
    		    // will do something more sensible here
    		    for(WatchEvent e : list){
    		        String message = "";
    		        if(e.kind() == StandardWatchEventKind.ENTRY_CREATE){
    		            Path context = (Path)e.context();
    		            message = context.toString() + " created";
    		        } else if(e.kind() == StandardWatchEventKind.ENTRY_DELETE){
    		            Path context = (Path)e.context();
    		            message = context.toString() + " deleted";
    		        } else if(e.kind() == StandardWatchEventKind.OVERFLOW){
    		            message = "OVERFLOW: more changes happened than we could retreive";
    		        }
    		        System.out.println(message);
    		    }
    		}
     
     
    	}
     
    }
    J'ai essayé donc trois alternatives mais les trois ont des problème , svp avez vous quelques suggestions concernat un de trois proposition ?
    Merci

Discussions similaires

  1. Réponses: 2
    Dernier message: 21/11/2014, 16h25
  2. Java Watch Service Api Problème
    Par wajdinho dans le forum Général Java
    Réponses: 1
    Dernier message: 19/03/2013, 10h34
  3. Réponses: 10
    Dernier message: 26/08/2008, 10h00
  4. [Débutant][Java] Web Service
    Par ArseNic dans le forum XML/XSL et SOAP
    Réponses: 1
    Dernier message: 14/11/2005, 11h09
  5. [Java et as400] Problème d'autocommit
    Par fraisetagada dans le forum Autres SGBD
    Réponses: 2
    Dernier message: 01/07/2005, 15h54

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo