/** * The init method is called when this plugin is initialized * <P> * @param actionServlet The struts ActionServlet * @param moduleConfig The struts ModuleConfig */ public static void startScheduler(ServletContext context) { // The Quartz Scheduler Scheduler scheduler = null; // Retrieve the factory from the ServletContext. // It will be put there by the Quartz Servlet StdSchedulerFactory factory = (StdSchedulerFactory) context.getAttribute(QuartzInitializerServlet.QUARTZ_FACTORY_KEY); try { // Retrieve the scheduler from the factory scheduler = factory.getScheduler(); scheduler.start(); } catch (Exception e) { logger.error("Error setting up scheduler: " + e.getMessage()); } sm_scheduler = scheduler; }
private StdSchedulerFactory getSchedulerFactory( ServletConfig config ) { StdSchedulerFactory schedFact = (StdSchedulerFactory) config. getServletContext(). getAttribute(QuartzInitializerServlet.QUARTZ_FACTORY_KEY); return schedFact; }
/** * The init method is called when this plugin is initialized * <P> * @param actionServlet The struts ActionServlet * @param moduleConfig The struts ModuleConfig */ public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException { // Retrieve the ServletContext ServletContext ctx = actionServlet.getServletContext(); // The Quartz Scheduler Scheduler scheduler = null; // Retrieve the factory from the ServletContext. // It will be put there by the Quartz Servlet StdSchedulerFactory factory = (StdSchedulerFactory) ctx.getAttribute(QuartzInitializerServlet.QUARTZ_FACTORY_KEY); try { // Retrieve the scheduler from the factory scheduler = factory.getScheduler(); // Start the scheduler in case, it isn't started yet if (startOnLoad != null && startOnLoad.equals(Boolean.TRUE.toString())) { scheduler.start(); } } catch (Exception e) { logger.error("Error setting up scheduler", e); } sm_scheduler = scheduler; }
public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); try { Properties props = new Properties(); props.load(getServletContext().getResourceAsStream("/WEB-INF/classes/META-INF/conf.properties")); // This is for Tomcat if (emf == null) { emf = Persistence.createEntityManagerFactory(props.getProperty("persistenceUnit")); getServletContext().setAttribute("emf", emf); } SchedulerFactory schedulerFactory = (SchedulerFactory) getServletContext().getAttribute(QuartzInitializerServlet.QUARTZ_FACTORY_KEY); Scheduler scheduler = schedulerFactory.getScheduler(); // The news fetch job JobDetail jobDetail1 = new JobDetail("MyJob1", "MyJobGroup", FetchAtomWorker.class); JobDataMap dataMap1 = new JobDataMap (); dataMap1.put("emf", emf); dataMap1.put("scontext", getServletContext()); jobDetail1.setJobDataMap (dataMap1); CronTrigger cronTrigger1 = new CronTrigger("MyTrigger1", "MyTriggerGroup"); CronExpression cexp1 = new CronExpression(CRON_EXPRESSION_1); cronTrigger1.setCronExpression(cexp1); scheduler.scheduleJob(jobDetail1, cronTrigger1); // The daily news job for Facebook JobDetail jobDetail2 = new JobDetail("MyJob2", "MyJobGroup", com.ringfulhealth.bots.facebook.SendNewsWorker.class); JobDataMap dataMap2 = new JobDataMap (); dataMap2.put("emf", emf); dataMap2.put("scontext", getServletContext()); jobDetail2.setJobDataMap (dataMap2); CronTrigger cronTrigger2 = new CronTrigger("MyTrigger2", "MyTriggerGroup"); CronExpression cexp2 = new CronExpression(CRON_EXPRESSION_2); cronTrigger2.setCronExpression(cexp2); scheduler.scheduleJob(jobDetail2, cronTrigger2); // The daily news job for Slack JobDetail jobDetail3 = new JobDetail("MyJob3", "MyJobGroup", com.ringfulhealth.bots.slack.SendNewsWorker.class); JobDataMap dataMap3 = new JobDataMap (); dataMap3.put("emf", emf); dataMap3.put("scontext", getServletContext()); jobDetail3.setJobDataMap (dataMap3); CronTrigger cronTrigger3 = new CronTrigger("MyTrigger3", "MyTriggerGroup"); CronExpression cexp3 = new CronExpression(CRON_EXPRESSION_2); cronTrigger3.setCronExpression(cexp3); scheduler.scheduleJob(jobDetail3, cronTrigger3); // always rebuild the search index FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(emf.createEntityManager()); fullTextEntityManager.createIndexer().startAndWait(); // Instantiate NewsServlet so that it sets up all static variables for the BaseServlet, which is used by the worker thread com.ringfulhealth.bots.facebook.NewsServlet ns1 = new com.ringfulhealth.bots.facebook.NewsServlet(); com.ringfulhealth.bots.slack.NewsServlet ns2 = new com.ringfulhealth.bots.slack.NewsServlet(); } catch (Exception e) { e.printStackTrace(); } }
public static Scheduler getScheduler(HttpServletRequest request) throws SchedulerException { ServletContext ctx = request.getSession().getServletContext(); StdSchedulerFactory factory = (StdSchedulerFactory) ctx.getAttribute(QuartzInitializerServlet.QUARTZ_FACTORY_KEY); return factory.getScheduler(); }