package com.fp.facade.ejb; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.ejb.Singleton; import javax.ejb.Startup; import javax.persistence.EntityManager; import com.fp.base.persistence.util.job.ServiceManager; import com.fp.common.logger.APPLogger; import com.fp.facade.ejb.helper.BeanHelper; import com.fp.general.scheduler.MiaScheduler; import com.fp.persistence.commondb.PersistenceHelper; import com.fp.persistence.commondb.PersistenceManager; @Singleton @Startup public class JobsIniciatorBean extends BeanHelper { /** Variable que controla que los job's inicien. */ private boolean iniciar = false; private MiaScheduler ms; @PostConstruct protected void iniciartareas() { if (!iniciar) { APPLogger.getLogger().error("INICIA EJECUCION DE TAREAS O DEMONIOS EN EL SERVIDOR"); try { this.startJobs(); iniciar = true; } catch (Exception e) { APPLogger.getLogger().error(e.getStackTrace()); } } } /** * Metodo que inicia el Job de tareas automaticas. * * @throws Exception */ public void startJobs() throws Exception { this.fillThreadFacade(this.getCompany(null)); EntityManager em = PersistenceManager.getInstance().createEntityManagerLocal(); PersistenceHelper.setEntityManager(em); try { APPLogger.getLogger().error("Inicia Servidor Contable"); ServiceManager sm = new ServiceManager(); // Inicia tareas automaticas. sm.autoamaticInit(); APPLogger.getLogger().error("Servidor Contable ok"); } catch (Exception e) { } finally { PersistenceHelper.closeSession(); } this.scheduleJObs(); } /** * Metodo que inicia la ejeciucion de tareas calendarizadas. * * @throws Exception */ private void scheduleJObs() throws Exception { this.fillThreadFacade(this.getCompany(null)); EntityManager em = PersistenceManager.getInstance().createEntityManagerLocal(); PersistenceHelper.setEntityManager(em); try { // Levanta ejecutor de tareas calendarizadas. MiaScheduler ms = new MiaScheduler(); ms.init(); } catch (Exception e) { } finally { PersistenceHelper.closeSession(); } } @PreDestroy private void detenerJobs() throws Exception{ ms.detenerJob(); } }