I am starting to create a project in Spring Batch
, I have created two different classes that have no relation to each other.
This is the NotificationOne.java
class:
@Slf4j
public class NotificationOne {
public NotificationDataOne {
log.info("NotificationDataOne");
}
}
This is the NotificationTwo.java
class:
@Slf4j
public class NotificationOTwo {
public NotificationDataTwo {
log.info("NotificationDataTwo");
}
}
And this is the kind of configuration that I currently have but I don't know how to complete it:
@Configuration
@EnableBatchProcessing
public class JobBatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Bean
public Job jobOne(NotificationOne notificationOne) {
return jobBuilderFactory.get("jobOne").incrementer(new RunIdIncrementer())
.listener(notificationOne).flow(null).end().build();
}
}
What I need is to be able to start both classes and configure it in the config class.
the only thing is to execute that class but I do not know how to create or modify the configuration case. Please can you tell me how can I launch the Job?
Here is an quick hello world job and how to launch it from a main
method:
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
public class MyJobConfiguration {
@Bean
public Job job(JobBuilderFactory jobs, StepBuilderFactory steps) {
return jobs.get("job")
.start(steps.get("step")
.tasklet((contribution, chunkContext) -> {
System.out.println("hello world");
return RepeatStatus.FINISHED;
})
.build())
.listener(new NotificationOne())
.build();
}
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(MyJobConfiguration.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
jobLauncher.run(job, new JobParameters());
}
static class NotificationOne implements JobExecutionListener {
@Override
public void beforeJob(JobExecution jobExecution) {
System.out.println("NotificationOne.beforeJob");
}
@Override
public void afterJob(JobExecution jobExecution) {
System.out.println("NotificationOne.afterJob");
}
}
}
This prints:
NotificationOne.beforeJob
hello world
NotificationOne.afterJob
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.