Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring批处理作业参数在java配置中不工作_Java_Spring_Spring Batch_Jobs - Fatal编程技术网

Spring批处理作业参数在java配置中不工作

Spring批处理作业参数在java配置中不工作,java,spring,spring-batch,jobs,Java,Spring,Spring Batch,Jobs,我正在尝试运行一个批处理作业,在这里我想使我的SQL查询成为动态的。但我在构建代码时遇到了一个异常:“在类型为“org.springframework.beans.factory.config.BeanExpressionContext”的对象上找不到属性或字段“jobParameters”-可能不是公共的或无效的?”。下面是相同的代码片段和异常跟踪 BatchConfig @Configuration @EnableBatchProcessing public class StoreSales

我正在尝试运行一个批处理作业,在这里我想使我的SQL查询成为动态的。但我在构建代码时遇到了一个异常:“在类型为“org.springframework.beans.factory.config.BeanExpressionContext”的对象上找不到属性或字段“jobParameters”-可能不是公共的或无效的?”。下面是相同的代码片段和异常跟踪

BatchConfig

@Configuration
@EnableBatchProcessing
public class StoreSalesBatchConfiguration {

@Autowired
@Qualifier("jobBuilderFactory")
private JobBuilderFactory jobBuilderFactory;

@Autowired
@Qualifier("stepBuilderFactory")
private StepBuilderFactory stepBuilderFactory;

@Autowired
@Qualifier("jobCompletionNotificationListener")
private JobCompletionNotificationListener jobCompletionNotificationListener;

@Autowired
@Qualifier("jobLauncher")
private JobLauncher jobLauncher;

@Autowired
@Qualifier("storeSalesJob")
private Job storeSalesJob;

@Autowired
private GameStoreSalesRepository storeSalesRepository;  

@Bean
@StepScope
ItemReader<GameStoreSales> gameStoreSalesReader(@Qualifier("gdwMpsBatch") final DataSource dataSource,
        @Value("#{jobParameters[maxDate]}") String maxDate) {
    JdbcCursorItemReader<GameStoreSales> databaseReader = new JdbcCursorItemReader<>();
    databaseReader.setDataSource(dataSource);
    databaseReader.setSql(CommonConstants.STORE_SALES_QUERY);
    databaseReader.setPreparedStatementSetter(new PreparedStatementSetter() {

        @Override
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setString(1, maxDate);
        }
    });
    databaseReader.setRowMapper(new BeanPropertyRowMapper<>(GameStoreSales.class));
    return databaseReader;
}

@Bean
public GameStoreSalesProcessor gameStoreSalesProcessor() {
    return new GameStoreSalesProcessor();
}

@Bean
public ItemWriter<GameStoreSales> gameStoreSalesWriter() throws Exception {
    return new GameStoreSalesWriter();
}

@Bean
public Step gameStoreSalesStep(@Qualifier("gdwMpsBatch") final DataSource dataSource,
                            @Value("#{jobParameters[maxDate]}") String maxDate) throws Exception {
    return stepBuilderFactory.get("gameStoreSalesStep").<GameStoreSales, GameStoreSales>chunk(1000)
            .reader(gameStoreSalesReader(dataSource,maxDate)).processor(gameStoreSalesProcessor()).writer(gameStoreSalesWriter()).build();
}

@Bean(name = "storeSalesJob")
public Job storeSalesJob(Step gameStoreSalesStep) {
    return jobBuilderFactory.get("storeSalesJob").incrementer(new RunIdIncrementer())
            .listener(jobCompletionNotificationListener).flow(gameStoreSalesStep).end().build();
}

@Scheduled(cron = "*/30 * * * * *")
public void runStoreSalesJob() throws JobExecutionAlreadyRunningException, JobRestartException,
        JobInstanceAlreadyCompleteException, JobParametersInvalidException {
    String dateParam = new Date().toString();
    String maxDate = storeSalesRepository.getMaxCalDate();
    JobParameters param = new JobParametersBuilder().addString("date", dateParam)
                                                    .addString("maxDate", weekEnd)
                                                    .toJobParameters();
    try{
        jobLauncher.run(storeSalesJob, param);
    } catch(Exception e){
        e.printStackTrace();
    }
}
}

请说明未识别作业参数的原因以及配置出错的位置。

spring batchStepScope对象是特定步骤特有的对象,而不是单一对象。您可能知道,Spring中的默认bean范围是一个单例。但通过将spring批处理组件指定为StepScope,意味着spring批处理将使用spring容器为每个步骤执行实例化该组件的新实例

在执行参数后期绑定时,这通常非常有用,因为参数可以在StepContext或JobExecutionContext级别指定,并且需要替换占位符,这与您的文件名要求示例非常相似

使用StepScope的另一个有用原因是当您决定在并行步骤中重用同一组件时。如果组件管理任何内部状态,重要的是它必须基于StepScope,以便一个线程不会损害另一个线程管理的状态(例如,给定步骤的每个线程都有自己的StepScope组件实例)

尝试使用StepScope进行注释:

    @Bean
    @StepScope
    public Step gameStoreSalesStep(@Qualifier("gdwMpsBatch") final DataSource dataSource,
                                @Value("#{jobParameters[maxDate]}") String maxDate) throws Exception {
        return stepBuilderFactory.get("gameStoreSalesStep").<GameStoreSales, GameStoreSales>chunk(1000)
                .reader(gameStoreSalesReader(dataSource,maxDate)).processor(gameStoreSalesProcessor()).writer(gameStoreSalesWriter()).build();
    }
@Bean
@步进镜
公共步骤gameStoreSalesStep(@Qualifier(“gdwMpsBatch”)最终数据源数据源,
@值(“#{jobParameters[maxDate]}”)字符串maxDate)引发异常{
返回stepBuilderFactory.get(“gameStoreSalesStep”).chunk(1000)
.reader(gameStoreSalesReader(dataSource,maxDate)).processor(gameStoreSalesProcessor()).writer(gameStoreSalesWriter()).build();
}

您是否尝试过在参数名称周围添加简单的引号?比如:
@Value(“#{jobParameters['maxDate']}”)
是的,我做了。但我还是得到了同样的例外。它似乎根本无法识别jobParameters。我在前面的评论中给出的语法是正确的。有关更多详细信息,请参见此处:您的bean用
@StepScope
注释。没错。我看你的代码没有任何问题。为什么item reader bean的方法
gameStoreSalesReader
不是公共的?你有没有像其他人一样把它公之于众?@SonamBhardwaj你找到问题的根源了吗?还是一个可能的解决方案?我也面临同样的问题。
    @Bean
    @StepScope
    public Step gameStoreSalesStep(@Qualifier("gdwMpsBatch") final DataSource dataSource,
                                @Value("#{jobParameters[maxDate]}") String maxDate) throws Exception {
        return stepBuilderFactory.get("gameStoreSalesStep").<GameStoreSales, GameStoreSales>chunk(1000)
                .reader(gameStoreSalesReader(dataSource,maxDate)).processor(gameStoreSalesProcessor()).writer(gameStoreSalesWriter()).build();
    }