spring batch admin ui不显示已配置的作业 [英] spring batch admin ui not showing jobs that are be configured

查看:78
本文介绍了spring batch admin ui不显示已配置的作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功将spring boot与spring-batch-admin集成在一起.我使用了spring-batch中的示例指南来设置作业.我在工作中添加了更多的tasklet.我的用户界面仅显示一项工作.我希望看到其中的三个.我还希望这三个作业具有启动/停止功能并从UI中获取作业参数.

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.

这是我的job.xml,位于src/main/resources/batch/jobs/

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>

这是我的BatchConfiguration

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;
            }
        }
    }


}

UI的屏幕截图

推荐答案

Spring Batch Admin UI仅显示作业.您无法在用户界面中看到步骤/任务集,因为它们无法单独运行.但是,在运行您的工作之后,您可以看到在该工作中执行的每个步骤的统计信息.希望这会有所帮助.

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.

这篇关于spring batch admin ui不显示已配置的作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆