package proyectoClase;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class SegundoPlano implements Runnable{
ScheduledExecutorService ses;
ListenerPrincipal listener;
public SegundoPlano(ListenerPrincipal listener) {
this.listener = listener;
ses = Executors.newScheduledThreadPool(1); //creamos un nuevo schedule para ses con la clase estática Executors
ses.scheduleAtFixedRate(this, 1, 5, TimeUnit.MINUTES); //REPETIMOS CADA MINUTO Y LA PRIMERA VEZ SE EJECUTARÁ DESPUÉS DE PASADO 5 MINUTOs
}
@Override
public void run() {
boolean correcto = true;
try {
ObjectOutputStream escritorDeObjetos = new ObjectOutputStream(new FileOutputStream(ListenerPrincipal.getDatosPropietario()));
escritorDeObjetos.writeObject(listener.getGestionPropietario());
} catch (FileNotFoundException e) {
correcto = false;
listener.getMenu().getStatusBar().setText("Error al grabar fichero no encontrado!!");
e.printStackTrace();
} catch (IOException e) {
correcto = false;
listener.getMenu().getStatusBar().setText("Error al grabar problema de entrada salida!!");
e.printStackTrace();
}
if (correcto)
listener.getMenu().getStatusBar().setText("Datos guardados correctamente en el fichero " + ListenerPrincipal.getDatosPropietario());
correcto = true;
try {
ObjectOutputStream escritorDeObjetos = new ObjectOutputStream(new FileOutputStream(ListenerPrincipal.getDatosVehiculo()));
escritorDeObjetos.writeObject(listener.getGestionVehiculo());
} catch (FileNotFoundException e) {
correcto = false;
listener.getMenu().getStatusBar().setText("Error al grabar, fichero no encontrado!!");
e.printStackTrace();
} catch (IOException e) {
correcto = false;
listener.getMenu().getStatusBar().setText("Error al grabar problema de entrada salida!!");
e.printStackTrace();
}
if (correcto)
listener.getMenu().getStatusBar().setText("Datos guardados correctamente en el fichero " + ListenerPrincipal.getDatosVehiculo());
}
private void detener() {
ses.shutdown();
}
}