예제 #1
0
  /**
   * Build a step from the job provided.
   *
   * @return a new job step
   */
  public Step build() {

    JobStep step = new JobStep();
    step.setName(getName());
    super.enhance(step);
    if (job != null) {
      step.setJob(job);
    }
    if (jobParametersExtractor != null) {
      step.setJobParametersExtractor(jobParametersExtractor);
    }
    if (jobLauncher == null) {
      SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
      jobLauncher.setJobRepository(getJobRepository());
      try {
        jobLauncher.afterPropertiesSet();
      } catch (Exception e) {
        throw new StepBuilderException(e);
      }
      this.jobLauncher = jobLauncher;
    }
    step.setJobLauncher(jobLauncher);
    try {
      step.afterPropertiesSet();
    } catch (Exception e) {
      throw new StepBuilderException(e);
    }
    return step;
  }
  @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());
  }
예제 #3
0
 @Before
 public void setUp() throws Exception {
   step.setName("step");
   MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean();
   jobRepository = factory.getObject();
   step.setJobRepository(jobRepository);
   JobExecution jobExecution = jobRepository.createJobExecution("job", new JobParameters());
   stepExecution = jobExecution.createStepExecution("step");
   jobRepository.add(stepExecution);
   SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
   jobLauncher.setJobRepository(jobRepository);
   jobLauncher.afterPropertiesSet();
   step.setJobLauncher(jobLauncher);
 }
  private void run(ExitStatus exitStatus) throws Exception {
    JobExecution jobExecution = new JobExecution(null, null);

    expect(jobRepository.getLastJobExecution(job.getName(), jobParameters)).andReturn(null);
    expect(jobRepository.createJobExecution(job.getName(), jobParameters)).andReturn(jobExecution);
    replay(jobRepository);

    jobLauncher.afterPropertiesSet();
    try {
      jobLauncher.run(job, jobParameters);
    } finally {
      assertEquals(exitStatus, jobExecution.getExitStatus());
      verify(jobRepository);
    }
  }
  @Test(expected = JobParametersInvalidException.class)
  public void testRunWithValidator() throws Exception {

    job.setJobParametersValidator(
        new DefaultJobParametersValidator(new String[] {"missing-and-required"}, new String[0]));

    expect(jobRepository.getLastJobExecution(job.getName(), jobParameters)).andReturn(null);
    replay(jobRepository);

    jobLauncher.afterPropertiesSet();
    try {
      jobLauncher.run(job, jobParameters);
    } finally {
      verify(jobRepository);
    }
  }
예제 #6
0
  /**
   * 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);
    }
  }
 protected JobLauncher createJobLauncher() throws Exception {
   SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
   jobLauncher.setJobRepository(getJobRepository());
   jobLauncher.afterPropertiesSet();
   return jobLauncher;
 }
 @Test
 public void testInitialiseWithRepository() throws Exception {
   jobLauncher = new SimpleJobLauncher();
   jobLauncher.setJobRepository(jobRepository);
   jobLauncher.afterPropertiesSet(); // no error
 }