/**
   * Schedule a job with a unix-like cron expression.
   *
   * @param job the job to schedule
   * @param cronExpression the cron expression to use. For a complete tutorial about cron
   *     expressions, please refer to <a
   *     href="http://quartz-scheduler.org/documentation/quartz-2.1.x/tutorials/crontrigger">quartz
   *     reference documentation</a>.
   */
  public void scheduleCron(final org.easybatch.core.job.Job job, final String cronExpression)
      throws JobSchedulerException {
    checkNotNull(job, "job");
    checkNotNull(cronExpression, "cronExpression");

    String executionId = job.getExecutionId();
    String jobName = JOB_NAME_PREFIX + executionId;
    String triggerName = TRIGGER_NAME_PREFIX + executionId;

    Trigger trigger =
        newTrigger()
            .withIdentity(triggerName)
            .withSchedule(cronSchedule(cronExpression))
            .forJob(jobName)
            .build();

    JobDetail jobDetail = getJobDetail(job, jobName);

    try {
      LOGGER.log(
          Level.INFO,
          "Scheduling job {0} with cron expression {1}",
          new Object[] {job, cronExpression});
      scheduler.scheduleJob(jobDetail, trigger);
    } catch (SchedulerException e) {
      throw new JobSchedulerException("Unable to schedule job " + job, e);
    }
  }
  /**
   * Schedule a job to start at a fixed point of time and repeat with interval period.
   *
   * @param job the job to schedule
   * @param startTime the start time
   * @param interval the repeat interval in seconds
   */
  public void scheduleAtWithInterval(
      final org.easybatch.core.job.Job job, final Date startTime, final int interval)
      throws JobSchedulerException {
    checkNotNull(job, "job");
    checkNotNull(startTime, "startTime");

    String executionId = job.getExecutionId();
    String jobName = JOB_NAME_PREFIX + executionId;
    String triggerName = TRIGGER_NAME_PREFIX + executionId;

    ScheduleBuilder scheduleBuilder =
        simpleSchedule().withIntervalInSeconds(interval).repeatForever();

    Trigger trigger =
        newTrigger()
            .withIdentity(triggerName)
            .startAt(startTime)
            .withSchedule(scheduleBuilder)
            .forJob(jobName)
            .build();

    JobDetail jobDetail = getJobDetail(job, jobName);

    try {
      LOGGER.log(
          Level.INFO,
          "Scheduling job {0} to start at {1} and every {2} second(s)",
          new Object[] {job, startTime, interval});
      scheduler.scheduleJob(jobDetail, trigger);
    } catch (SchedulerException e) {
      throw new JobSchedulerException("Unable to schedule job " + job, e);
    }
  }
 /**
  * Check if the given job is scheduled.
  *
  * @param job the job to check
  * @return true if the job is scheduled, false else
  * @throws JobSchedulerException thrown if an exception occurs while checking if the job is
  *     scheduled
  */
 public boolean isScheduled(final org.easybatch.core.job.Job job) throws JobSchedulerException {
   try {
     return scheduler.checkExists(
         TriggerKey.triggerKey(TRIGGER_NAME_PREFIX + job.getExecutionId()));
   } catch (SchedulerException e) {
     throw new JobSchedulerException("Unable to check if the job " + job + " is scheduled", e);
   }
 }
 /**
  * Unschedule the given job.
  *
  * @param job the job to unschedule
  * @throws JobSchedulerException thrown if an exception occurs during job unscheduling
  */
 public void unschedule(final org.easybatch.core.job.Job job) throws JobSchedulerException {
   LOGGER.log(Level.INFO, "Unscheduling job {0} ", job);
   try {
     scheduler.unscheduleJob(TriggerKey.triggerKey(TRIGGER_NAME_PREFIX + job.getExecutionId()));
   } catch (SchedulerException e) {
     throw new JobSchedulerException("Unable to unschedule job " + job, e);
   }
 }
  @SuppressWarnings("unchecked")
  @Test
  public void testBatchProcessing() throws Exception {

    Job job =
        aNewJob()
            .reader(apacheCommonCsvBatchReader)
            .processor(new RecordCollector<Batch>())
            .build();

    JobReport jobReport = job.call();
    assertThat(jobReport.getMetrics().getTotalCount()).isEqualTo(2);

    List<Batch> batches = (List<Batch>) jobReport.getResult();
    assertThat(batches).isNotNull().hasSize(2);

    Batch batch1 = batches.get(0);
    assertThat(batch1.getPayload().size()).isEqualTo(2);
    CSVRecord tweet = (CSVRecord) batch1.getPayload().get(0).getPayload();
    assertThat(tweet).isNotNull();
    assertThat(tweet.get(0)).isEqualTo("1");
    assertThat(tweet.get(1)).isEqualTo("foo");
    assertThat(tweet.get(2)).isEqualTo("hello");

    tweet = (CSVRecord) batch1.getPayload().get(1).getPayload();
    assertThat(tweet).isNotNull();
    assertThat(tweet.get(0)).isEqualTo("2");
    assertThat(tweet.get(1)).isEqualTo("bar");
    assertThat(tweet.get(2)).isEqualTo("hey");

    Batch batch2 = batches.get(1);
    assertThat(batch2.getPayload().size()).isEqualTo(1);
    tweet = (CSVRecord) batch2.getPayload().get(0).getPayload();
    assertThat(tweet).isNotNull();
    assertThat(tweet.get(0)).isEqualTo("3");
    assertThat(tweet.get(1)).isEqualTo("baz");
    assertThat(tweet.get(2)).isEqualTo("hi");
  }
  /**
   * Schedule a job to start at a fixed point of time.
   *
   * @param job the job to schedule
   * @param startTime the start time
   */
  public void scheduleAt(final org.easybatch.core.job.Job job, final Date startTime)
      throws JobSchedulerException {
    checkNotNull(job, "job");
    checkNotNull(startTime, "startTime");

    String executionId = job.getExecutionId();
    String jobName = JOB_NAME_PREFIX + executionId;
    String triggerName = TRIGGER_NAME_PREFIX + executionId;

    Trigger trigger =
        newTrigger().withIdentity(triggerName).startAt(startTime).forJob(jobName).build();

    JobDetail jobDetail = getJobDetail(job, jobName);

    try {
      LOGGER.log(Level.INFO, "Scheduling job {0} to start at {1}", new Object[] {job, startTime});
      scheduler.scheduleJob(jobDetail, trigger);
    } catch (SchedulerException e) {
      throw new JobSchedulerException("Unable to schedule job " + job, e);
    }
  }