spring batch admin ui not showing jobs that are be configured

一世执手 提交于 2019-12-25 08:28:57

问题


I have integrated spring boot with spring-batch-admin successfully. I used the sample guide from spring-batch for setting up jobs. I added couple of more tasklets to the jobs. my UI shows only one job. I expect to see three of them. I also the expect the three jobs to have start/stop functionalities and take job parameters from UI.

I have pushed entire code here.. please feel free to issue pull requests if you have solutions or improvements.

Here is my job.xml located in 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>

Here is my 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;
            }
        }
    }


}

screenshot of UI


回答1:


Spring Batch Admin UI displays only the Jobs. You wouldn't see steps/tasklets in the UI as they cannot be run individually. But after you run your job, you could see the stats of each step that was performed in that job. Hope this helps.



来源:https://stackoverflow.com/questions/41731264/spring-batch-admin-ui-not-showing-jobs-that-are-be-configured

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!