@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());
  }
Exemplo n.º 2
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;
  }
 @Bean
 public SimpleJobLauncher jobLauncher() {
   SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
   jobLauncher.setJobRepository(jobRepository);
   jobLauncher.setTaskExecutor(taskExecutor());
   return jobLauncher;
 }
  @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());
    }
  }
Exemplo n.º 5
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
  public void testRunRestartableJobInstanceTwice() throws Exception {
    job =
        new JobSupport("foo") {
          @Override
          public boolean isRestartable() {
            return true;
          }

          @Override
          public void execute(JobExecution execution) {
            execution.setExitStatus(ExitStatus.COMPLETED);
            return;
          }
        };

    testRun();
    reset(jobRepository);
    expect(jobRepository.getLastJobExecution(job.getName(), jobParameters))
        .andReturn(new JobExecution(new JobInstance(1L, jobParameters, job.getName())));
    expect(jobRepository.createJobExecution(job.getName(), jobParameters))
        .andReturn(new JobExecution(new JobInstance(1L, jobParameters, job.getName())));
    replay(jobRepository);
    jobLauncher.run(job, jobParameters);
    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);
    }
  }
  @Before
  public void setUp() throws Exception {

    jobLauncher = new SimpleJobLauncher();
    jobRepository = createMock(JobRepository.class);
    jobLauncher.setJobRepository(jobRepository);
  }
Exemplo n.º 10
0
  public void run()
      throws JobExecutionAlreadyRunningException, JobRestartException,
          JobInstanceAlreadyCompleteException, JobParametersInvalidException {

    JobParameters parameters =
        new JobParametersBuilder()
            .addLong("currentTime", new Long(System.currentTimeMillis()))
            .toJobParameters();

    jobLauncher.run(job, parameters);
  }
 @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());
 }
Exemplo n.º 12
0
  @Override
  public void runner() { // job runner
    log.info("RUNNER: PymkMain");
    try {
      Job job = (Job) PymkMain.getContext().getBean("pymkJob");
      JobParameters params =
          new JobParametersBuilder().addString("task", job.getName()).toJobParameters();
      jobLauncher.run(job, params);
    } catch (Exception e) {
      e.printStackTrace();
    }

    // TaskScheduler scheduler = PymkMain.getContext().getBean("scheduler", TaskScheduler.class);
    // scheduler.schedule(new Runnable() {
    // public void run() {
    // System.out.println(new Date());
    // }
    // }, new Date());

    // PymkDbSyncMain r1 = PymkMain.getContext().getBean("pymkDbSyncMain", PymkDbSyncMain.class);
    // r1.runner();
    // AddressBookMain r2 = PymkMain.getContext().getBean("addressBookMain", AddressBookMain.class);
    // r2.runner();
  }
Exemplo n.º 13
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
 }