简体   繁体   中英

Interface object seen in Spring batch Job launcher? how?

public interface JobLauncher { public JobExecution run(Job job, JobParameters jp) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException; }

This is a part of spring framework JobLauncher i/f The method run has to be implemented by an implementing class.

Does "job" in run(Job job, JobParameters jp) represents an object of interface Job? but with Java's logic you cannot create object of any interface..

Please explain with an example using Job interface...

Does "job" in run(Job job, JobParameters jp) represents an object of interface Job? but with Java's logic you cannot create object of any interface..

You are right, you cannot instantiate an Interface. The reason the parameter is Job is so that you can pass the method any object that implements the interface Job . You can create multiple Job objects with each of them doing different things and use the one JobLauncher to launch them.

You can use a JobBuilderFactory to create a Job that can include many steps. A good example of this can be found here: https://spring.io/guides/gs/batch-processing/

@Bean
public Job importUserJob(JobCompletionNotificationListener listener) {
        return jobBuilderFactory.get("importUserJob")
                .incrementer(new RunIdIncrementer())
                .listener(listener)
                .flow(step1())
                .end()
                .build();
}

Using the above will create a FlowJob object in Spring which will be compatible with JobLauncher's run method as it implements Job .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM