1

I am trying to code a batch job using Spring Batch Java configuration and Quartz as the scheduler.

Relevant code snippet for java configuration :-

@Configuration
@EnableBatchProcessing
@EnableScheduling
public class BatchProcessingConfig {

@Autowired
private JobBuilderFactory jobBuilderFactory;

@Autowired
private EntityManagerFactory entityManagerFactory;

@Autowired
private StepBuilderFactory stepBuilderFactory;

    @Bean
@Qualifier("theJob")
public Job job() {
    return jobBuilderFactory.get("theJobName")
            .start(jobStep()).build();
}

@Bean
public Step jobStep() {
    StepBuilder stepBuilder = stepBuilderFactory.get("stepName");
    SimpleStepBuilder<Person, Person> chunk = stepBuilder
            .<Person, Person> chunk(10);
    return chunk.reader(personeReader())                
            .processor(jobProcessor()).build();
}

    @Bean
public JobProcessor<Person, Person> invoiceRejobProcessorminderProcessor() {
    return new JobProcessor();
}

The job has been wired the ItemReader and ItemProcessor.

I am able to successfully Junit Test the Job/Step using JobLauncherTestUtils class.

Problem :-

  1. How can I configure Quartz scheduler to run the above configured Job.

    All the samples & examples I could explore have created their own Job class by extending QuartzJobBean but I want my above configured Job to be scheduled for running.

  2. How do I unit test the scheduler functionality.

Kumar Sambhav
  • 7,503
  • 15
  • 63
  • 86

1 Answers1

0

This is how I did it, when I had such a requirement

public class Schedule {
    public Schedule()throws Exception{
       SchedulerFactory sf=new StdSchedulerFactory();
       Scheduler sched=sf.getScheduler();
       sched.start();
       JobDetail jd=new JobDetailImpl("myjob",sched.DEFAULT_GROUP,QuartzJob.class);
       SimpleTrigger st=new SimpleTriggerImpl("mytrigger",sched.DEFAULT_GROUP,new Date(),
                  null,SimpleTrigger.REPEAT_INDEFINITELY,60L*1000L);
       sched.scheduleJob(jd, st);
    }
    public static void main(String args[]){
       try{
         new Schedule();
       }catch(Exception e){}
    }
}

==================================

public class QuartzJob implements Job{

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
          String[] springConfig  = 
                {   
                    "resources\\spring-batch.xml" ,"SPRING_BATCH_JOB_NAME"
                };

            CommandLineJobRunner.main( springConfig );
            System.out.println("Done");

    }

}
Hirak
  • 3,601
  • 1
  • 22
  • 33
  • I don't have any class that implements Job interface. How should I create JobDetail instance ? – Kumar Sambhav May 13 '14 at 10:23
  • This job is not spring batch job, but a quartz job. I hope you have your spring batch job configured in your xml? Use those in the springconfig array as I have shown above – Hirak May 13 '14 at 10:27
  • Is is it even possible to use Quartz scheduler while configuring jobs as mentioned in question ? – Kumar Sambhav May 13 '14 at 10:27
  • I am not using XML config but Java config instead -@EnableBatchProcessing. – Kumar Sambhav May 13 '14 at 10:28
  • How are you running the spring batch job right now? – Hirak May 13 '14 at 10:32
  • As of now only in Junit using JobLauncherTestUtils. In future I want to run/schedule job as soon as my ApplicationContext/WebApplicationContext is up. – Kumar Sambhav May 13 '14 at 12:37
  • I am not sure, but can you check if you can put the code from JobLauncherUtils into a main method and run that in standalone? – Hirak May 13 '14 at 13:25
  • It seems I have the same problem as a topicstarter - to match Quartz job and Spring Batch job for my configuration described here http://stackoverflow.com/questions/32647018/how-should-i-use-tasklet-chunk-to-finish-job-succesfully. Why Quartz - because our app works on cluster environment, suggestion from http://stackoverflow.com/questions/26492788/spring-batch-on-clustered-environment-websphere. Could someone make a suggestion how to use Quartz job in Spring Batch? – Dmitry Adonin Sep 22 '15 at 09:23