@Bean public SimpleJobLauncher jobLauncher() { SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); jobLauncher.setJobRepository(jobRepository); jobLauncher.setTaskExecutor(taskExecutor()); return jobLauncher; }
@Test public void testTaskExecutorRejects() throws Exception { final List<String> list = new ArrayList<String>(); jobLauncher.setTaskExecutor( new TaskExecutor() { @Override public void execute(Runnable task) { list.add("execute"); throw new TaskRejectedException("Planned failure"); } }); JobExecution jobExecution = new JobExecution(null, null); expect(jobRepository.getLastJobExecution(job.getName(), jobParameters)).andReturn(null); expect(jobRepository.createJobExecution(job.getName(), jobParameters)).andReturn(jobExecution); jobRepository.update(jobExecution); expectLastCall(); replay(jobRepository); jobLauncher.afterPropertiesSet(); try { jobLauncher.run(job, jobParameters); } finally { assertEquals(BatchStatus.FAILED, jobExecution.getStatus()); assertEquals(ExitStatus.FAILED.getExitCode(), jobExecution.getExitStatus().getExitCode()); verify(jobRepository); } assertEquals(1, list.size()); }
@Test public void testPropertyFileLoaderJob() { try { logger.debug("Begin Testing File Loader"); SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); jobLauncher.setTaskExecutor(new SyncTaskExecutor()); jobLauncher.setJobRepository(jobRepository); jobLauncherTestUtils.setJobLauncher(jobLauncher); SimpleJob job = importProductsJob(); logJobDetails(job); jobLauncherTestUtils.setJob(job); JobExecution exec = jobLauncherTestUtils.launchJob(new JobParameters()); Collection<String> stepNames = job.getStepNames(); for (String stepName : stepNames) { TaskletStep step = (TaskletStep) job.getStep(stepName); step.setTransactionManager(transactionManager); } assertTrue("Steps MUST exist", stepNames != null); assertTrue("At least ONE step MUST exist", stepNames.size() > 0); Assert.assertTrue( "Product Repository MUST have records", jobRepository.isJobInstanceExists(job.getName(), new JobParameters())); logger.debug("Job Execution Status {}", exec.getExitStatus()); logJobRepository(exec); Assert.assertEquals(BatchStatus.COMPLETED, exec.getStatus()); } catch (Exception e) { logger.error(e.getMessage(), e); Assert.fail(e.getMessage()); } }
@Test public void testTaskExecutor() throws Exception { final List<String> list = new ArrayList<String>(); jobLauncher.setTaskExecutor( new TaskExecutor() { @Override public void execute(Runnable task) { list.add("execute"); task.run(); } }); testRun(); assertEquals(1, list.size()); }
/** * This method reads all the task from an xml file and registers them with the MifosScheduler * * @param document Task configuration document * @throws TaskSystemException when something goes wrong */ @Deprecated private void registerTasksOldConfigurationFile(Document document) throws TaskSystemException { try { logger.warn( "Old format task.xml configuration file is deprecated. Please configure scheduler using spring managed beans."); NodeList rootSchedulerTasks = document.getElementsByTagName(SchedulerConstants.SCHEDULER_TASKS); Element rootNodeName = (Element) rootSchedulerTasks.item(0); NodeList collectionOfScheduledTasks = rootNodeName.getElementsByTagName(SchedulerConstants.SCHEDULER); DataSource dataSource = SessionFactoryUtils.getDataSource(StaticHibernateUtil.getSessionFactory()); SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(dataSource); JobRegistry jobRegistry = new MapJobRegistry(); this.jobLocator = jobRegistry; JdbcJobInstanceDao jobInstanceDao = new JdbcJobInstanceDao(); jobInstanceDao.setJdbcTemplate(jdbcTemplate); jobInstanceDao.setJobIncrementer( new MySQLMaxValueIncrementer(dataSource, "BATCH_JOB_SEQ", "id")); jobInstanceDao.afterPropertiesSet(); JdbcJobExecutionDao jobExecutionDao = new JdbcJobExecutionDao(); jobExecutionDao.setJdbcTemplate(jdbcTemplate); jobExecutionDao.setJobExecutionIncrementer( new MySQLMaxValueIncrementer(dataSource, "BATCH_JOB_EXECUTION_SEQ", "id")); jobExecutionDao.afterPropertiesSet(); JdbcStepExecutionDao stepExecutionDao = new JdbcStepExecutionDao(); stepExecutionDao.setJdbcTemplate(jdbcTemplate); stepExecutionDao.setStepExecutionIncrementer( new MySQLMaxValueIncrementer(dataSource, "BATCH_STEP_EXECUTION_SEQ", "id")); stepExecutionDao.afterPropertiesSet(); JdbcExecutionContextDao executionContextDao = new JdbcExecutionContextDao(); executionContextDao.setJdbcTemplate(jdbcTemplate); executionContextDao.afterPropertiesSet(); JobRepository jobRepository = new SimpleJobRepository( jobInstanceDao, jobExecutionDao, stepExecutionDao, executionContextDao); this.jobRepository = jobRepository; SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); jobLauncher.setJobRepository(jobRepository); jobLauncher.setTaskExecutor(new SyncTaskExecutor()); jobLauncher.afterPropertiesSet(); this.jobLauncher = jobLauncher; JobExplorer jobExplorer = new SimpleJobExplorer( jobInstanceDao, jobExecutionDao, stepExecutionDao, executionContextDao); this.jobExplorer = jobExplorer; Map<String, Object> jobData = new HashMap<String, Object>(); jobData.put("jobLocator", jobRegistry); jobData.put("jobLauncher", jobLauncher); jobData.put("jobExplorer", jobExplorer); jobData.put("jobRepository", jobRepository); JobRegistryBeanPostProcessor jobRegistryProcessor = new JobRegistryBeanPostProcessor(); jobRegistryProcessor.setJobRegistry(jobRegistry); ResourcelessTransactionManager transactionManager = new ResourcelessTransactionManager(); Date loanArrearsTaskInitialTime = null; Long loanArrearsTaskDelayTime = null; boolean portfolioAtRiskTaskExists = false; for (int i = 0; i < collectionOfScheduledTasks.getLength(); i++) { Element scheduledTask = (Element) collectionOfScheduledTasks.item(i); Element subNodeName1 = (Element) scheduledTask.getElementsByTagName(SchedulerConstants.TASK_CLASS_NAME).item(0); String taskName = ((Text) subNodeName1.getFirstChild()).getData().trim(); Element subNodeName2 = (Element) scheduledTask.getElementsByTagName(SchedulerConstants.INITIAL_TIME).item(0); String initialTime = ((Text) subNodeName2.getFirstChild()).getData().trim(); Element subNodeName3; String delayTime = null; if ((scheduledTask.getElementsByTagName(SchedulerConstants.DELAY_TIME)) != null) { subNodeName3 = (Element) scheduledTask.getElementsByTagName(SchedulerConstants.DELAY_TIME).item(0); if (subNodeName3.getFirstChild() != null) { delayTime = ((Text) subNodeName3.getFirstChild()).getData().trim(); } } if (Long.parseLong(delayTime) < 86400) { throw new IllegalArgumentException("Please specify the delay time >= 86400(1 day)"); } if (scheduler.getJobDetail(taskName, Scheduler.DEFAULT_GROUP) != null) { scheduler.unscheduleJob(taskName, Scheduler.DEFAULT_GROUP); } if ("LoanArrearsTask".equals(taskName)) { loanArrearsTaskInitialTime = parseInitialTime(initialTime); loanArrearsTaskDelayTime = Long.parseLong(delayTime) * 1000; continue; } if ("PortfolioAtRiskTask".equals(taskName)) { portfolioAtRiskTaskExists = true; continue; } schedule( taskName, parseInitialTime(initialTime), Long.parseLong(delayTime) * 1000, jobRegistry, jobRepository, jobData, transactionManager); } if (loanArrearsTaskInitialTime != null) { if (portfolioAtRiskTaskExists) { scheduleLoanArrearsAndPortfolioAtRisk( loanArrearsTaskInitialTime, loanArrearsTaskDelayTime, jobRegistry, jobRepository, jobData, transactionManager); } else { schedule( "LoanArrearsTask", loanArrearsTaskInitialTime, loanArrearsTaskDelayTime, jobRegistry, jobRepository, jobData, transactionManager); } } } catch (Exception e) { throw new TaskSystemException(e); } }