我已成功将spring-boot与spring-batch-admin集成在一起。我使用spring-batch中的sample guide来设置作业。我在工作中添加了几个tasklet。我的UI只显示一项工作。我希望看到其中三个。我还希望这三个作业具有启动/停止功能,并从UI中获取作业参数。
我推了整个代码here.。如果您有解决方案或改进,请随时发出拉取请求。
这是我的job.xml,位于src/main/resources/batch/jobs/
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- This is the XML way to define jobs but it will be very handy if you already have jobs like this -->
<batch:job id="FirstJob" restartable="true">
<batch:step id="firstStep">
<batch:tasklet ref="firstTasklet" start-limit="1" />
</batch:step>
</batch:job>
<bean id="firstTasklet" class="hello.FirstTasklet">
<property name="property" value="${custom-property}" />
</bean>
</beans>
这是我的BatchConfiguration
package hello;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
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.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
@Configuration
//@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
public DataSource dataSource;
// @Value("#{jobParameters['file']:sample-data.csv}")
// String filename;
// tag::readerwriterprocessor[]
@Bean
//@StepScope
public FlatFileItemReader<Person> reader() {
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
reader.setResource(new ClassPathResource("sample-data.csv"));
reader.setLineMapper(new DefaultLineMapper<Person>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[] { "firstName", "lastName" });
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
setTargetType(Person.class);
}});
}});
return reader;
}
@Bean
public PersonItemProcessor processor() {
return new PersonItemProcessor();
}
@Bean
public JdbcBatchItemWriter<Person> writer() {
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
writer.setDataSource(dataSource);
return writer;
}
// end::readerwriterprocessor[]
// tag::jobstep[]
@Bean
public Job importUserJob(JobCompletionNotificationListener listener) {
return jobBuilderFactory.get("importUserJob")
.incrementer(new RunIdIncrementer())
.listener(listener)
.flow(step1())
.end()
.build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Person, Person> chunk(10)
.reader(reader())
.processor(processor())
.writer(writer())
.build();
}
// end::jobstep[]
@Bean
@StepScope
public FailableTasklet tasklet(@Value("#{jobParameters[fail]}") Boolean failable) {
if(failable != null) {
return new FailableTasklet(failable);
}
else {
return new FailableTasklet(false);
}
}
public static class FailableTasklet implements Tasklet {
private final boolean fail;
public FailableTasklet(boolean fail) {
this.fail = fail;
}
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("Tasklet was executed");
if(fail) {
throw new RuntimeException("This exception was expected");
}
else {
return RepeatStatus.FINISHED;
}
}
}
}
UI
的屏幕截图答案 0 :(得分:0)
Spring Batch Admin UI仅显示作业。您不会在UI中看到步骤/ tasklet,因为它们无法单独运行。但是在你完成工作后,你可以看到在该工作中执行的每个步骤的统计数据。希望这会有所帮助。