[Castore-commits] ConverterThread.java 1.5 1.6

mathieu grimault grimault-m at adullact1.hosting.cri74.org
Jeu 14 Déc 11:25:36 CET 2006


Update of /cvsroot/castore/castore-core/src/java/fr/emn/castor/documents/metier/textes/retro
In directory adullact1:/tmp/cvs-serv22355/src/java/fr/emn/castor/documents/metier/textes/retro

Modified Files:
	ConverterThread.java 
Log Message:
Annulation dans castore.
Le thread de retro est mise en attente jusqu'a la fin de soumission.

Index: ConverterThread.java
===================================================================
RCS file: /cvsroot/castore/castore-core/src/java/fr/emn/castor/documents/metier/textes/retro/ConverterThread.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** ConverterThread.java	17 Nov 2006 14:51:17 -0000	1.5
--- ConverterThread.java	14 Dec 2006 10:25:34 -0000	1.6
***************
*** 45,48 ****
--- 45,50 ----
  import java.io.IOException;
  import java.io.PrintStream;
+ import java.util.HashMap;
+ import java.util.Map;
  
  import org.apache.commons.logging.Log;
***************
*** 67,70 ****
--- 69,92 ----
   */
  public final class ConverterThread extends Thread {
+ 	
+ 	/**
+ 	 * Une map ayant pour clef les identifiant de document
+ 	 * et pour valeur les ConverterThread associés
+ 	 */
+ 	private static Map threads = new HashMap();
+ 	
+ 	/** Liste des états acceptés */
+ 	public static final int ETAT_EN_ATTENTE	 = 0;
+ 	public static final int ETAT_ANNULER	 = 1;
+ 	public static final int ETAT_CONVERTIR	 = 2;
+ 	
+ 	/** L'état courant de ce Thread */
+ 	private int etat = ETAT_EN_ATTENTE;
+ 	
+ 	/** L'intervalle de sommeil de sommeil des ConverterThread (ms)*/
+ 	private static int INTERVALLE = 5*1000; // 5s
+ 	
+ 	/** Timeout avant de considérer que la soumission a été abandonnée (ms)*/
+ 	private static int TIMEOUT = 30*60*1000; // 30min
  
      /** le logger. */
***************
*** 105,125 ****
              final String classDoc) {
          super("ConverterThread-" + idDoc);
!         if (classDoc.equals(DocumentsConstants.DOC_CLASSE_GROUPE)) {
!             this.idElt = idDoc.substring(idDoc.indexOf(":") + 1, idDoc
!                 .length());
!             this.id = idDoc.substring(0, idDoc.indexOf(":"));
!         } else
!             this.id = idDoc;
!         this.inputFormat = format;
!         this.fonds = Fonds.getInstance();
!         this.fsFile = fsUsed;
!         this.nameFic = nameFichier;
!         this.classDoc = classDoc;
      }
  
      /**
!      * @see java.lang.Thread#run()
!      */
      public void run() {
          // on lance la tache ant de conversion
          // 5 parametres
--- 127,207 ----
              final String classDoc) {
          super("ConverterThread-" + idDoc);
! 		if (classDoc.equals(DocumentsConstants.DOC_CLASSE_GROUPE)) {
! 			this.idElt = idDoc
! 					.substring(idDoc.indexOf(":") + 1, idDoc.length());
! 			this.id = idDoc.substring(0, idDoc.indexOf(":"));
! 		} else {
! 			this.id = idDoc;
! 		}
! 		this.inputFormat = format;
! 		this.fonds = Fonds.getInstance();
! 		this.fsFile = fsUsed;
! 		this.nameFic = nameFichier;
! 		this.classDoc = classDoc;
! 
! 		if (log.isDebugEnabled()) {
! 			log.debug(
! 				"Ajout de ConverterThread(" + id + ") : conversion à faire"
! 			);
! 		}
! 		ConverterThread.threads.put(idDoc, this);
! 		this.etat=ETAT_EN_ATTENTE;
      }
  
      /**
! 	 * @see java.lang.Thread#run()
! 	 */
      public void run() {
+     	
+     	// Le thread est en attente de : 
+     	// - la fin de soumission
+     	// - une annulation
+     	// - un timeout
+     	int attente=0;
+     	while (etat == ETAT_EN_ATTENTE) {
+     		try {
+ 				Thread.sleep(INTERVALLE);
+ 			} catch (InterruptedException e) {
+ 				log.error(
+ 					"Interruption du sommeil de " +
+ 					"ConverterThread(" + id + ") : " + e, e 
+ 				);
+ 				ConverterThread.threads.remove(id);
+ 				return;
+ 			}
+     		attente += INTERVALLE;
+     		if (attente >= TIMEOUT) {
+     			log.error(
+     				"ConverterThread(" + id + ") a attendu plus de " + 
+     				TIMEOUT/(60*1000) + " minutes, " +
+     				"le Thread s'arrête..."
+     			);
+     			ConverterThread.threads.remove(id);
+     			return;
+     		} else {
+     			if (log.isTraceEnabled()) {
+     				log.trace(
+     					"ConverterThread(" + id + ") attend..."
+     				);
+     			}
+     		}
+     	}
+     	
+     	// Si on demande l'annulation -> sortie !
+     	if (etat == ETAT_ANNULER) {
+     		log.debug(
+ 				"ConverterThread(" + id + ") annulé..."
+ 			);
+     		ConverterThread.threads.remove(id);
+     		return;
+     	} else if (etat == ETAT_EN_ATTENTE) {
+     		// cas normalement impossible !
+     		log.error(
+ 				"L'état de ConverterThread(" + id + ") est incohérent ! on annule..."
+ 			);
+     		ConverterThread.threads.remove(id);
+     		return;
+     	}
+     	
          // on lance la tache ant de conversion
          // 5 parametres
***************
*** 156,160 ****
                          + DocumentsConstants.XML;
                  pathTmpDataDir = this.fonds.getPathForId(this.id);
- 
              } else {
                  pathDest = this.fonds.getPathDest(this.id, this.nameFic)
--- 238,241 ----
***************
*** 240,243 ****
--- 321,331 ----
              }
              return;
+         } finally {
+         	if (log.isDebugEnabled()) {
+         		log.debug(
+         			"Suppresion du ConverterThread(" + id + ") : conversion finie"
+         		);
+         	}
+         	ConverterThread.threads.remove(this.id);
          }
          if (log.isDebugEnabled()) {
***************
*** 261,266 ****
                  boolean okClean = converter.cleanConv(this.id, false);
                  if (!okClean && log.isErrorEnabled()) {
!                     log
!                         .error("Le nettoyage ne s'est pas correctement terminé.");
                  }
              }
--- 349,355 ----
                  boolean okClean = converter.cleanConv(this.id, false);
                  if (!okClean && log.isErrorEnabled()) {
!                     log.error(
!                     	"Le nettoyage ne s'est pas correctement terminé."
!                     );
                  }
              }
***************
*** 283,285 ****
--- 372,390 ----
      }
  
+     /**
+      * Setter de l'état du thread
+      * @param i parmis ETAT_XXX
+      */
+     public void setEtat(int i) {
+     	this.etat = i;
+     }
+     
+     /**
+      * Retourne le thread de conversion pour un document
+      * @param idDoc L'identifiant du document
+      * @return Le Thread ou null
+      */
+     public static ConverterThread getThread(String idDoc) {
+     	return (ConverterThread)threads.get(idDoc);
+     }
  }
\ No newline at end of file




Plus d'informations sur la liste de diffusion Castore-commits