/** * String representation of the SchedulerJobHandle. * * @return string representation of the handleId */ @Override public String toString() { return getHandleIdString(); }
public static SchedulerJobHandle generateSchedulerJobHandle() { return new SchedulerJobHandle(UUID.randomUUID()); }
/** * Get the job handles given the job name * * @param jobname * @return A list of handles * @throws SQLException */ public List<SchedulerJobHandle> getJobsByName(String jobname) throws SQLException { String fetchSQL = "SELCET " + COLUMN_ID + " FROM " + JOB_TABLE + " WHERE " + COLUMN_JOB_NAME + "=?"; List<Object[]> result = runner.query(fetchSQL, multipleRowsHandler, jobname); List<SchedulerJobHandle> resOut = new ArrayList<>(); for (int i = 0; i < result.size(); i++) { Object[] row = result.get(i); resOut.add(SchedulerJobHandle.fromString((String) row[0])); } return resOut; }
@Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { JobDataMap data = jobExecutionContext.getMergedJobDataMap(); DateTime nominalTime = new DateTime(jobExecutionContext.getScheduledFireTime()); SchedulerJobHandle jobHandle = SchedulerJobHandle.fromString(data.getString(JOB_HANDLE)); boolean shouldWait = data.getBoolean(SHOULD_WAIT); SchedulerAlarmEvent alarmEvent = new SchedulerAlarmEvent(jobHandle, nominalTime, SchedulerAlarmEvent.EventType.SCHEDULE, null); try { notifyEventService(alarmEvent, shouldWait); if (jobExecutionContext.getNextFireTime() == null) { SchedulerAlarmEvent expireEvent = (new SchedulerAlarmEvent(jobHandle, nominalTime, SchedulerAlarmEvent.EventType.EXPIRE, null)); notifyEventService(expireEvent, shouldWait); } } catch (LensException e) { log.error("Failed to notify SchedulerAlarmEvent for jobHandle: {} and scheduleTime: {}", jobHandle.getHandleIdString(), nominalTime.toString()); throw new JobExecutionException("Failed to notify alarmEvent", e); } catch (InterruptedException e) { log.error("Job execution tread interrupted", e); } } }
@Test public void testAlarmServiceEnums() throws InterruptedException, LensException { DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-mm-dd"); DateTime start = formatter.parseDateTime("2016-03-03"); DateTime end = formatter.parseDateTime("2016-03-06"); SchedulerJobHandle jobHandle = new SchedulerJobHandle(UUID.randomUUID()); XFrequency frequency = new XFrequency(); frequency.setEnum(XFrequencyEnum.DAILY); alarmService.schedule(start, end, frequency, jobHandle.toString()); Thread.sleep(2000); // give chance to the event listener to process the data int count = 0; for (SchedulerAlarmEvent event : events) { if (event.getJobHandle().equals(jobHandle)) { count++; } } // 3 scheduled events, and one expired event. Assert.assertEquals(count, 4); DateTime expectedDate = start; Set<DateTime> actualSet = new HashSet<>(); for (SchedulerAlarmEvent e : events) { actualSet.add(e.getNominalTime()); } for (int i = 0; i < actualSet.size(); i++) { Assert.assertTrue(actualSet.contains(expectedDate)); expectedDate = expectedDate.plusDays(1); } }
@Test public void testAlarmServiceCronExpressions() throws InterruptedException, LensException { DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-mm-dd"); DateTime start = formatter.parseDateTime("2016-03-03"); DateTime end = formatter.parseDateTime("2016-03-06"); SchedulerJobHandle jobHandle = new SchedulerJobHandle(UUID.randomUUID()); System.out.println("jobHandle = " + jobHandle); XFrequency frequency = new XFrequency(); frequency.setCronExpression("0 0 12 * * ?"); alarmService.schedule(start, end, frequency, jobHandle.toString()); Thread.sleep(1000); alarmService.unSchedule(jobHandle); // Assert that the events are fired and at per second interval. assertTrue(events.size() > 1); }
/** * Inserts the Job info object into job table * * @param jobInfo * @return number of rows inserted. * @throws SQLException */ public int insertIntoJobTable(SchedulerJobInfo jobInfo) throws SQLException { String insertSQL = "INSERT INTO " + JOB_TABLE + " VALUES(?,?,?,?,?,?,?)"; JAXBElement<XJob> xmlJob = jobFactory.createJob(jobInfo.getJob()); return runner.update(insertSQL, jobInfo.getId().toString(), ToXMLString.toString(xmlJob), jobInfo.getUserName(), jobInfo.getJobState().name(), jobInfo.getCreatedOn(), jobInfo.getModifiedOn(), jobInfo.getJob().getName()); }
/** * String representation of the SchedulerJobHandle. * * @return string representation of the handleId */ @Override public String toString() { return getHandleIdString(); }
/** * From string. * * @param handle the handle for scheduler job * @return the handle for */ public static SchedulerJobHandle fromString(@NonNull String handle) { return new SchedulerJobHandle(UUID.fromString(handle)); }
for (int i = 0; i < result.size(); i++) { Object[] row = result.get(i); resOut.add(SchedulerJobHandle.fromString((String) row[0]));
@Override public String getEventId() { return jobHandle.getHandleIdString(); }
/** * From string. * * @param handle the handle for scheduler job * @return the handle for */ public static SchedulerJobHandle fromString(@NonNull String handle) { return new SchedulerJobHandle(UUID.fromString(handle)); }
/** * Gets the Job info object * * @param idStr * @return SchedulerJobInfo object corresponding to the job handle. * @throws SQLException */ public SchedulerJobInfo getSchedulerJobInfo(String idStr) throws SQLException { String fetchSQL = "SELECT * FROM " + JOB_TABLE + " WHERE " + COLUMN_ID + "=?"; List<Object[]> result = runner.query(fetchSQL, multipleRowsHandler, idStr); if (result.size() == 0) { return null; } else { Object[] jobInfo = result.get(0); SchedulerJobHandle id = SchedulerJobHandle.fromString((String) jobInfo[0]); XJob xJob = ToXMLString.valueOf((String) jobInfo[1], ObjectFactory.class); String userName = (String) jobInfo[2]; String status = (String) jobInfo[3]; long createdOn = (Long) jobInfo[4]; long modifiedOn = (Long) jobInfo[5]; SchedulerJobState jobState = SchedulerJobState.valueOf(status); return new SchedulerJobInfo(id, xJob, userName, jobState, createdOn, modifiedOn); } }
@Override public String getEventId() { return jobHandle.getHandleIdString(); }
@Test(priority = 1) public void testStoreJob() throws Exception { XJob job = getTestJob(); long currentTime = System.currentTimeMillis(); jobHandle = new SchedulerJobHandle(UUID.randomUUID()); SchedulerJobInfo info = new SchedulerJobInfo(jobHandle, job, "lens", SchedulerJobState.NEW, currentTime, currentTime); // Store the job schedulerDAO.storeJob(info); // Retrive the stored job XJob outJob = schedulerDAO.getJob(info.getId()); Assert.assertEquals(job, outJob); }
private SchedulerJobInstanceInfo parseSchedulerInstance(Object[] instanceInfo) throws SQLException { SchedulerJobInstanceHandle id = SchedulerJobInstanceHandle.fromString((String) instanceInfo[0]); SchedulerJobHandle jobId = SchedulerJobHandle.fromString((String) instanceInfo[1]); long createdOn = (Long) instanceInfo[2]; // Get the Runs sorted by run id to make sure the last on the list is the latest run. String fetchSQL = "SELECT * FROM " + JOB_INSTANCE_RUN_TABLE + " WHERE " + COLUMN_ID + "=? ORDER BY " + COLUMN_RUN_ID; List<Object[]> instanceRuns = runner.query(fetchSQL, multipleRowsHandler, (String) instanceInfo[0]); List<SchedulerJobInstanceRun> runList = processInstanceRun(instanceRuns); return new SchedulerJobInstanceInfo(id, jobId, createdOn, runList); }
/** * Gets the job definition from the store * * @param id : Job handle id. * @return XJob definition */ public XJob getJob(SchedulerJobHandle id) { try { return store.getJob(id.getHandleIdString()); } catch (SQLException e) { log.error("Error while getting the job for {}", id.getHandleIdString(), e); return null; } }
@Test(priority = 3) public void testSearchStoreJob() throws Exception { // Store more jobs with the one user and search XJob job = getTestJob(); long currentTime = System.currentTimeMillis(); SchedulerJobInfo info = new SchedulerJobInfo(SchedulerJobHandle.fromString(UUID.randomUUID().toString()), job, "lens", SchedulerJobState.NEW, currentTime, currentTime); // Store the job schedulerDAO.storeJob(info); info = new SchedulerJobInfo(SchedulerJobHandle.fromString(UUID.randomUUID().toString()), job, "lens", SchedulerJobState.NEW, currentTime, currentTime); schedulerDAO.storeJob(info); // There should be 3 jobs till now. Assert.assertEquals(schedulerDAO.getJobs("lens", null, null, SchedulerJobState.values()).size(), 3); //Assert.assertEquals(schedulerDAO.getJobs("lens", 1L, System.currentTimeMillis(), SchedulerJobState.NEW).size(), 2); Assert.assertEquals(schedulerDAO.getJobs("Alice", null, null, SchedulerJobState.NEW).size(), 0); } }
/** * Gets the Job status * * @param id : Job handle id. * @return SchedulerJobStatus of the job. */ public SchedulerJobState getJobState(SchedulerJobHandle id) { try { return store.getJobState(id.getHandleIdString()); } catch (SQLException e) { log.error("Error while getting the job status for {}", id.getHandleIdString(), e); return null; } }
/** * Fetches the SchedulerJobInfo object corresponding to handle id. * * @param id: Job handle id. * @return SchedulerJobInfo */ public SchedulerJobInfo getSchedulerJobInfo(SchedulerJobHandle id) { try { return store.getSchedulerJobInfo(id.getHandleIdString()); } catch (SQLException e) { log.error("Error while getting the job detail for {}", id.getHandleIdString(), e); return null; } }