package fi.etie.ifms.jbpm; import javax.servlet.ServletException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.action.ActionServlet; import org.apache.struts.action.PlugIn; import org.apache.struts.config.ModuleConfig; import org.jbpm.JbpmConfiguration; import org.jbpm.impl.ServiceFactory; import org.jbpm.scheduler.SchedulerServer; /** * A Struts Plug-In to poll the JBPM Scheduler. On init, it starts a thread that * will repeatedly sleep, poll the scheduler and execute scheduled jobs, until * destroy method is invoked. * * @author Diego Ballve / * Digital Artefacts Europe */ public class SchedulerPlugIn implements PlugIn { /** The log */ private static final Log log = LogFactory.getLog(SchedulerPlugIn.class); /** The trigger worker thread */ private Thread triggerThread; /** Flag to signal plug-in status and control worker thread */ private boolean active = false; public void destroy() { active = false; triggerThread.interrupt(); } public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException { active = true; triggerThread = new Thread(new SchedulerTrigger()); triggerThread.start(); } /** Worker class (Runnable) */ class SchedulerTrigger implements Runnable { public ServiceFactory serviceFactory; public void run() { // start the polling server after 5 minutes (let the server boot) // after that use time to wait returned from checkJobs. long millisToWait = 300000; while(active) { sleep(millisToWait); if (!active) break; try { log.debug("triggering the server to execute jobs"); serviceFactory = JbpmConfiguration.getInstance().getServiceFactory(); SchedulerServer schedulerServer = serviceFactory.openSchedulerServer(); millisToWait = schedulerServer.checkJobs(); schedulerServer.close(); } catch (Exception e) { log.error("Exception on triggering poll scheduler", e); } } } private void sleep(long millisToWait) { try { log.debug("waiting for " + millisToWait + " milliseconds"); Thread.sleep( millisToWait ); } catch (InterruptedException e) { log.warn("Thread sleeping interrupted", e); } } } }