2

as of now i am running spring batch with single job. then now i want to run multiple jobs which is different to each other means different functionality. in my configuration file i configured two jobs with different id and different names. now i have to run those jobs. can you please tell me how can i run. here my doubt is in my java class i have written this code for run the batch.

 @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job job;


CompositeWriter compositeWriter=new CompositeWriter();
            JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters();
            Long startTime=System.nanoTime();
            JobExecution execution = jobLauncher.run(job, jobParameters);

for other job how can i call the run method of jobLauncher.

and my configuration file is

<bean id="pagingItemReader" class="com.tcs.UserRowMapper">
    </bean>

     <job id="testJob" xmlns="http://www.springframework.org/schema/batch">
        <step id="step1">
            <tasklet>
                <chunk reader="pagingItemReader" processor="testApp" writer="itemWriter" 
                    commit-interval="1" />
            </tasklet>
        </step>
    </job> 
     <job id="testJob2" xmlns="http://www.springframework.org/schema/batch">
        <step id="step2">
            <tasklet>
                <chunk reader="itemReaderForNotification" processor="processforNoticeHeader" writer="itemUpateForNoticeHeader" 
                    commit-interval="1" />
            </tasklet>
        </step>
    </job> 
suri
  • 415
  • 1
  • 9
  • 22

1 Answers1

2

Your posted code seems incomplete; but you can just get the job from the config by its id, no ? Something like this :

ApplicationContext context = new ClassPathXmlApplicationContext(config);
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job1 = (Job) context.getBean("testJob");
Job job2 = (Job) context.getBean("testJob2");


JobExecution execution1 = jobLauncher.run(job1, new JobParameters());
System.out.println("Exit Status : " + execution1.getStatus());
JobExecution execution2 = jobLauncher.run(job2, new JobParameters());
System.out.println("Exit Status : " + execution2.getStatus());
  • in my application i configured every thing in spring configuration file. so i no need to instantiate object for jobLancher and Job. i just autowired and used thats it. – suri Mar 01 '16 at 05:25
  • thanks for your reply, and tell me one thing that is it good approach to write code like this.ApplicationContext context = new ClassPathXmlApplicationContext(config); because my application is webapplication. i am not using main method here. – suri Mar 01 '16 at 05:28