[Thread] Scrutage répertoire, accès concurrent
Hello,
dans les grandes lignes, j'ai un thread principal T1 qui contient une boucle infinie afin de scruter un répertoire en entrée.
Dès qu'il détecte un fichier dedans, il move ce fichier dans un répertoire "en_cours" et crée un autre thread T2 pour traiter ce fichier ( un sql ) .
Ce T2, traite le sql, puis move le fichier de "en_cours" à "traité".
Cest donc bien délimité et il n'est pas sensé y avoir de problème d'accès concurrent sur mes fichiers, pourtant , aléatoirement, j'ai un plantage lorsque je tente de bouger mes fichiers d'un répertoire vers un autre.
Ma classe :
Code:
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| public class ThreadGestionImport implements Runnable {
public ThreadGestionImport() {
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
Logger log = Logger.getLogger("toto");
try {
FileHandler hand = new FileHandler("logs/debug.log");
hand.setFormatter(new CustomLoggingFormatter());
log.addHandler(hand);
if (Lanceur.mode.equals("ALL") || Lanceur.mode.equals("INSERT")) {
Lanceur.logger.log(Level.INFO, "[DEBUT] Gestion imports");
while(!Lanceur.tablesParcourues || new File("a_traiter").listFiles().length > 0){
if((Lanceur.NB_THREADS_IMPORT >= Lanceur.nbThreadsImportCrees) && new File("a_traiter").listFiles().length > 0){
File f = getSingleFile();
log.log(Level.INFO, "lancement thread IMPORT => "+f.getName());
ImportFileThread th = new ImportFileThread(f);
new Thread(th).start();
Lanceur.nbThreadsImportCrees++;
}
}
Lanceur.logger.log(Level.INFO, "[FIN] Gestion imports");
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public File getSingleFile(){
boolean isOk = false;
File f = new File("");
if(Lanceur.prop.getProperty("VERIF_PERES_TRAITES").equals("OUI")){
while(!isOk){
File[] fs = new File("a_traiter").listFiles();
for(int i=0;i<fs.length;i++){
String table = fs[i].getName().replace("script_insertion_", "").replace(".sql", "");
ArrayList<String> parents = getParents(table);
if(checkParentsDone(parents) && getTablesToGenerate().containsAll(parents)){
isOk = true;
f = fs[i];
try {
GestionFichiers.move("a_traiter/" + fs[i].getName(),
"en_cours/" + fs[i].getName());
} catch (IOException e) {
e.printStackTrace();
}
}else if(! getTablesToGenerate().containsAll(getParents(table))){
isOk =true;
f = fs[i];
try {
GestionFichiers.move("a_traiter/" + fs[i].getName(),
"en_cours/" + fs[i].getName());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}else{
File[] fs = new File("a_traiter").listFiles();
try {
GestionFichiers.move("a_traiter/" + fs[0].getName(),
"en_cours/" + fs[0].getName());
} catch (IOException e) {
e.printStackTrace();
}
f = fs[0];
}
return new File("en_cours/"+f.getName());
}
public int getNbFiles(){
File[] f = new File("a_traiter").listFiles();
return (f.length);
}
private ArrayList<String> getTablesToGenerate(){
String pathDependances = Lanceur.prop
.getProperty("TABLE_LIST_FILE_PATH");
ArrayList<String> listeTables = new ArrayList<String>();
try {
Scanner sc = new Scanner(new File(pathDependances));
while(sc.hasNextLine()){
String line = sc.nextLine();
if(!line.startsWith("#"))
listeTables.add(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return listeTables;
}
private boolean checkParentsDone(ArrayList<String> parents){
boolean isOk = false;
int i=0;
for(String parent : parents){
for(File f : new File("traite").listFiles()){
if(f.getName().replace("script_insertion_", "").replace(".sql", "").equals(parent)){
i++;
break;
}
}
}
if(i==parents.size())
isOk = true;
return isOk;
}
private ArrayList<String> getParents(String table){
String pathDependances = Lanceur.prop
.getProperty("TABLE_DEPENDENCIES_FILE_PATH");
ArrayList<String> listeTablesParentes = new ArrayList<String>();
String ligne;
String[] ligneScindee;
String nomTableEnfant = "";
String nomTableParent = "";
try {
Scanner sc = new Scanner(new File(pathDependances));
// parcours du fichier des dependances de tables.
while (sc.hasNextLine()) {
ligne = sc.nextLine();
ligneScindee = ligne.split(";");
nomTableEnfant = ligneScindee[1];
nomTableParent = ligneScindee[3];
// si nom match, on stocke les cles etrangere et tables parentes
if (table.equals(nomTableEnfant) && !listeTablesParentes.contains(nomTableParent)) {
listeTablesParentes.add(nomTableParent);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return listeTablesParentes;
}
} |
Vous voyez quelque chose qui cloche?