ingo@1127: package de.intevation.flys.wsplgen; ingo@1127: ingo@1127: import java.util.Collections; ingo@1127: import java.util.LinkedList; ingo@1127: import java.util.List; ingo@1127: ingo@1127: import org.apache.log4j.Logger; ingo@1127: ingo@1127: import de.intevation.flys.artifacts.model.WSPLGENJob; ingo@1127: ingo@1127: ingo@1127: public class Scheduler implements Runnable { ingo@1127: ingo@1127: public static final int MAX_WSPLGEN_PROCESSES = 1; ingo@1127: ingo@1127: ingo@1127: protected List jobs; ingo@1127: ingo@1127: ingo@1127: private static Scheduler INSTANCE; ingo@1127: ingo@1127: private static final Logger logger = Logger.getLogger(Scheduler.class); ingo@1127: ingo@1127: ingo@1127: ingo@1127: private Scheduler() { ingo@1127: jobs = Collections.synchronizedList(new LinkedList()); ingo@1127: } ingo@1127: ingo@1127: ingo@1127: public static Scheduler getInstance() { ingo@1127: if (INSTANCE == null) { ingo@1127: logger.info("Create new WSPLGEN Scheduler..."); ingo@1127: ingo@1127: INSTANCE = new Scheduler(); ingo@1127: new Thread(INSTANCE).start(); ingo@1127: } ingo@1127: ingo@1127: return INSTANCE; ingo@1127: } ingo@1127: ingo@1127: ingo@1127: public void addJob(WSPLGENJob job) { ingo@1127: synchronized(jobs) { ingo@1127: jobs.add(job); ingo@1127: ingo@1127: logger.info("New WSPLGEN job added."); ingo@1127: ingo@1127: jobs.notifyAll(); ingo@1127: } ingo@1127: } ingo@1127: ingo@1127: ingo@1127: public WSPLGENJob getJob() { ingo@1127: synchronized(jobs) { ingo@1127: if (!jobs.isEmpty()) { ingo@1127: return jobs.remove(0); ingo@1127: } ingo@1127: ingo@1127: return null; ingo@1127: } ingo@1127: } ingo@1127: ingo@1127: ingo@1127: public void run() { ingo@1127: logger.info("WSPLGEN Scheduler started."); ingo@1127: ingo@1127: for (;;) { ingo@1127: try { ingo@1127: doRun(); ingo@1127: } ingo@1127: catch (InterruptedException ie) { ingo@1127: logger.warn("Interrupt in WSPLGEN Scheduler -> restart it!"); ingo@1127: } ingo@1127: } ingo@1127: } ingo@1127: ingo@1127: ingo@1127: public void doRun() ingo@1127: throws InterruptedException ingo@1127: { ingo@1127: for (;;) { ingo@1127: final WSPLGENJob job = getJob(); ingo@1127: ingo@1127: if (job != null) { ingo@1127: logger.debug("Got new job to execute..."); ingo@1127: ingo@1127: Thread t = new Thread() { ingo@1127: public void run() { ingo@1127: JobExecutor executor = new JobExecutor(job); ingo@1127: executor.execute(); ingo@1127: } ingo@1127: }; ingo@1127: ingo@1127: t.start(); ingo@1127: t.join(); ingo@1127: } ingo@1127: else { ingo@1127: logger.info("No more jobs in Scheduler -> go sleep!"); ingo@1127: synchronized (jobs) { ingo@1127: jobs.wait(); ingo@1127: } ingo@1127: ingo@1127: logger.info("New jobs in Scheduler -> wake up!"); ingo@1127: } ingo@1127: } ingo@1127: } ingo@1127: } ingo@1127: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :