如何在表batch_step_context的COLUMN可能是FIELDshort_context中添加额外信息?

问题描述

1。问题摘要

我运行作业后,看到如图所示的表batch_step_context: screenshot of the table

FIELD short_context的美丽信息是:

{
    "batch.taskletType": "com.a.b.job.config.BundleJobConfig$$Lambda$763/0x000000080070f840","batch.stepType": "org.springframework.batch.core.step.tasklet.taskletStep"
}

如何在其中添加其他信息?

例如,在工作完成之后,我希望我的FIELD short_context像这样:

{
    "output": "hello world","batch.taskletType": "com.a.b.job.config.BundleJobConfig$$Lambda$763/0x000000080070f840","batch.stepType": "org.springframework.batch.core.step.tasklet.taskletStep"
}

如何添加诸如“输出”之类的额外信息。

2。我的代码的一部分

@Configuration
public class AJobConfig {

    private final JobBuilderFactory jobBuilderFactory;

    private final StepBuilderFactory stepBuilderFactory;

    @Autowired
    public AJobConfig(JobBuilderFactory jobBuilderFactory,StepBuilderFactory stepBuilderFactory) {
        this.jobBuilderFactory = jobBuilderFactory;
        this.stepBuilderFactory = stepBuilderFactory;
    }

    @Bean
    public Job aJob(Step aStep) {
        return jobBuilderFactory.get("aJob")
                .start(aStep)
                .build();
    }

    @Bean
    public Step aStep() {
        return stepBuilderFactory.get("aStep")
                .tasklet((contribution,chunkContext) -> {

                    System.out.println("hi");
                  
                    return RepeatStatus.FINISHED;
                })
                .build();
    }
}

解决方法

当我在此website

找到一些有用的代码时,这是一个非常简单的问题

部分有用的代码是:

...
        @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        fu.closeReader();
        stepExecution
          .getJobExecution()
          .getExecutionContext()
          .put("lines",this.lines);
        logger.debug("Lines Reader ended.");
        return ExitStatus.COMPLETED;
    }
...

这启发了我很多。

我需要做的就是在该步骤中添加一个侦听器。

这是我的aStep的最终代码:

...
@Bean
    public Step aStep() {
        return stepBuilderFactory.get("aStep")
                .tasklet((contribution,chunkContext) -> {
                    System.out.println("hi");
                  
                    return RepeatStatus.FINISHED;
                })
                .listener(new StepExecutionListener() {
                    @Override
                    public void beforeStep(StepExecution stepExecution) {

                    }

                    @Override
                    public ExitStatus afterStep(StepExecution stepExecution) {
                        stepExecution
                                .getExecutionContext()
                                .put("output","hello world");

                        return ExitStatus.COMPLETED;
                    }
                })
                .build();
    }
...

原因是我在表batch_step_execution_context中得到了想要的东西。

enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...