public static void initSched(int threadCount) { try { SimpleThreadPool threadPool = new SimpleThreadPool(threadCount, Thread.NORM_PRIORITY); threadPool.initialize(); // create the job store JobStore jobStore = new RAMJobStore(); DirectSchedulerFactory.getInstance().createScheduler(threadPool, jobStore); sched = DirectSchedulerFactory.getInstance().getScheduler(); //init heart sched SimpleThreadPool threadPoolHeart = new SimpleThreadPool(1, Thread.NORM_PRIORITY); threadPoolHeart.initialize(); JobStore jobStoreHeart = new RAMJobStore(); DirectSchedulerFactory.getInstance().createScheduler("HeartScheduler", "SIMPLE_NON_CLUSTERED", threadPoolHeart, jobStoreHeart); heartSched = DirectSchedulerFactory.getInstance().getScheduler("HeartScheduler"); } catch (Exception e) { log.error("error init sched", e); } }
@Override public void start() throws Exception { if (!started) { System.setProperty("org.terracotta.quartz.skipUpdateCheck", "true"); DirectSchedulerFactory factory = DirectSchedulerFactory.getInstance(); SimpleThreadPool threadPool = new SimpleThreadPool(this.numThreads, Thread.NORM_PRIORITY); threadPool.setThreadNamePrefix("scheduling-worker"); threadPool.initialize(); final String schedulerName = DEFAULT_SCHEDULER_NAME + ":" + this.name; factory.createScheduler(schedulerName, DEFAULT_INSTANCE_ID + ":" + this.name, threadPool, new RAMJobStore()); this.scheduler = factory.getScheduler(schedulerName); this.scheduler.getListenerManager().addTriggerListener(new TriggerListener()); this.scheduler.start(); started = true; log.info("Quartz started"); } }
private Properties getProperties() { Properties props = new Properties(); props.setProperty("org.quartz.jobStore.class", quartzConfig.getJobStore().getName()); props.setProperty("org.quartz.threadPool.threadCount", String.valueOf(quartzConfig.getThreads())); if (!quartzConfig.getJobStore().equals(RAMJobStore.class)) { props.setProperty("org.quartz.jobStore.driverDelegateClass", quartzConfig.getDbDelegate().getName()); props.setProperty("org.quartz.jobStore.dataSource", "main"); props.setProperty("org.quartz.dataSource.main.driver", dbConfig.getDriverClass()); props.setProperty("org.quartz.dataSource.main.URL", dbConfig.getUrl()); props.setProperty("org.quartz.dataSource.main.user", dbConfig.getUser()); props.setProperty("org.quartz.dataSource.main.password", dbConfig.getPassword()); } return props; }
/** * Creates an in memory job store (<code>{@link RAMJobStore}</code>) * The thread priority is set to Thread.NORM_PRIORITY * * @param maxThreads * The number of threads in the thread pool * @throws SchedulerException * if initialization failed. */ public void createVolatileScheduler(int maxThreads) throws SchedulerException { SimpleThreadPool threadPool = new SimpleThreadPool(maxThreads, Thread.NORM_PRIORITY); threadPool.initialize(); JobStore jobStore = new RAMJobStore(); this.createScheduler(threadPool, jobStore); }
public Scheduler createQuartzScheduler() throws SchedulerException { Properties properties = new Properties(); properties.setProperty("org.quartz.scheduler.skipUpdateCheck", "true"); properties.setProperty("org.quartz.threadPool.class", SimpleThreadPool.class.getName()); properties.setProperty("org.quartz.threadPool.threadCount", "1"); properties.setProperty("org.quartz.jobStore.class", RAMJobStore.class.getName()); val schedulerFactory = new StdSchedulerFactory(properties); return schedulerFactory.getScheduler(); }
@Bean public Scheduler quartzScheduler(ApplicationContext context) throws Exception { SchedulerFactoryBean factory = new SchedulerFactoryBean(); factory.setApplicationContext(context); factory.setExposeSchedulerInRepository(true); factory.setApplicationContextSchedulerContextKey(APPLICATION_CONTEXT_KEY); factory.setJobFactory(glassJobFactory); Properties properties = new Properties(); properties.setProperty("org.quartz.scheduler.skipUpdateCheck","true"); properties.setProperty("org.quartz.threadPool.class", SimpleThreadPool.class.getName()); properties.setProperty("org.quartz.threadPool.threadCount", "15"); properties.setProperty("org.quartz.threadPool.threadPriority", "4"); if (configuration().isInMemory()) { properties.setProperty("org.quartz.jobStore.class", RAMJobStore.class.getName()); } else { factory.setDataSource(dataSource()); properties.setProperty("org.quartz.jobStore.tablePrefix", configuration().getTablePrefix()); properties.setProperty("org.quartz.jobStore.isClustered", "false"); properties.setProperty("org.quartz.jobStore.driverDelegateClass", configuration().getDriverDelegateClass()); } factory.setQuartzProperties(properties); factory.afterPropertiesSet(); Scheduler scheduler = factory.getObject(); scheduler.getListenerManager().addJobListener(glassJobListener); scheduler.getListenerManager().addSchedulerListener(glassSchedulerListener); scheduler.start(); return scheduler; }
@Test public void createAndUpdateSchedule() throws Exception { final ApplicationContext mockContext = Mockito.mock(ApplicationContext.class); final VmScheduleRepository vmScheduleRepository = Mockito.mock(VmScheduleRepository.class); final VmResource mockResource = Mockito.mock(VmResource.class); final Subscription entity = this.subscriptionRepository.findOneExpected(subscription); Mockito.when(mockContext.getBean(VmScheduleRepository.class)).thenReturn(vmScheduleRepository); Mockito.when(mockContext.getBean(SecurityHelper.class)).thenReturn(Mockito.mock(SecurityHelper.class)); Mockito.when(mockContext.getBean(VmResource.class)).thenReturn(mockResource); final StdScheduler scheduler = (StdScheduler) vmSchedulerFactoryBean.getScheduler(); final QuartzScheduler qscheduler = (QuartzScheduler) FieldUtils.getField(StdScheduler.class, "sched", true).get(scheduler); final QuartzSchedulerResources resources = (QuartzSchedulerResources) FieldUtils.getField(QuartzScheduler.class, "resources", true) .get(qscheduler); final JobDetail jobDetail = scheduler.getJobDetail(scheduler.getJobKeys(GroupMatcher.anyJobGroup()).iterator().next()); // "ON" call would fail Mockito.doThrow(new RuntimeException()).when(mockResource).execute(entity, VmOperation.ON); try { // Mock the factory jobDetail.getJobDataMap().put("context", mockContext); ((RAMJobStore) resources.getJobStore()).storeJob(jobDetail, true); Assert.assertEquals(1, this.vmScheduleRepository.findAll().size()); // Schedule all operations within the next 2 seconds final String cron = "" + ((DateUtils.newCalendar().get(Calendar.SECOND) + 2) % 60) + " * * * * ?"; final int id = mockSchedule(vmScheduleRepository, resource.createSchedule(newSchedule(cron, VmOperation.OFF))); mockSchedule(vmScheduleRepository, resource.createSchedule(newSchedule(cron + " *", VmOperation.ON))); Assert.assertEquals(3, this.vmScheduleRepository.findAll().size()); // Yield for the schedules Thread.sleep(2500); // Check the executions Mockito.verify(mockResource).execute(entity, VmOperation.OFF); Mockito.verify(mockResource).execute(entity, VmOperation.ON); // Failed Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.REBOOT); Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.RESET); Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.SHUTDOWN); Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.SUSPEND); // Update the CRON and the operation final VmScheduleVo vo = newSchedule("" + ((DateUtils.newCalendar().get(Calendar.SECOND) + 2) % 60) + " * * * * ?", VmOperation.SHUTDOWN); vo.setId(id); vo.setSubscription(subscription); resource.updateSchedule(vo); Assert.assertEquals(3, this.vmScheduleRepository.findAll().size()); // Yield for the schedules Thread.sleep(2500); Mockito.verify(mockResource).execute(entity, VmOperation.SHUTDOWN); } finally { // Restore the factory's context jobDetail.getJobDataMap().put("context", applicationContext); ((RAMJobStore) resources.getJobStore()).storeJob(jobDetail, true); } }