@Override protected Scheduler instantiate(QuartzSchedulerResources rsrcs, QuartzScheduler qs) { Scheduler scheduler = super.instantiate(rsrcs, qs); JobStore jobStore = rsrcs.getJobStore(); if (jobStore instanceof SchedulerAware) { ((SchedulerAware) jobStore).setScheduler(scheduler); } return scheduler; }
protected Scheduler instantiate(QuartzSchedulerResources rsrcs, QuartzScheduler qs) { SchedulingContext schedCtxt = new SchedulingContext(); schedCtxt.setInstanceId(rsrcs.getInstanceId()); Scheduler scheduler = new StdScheduler(qs, schedCtxt); 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); } }
protected Scheduler instantiate(QuartzSchedulerResources rsrcs, QuartzScheduler qs) { Scheduler scheduler = new StdScheduler(qs); return scheduler; }
/** * Same as * {@link DirectSchedulerFactory#createRemoteScheduler(String rmiHost, int rmiPort)}, * with the addition of specifying the scheduler name, instance ID, and rmi * bind name. This scheduler can only be retrieved via * {@link DirectSchedulerFactory#getScheduler(String)} * * @param schedulerName * The name for the scheduler. * @param schedulerInstanceId * The instance ID for the scheduler. * @param rmiBindName * The name of the remote scheduler in the RMI repository. If null * defaults to the generated unique identifier. * @param rmiHost * The hostname for remote scheduler * @param rmiPort * Port for the remote scheduler. The default RMI port is 1099. * @throws SchedulerException * if the remote scheduler could not be reached. */ public void createRemoteScheduler(String schedulerName, String schedulerInstanceId, String rmiBindName, String rmiHost, int rmiPort) throws SchedulerException { SchedulingContext schedCtxt = new SchedulingContext(); schedCtxt.setInstanceId(schedulerInstanceId); String uid = (rmiBindName != null) ? rmiBindName : QuartzSchedulerResources.getUniqueIdentifier( schedulerName, schedulerInstanceId); RemoteScheduler remoteScheduler = new RemoteScheduler(schedCtxt, uid, rmiHost, rmiPort); SchedulerRepository schedRep = SchedulerRepository.getInstance(); schedRep.bind(remoteScheduler); }
/** * Same as * {@link DirectSchedulerFactory#createRemoteScheduler(String rmiHost, int rmiPort)}, * with the addition of specifying the scheduler name, instance ID, and rmi * bind name. This scheduler can only be retrieved via * {@link DirectSchedulerFactory#getScheduler(String)} * * @param schedulerName * The name for the scheduler. * @param schedulerInstanceId * The instance ID for the scheduler. * @param rmiBindName * The name of the remote scheduler in the RMI repository. If null * defaults to the generated unique identifier. * @param rmiHost * The hostname for remote scheduler * @param rmiPort * Port for the remote scheduler. The default RMI port is 1099. * @throws SchedulerException * if the remote scheduler could not be reached. */ public void createRemoteScheduler(String schedulerName, String schedulerInstanceId, String rmiBindName, String rmiHost, int rmiPort) throws SchedulerException { String uid = (rmiBindName != null) ? rmiBindName : QuartzSchedulerResources.getUniqueIdentifier( schedulerName, schedulerInstanceId); RemoteScheduler remoteScheduler = new RemoteScheduler(uid, rmiHost, rmiPort); SchedulerRepository schedRep = SchedulerRepository.getInstance(); schedRep.bind(remoteScheduler); initialized = true; }
private QuartzScheduler getScheduler() { Logger logger = LoggerFactory.getLogger(this.getClass()); if (scheduler == null) { try { QuartzSchedulerResources qsr = getQuartzSchedulerResources(); if (qsr.getThreadExecutor() == null) { qsr.setThreadExecutor(new DefaultThreadExecutor()); } qsr.getThreadPool().initialize(); QuartzScheduler qs = new QuartzScheduler(qsr, getDefaultIdleWaitTime(), -1); ClassLoadHelper classLoadHelper = getJobStoreClassLoadHelper(); classLoadHelper.initialize(); qsr.getJobStore().initialize(classLoadHelper, qs.getSchedulerSignaler()); qsr.getJobRunShellFactory().initialize(this); qs.initialize(); qs.setJobFactory(getInitialJobFactory()); logger.info("Quartz scheduler '" + qsr.getName()); logger.info("Quartz scheduler version: " + qs.getVersion()); scheduler = qs; } catch (Exception ex) { logger.error("Exception creating scheduler!", ex); } } return scheduler; }
/** * Called when the scheduler is created to return a QuartzSchedulerResources that * is used to configure the scheduler. This is called during scheduler creation only. * * The Quartz scheduler resource should have following set, at a minimum, to create a scheduler: * <ul> * <li>Name of the scheduler (setName)</li> * <li>Instance name (setInstanceName)</li> * <li>Job run shell factory (setJobRunShellFactory)</li> * <li>Thread executor (setThreadExecutor)</li> * <li>Job store (setJobStore)</li> * <li>Thread pool (setThreadPool)</li> * </ul> * * When the scheduler is created in the manager, the thread pool and job store will * be initialized, so no need to do so in this function. * * Below is an example of creating and returning a scheduler resource: * {@code * if (qsr == null) { * qsr = new QuartzSchedulerResources(); * qsr.setName(name); * qsr.setJobRunShellFactory(new IPOJOJobRunShellFactory()); * qsr.setThreadExecutor(new DefaultThreadExecutor()); * qsr.setBatchTimeWindow(0l); * qsr.setInstanceId(name + "_" + UUID.randomUUID().toString()); * qsr.setInterruptJobsOnShutdown(true); * qsr.setInterruptJobsOnShutdownWithWait(true); * qsr.setMaxBatchSize(1); * qsr.setJobStore(new RAMJobStore()); * threadPool = new ResizableThreadPool(initialThreadPoolSize, threadPriority); * qsr.setThreadPool(threadPool); * } * return qsr; * } * * @see #getInitialJobFactory() * @see #getJobStoreClassLoadHelper() * @return QuartzSchedulerResource scheduler configuration */ protected abstract QuartzSchedulerResources getQuartzSchedulerResources();