Guest

Untitled

By: a guest on Feb 23rd, 2012  |  syntax: Java  |  size: 2.32 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. package proyectoClase;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectOutputStream;
  7. import java.util.concurrent.Executors;
  8. import java.util.concurrent.ScheduledExecutorService;
  9. import java.util.concurrent.TimeUnit;
  10.  
  11.  
  12. public class SegundoPlano implements Runnable{
  13.         ScheduledExecutorService ses;
  14.         ListenerPrincipal listener;
  15.        
  16.        
  17.         public SegundoPlano(ListenerPrincipal listener) {
  18.                 this.listener = listener;
  19.                 ses = Executors.newScheduledThreadPool(1);           //creamos un nuevo schedule para ses con la clase estática Executors
  20.                 ses.scheduleAtFixedRate(this, 1, 5, TimeUnit.MINUTES);   //REPETIMOS CADA MINUTO Y LA PRIMERA VEZ SE EJECUTARÁ DESPUÉS DE PASADO 5 MINUTOs
  21.  
  22.         }
  23.        
  24.                
  25.  
  26.         @Override
  27.         public void run() {
  28.                 boolean correcto = true;
  29.                
  30.                 try {
  31.                         ObjectOutputStream escritorDeObjetos = new ObjectOutputStream(new FileOutputStream(ListenerPrincipal.getDatosPropietario()));
  32.                         escritorDeObjetos.writeObject(listener.getGestionPropietario());
  33.                 } catch (FileNotFoundException e) {
  34.                         correcto = false;
  35.                         listener.getMenu().getStatusBar().setText("Error al grabar fichero no encontrado!!");
  36.                         e.printStackTrace();
  37.                 } catch (IOException e) {
  38.                         correcto = false;
  39.                         listener.getMenu().getStatusBar().setText("Error al grabar problema de entrada salida!!");
  40.                         e.printStackTrace();
  41.                 }
  42.                
  43.                 if (correcto)
  44.                         listener.getMenu().getStatusBar().setText("Datos guardados correctamente en el fichero " + ListenerPrincipal.getDatosPropietario());
  45.  
  46.                 correcto = true;
  47.                
  48.                 try {
  49.                         ObjectOutputStream escritorDeObjetos = new ObjectOutputStream(new FileOutputStream(ListenerPrincipal.getDatosVehiculo()));
  50.                         escritorDeObjetos.writeObject(listener.getGestionVehiculo());
  51.                 } catch (FileNotFoundException e) {
  52.                         correcto = false;
  53.                         listener.getMenu().getStatusBar().setText("Error al grabar, fichero no encontrado!!");
  54.                         e.printStackTrace();
  55.                 } catch (IOException e) {
  56.                         correcto = false;
  57.                         listener.getMenu().getStatusBar().setText("Error al grabar problema de entrada salida!!");
  58.                         e.printStackTrace();
  59.                 }
  60.                
  61.                 if (correcto)
  62.                         listener.getMenu().getStatusBar().setText("Datos guardados correctamente en el fichero " + ListenerPrincipal.getDatosVehiculo());
  63.                
  64.                
  65.         }
  66.        
  67.         private void detener() {
  68.                 ses.shutdown();
  69.         }
  70.        
  71.  
  72. }