首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring Batch 4.0 ~ JdbcCursorItemReader方法下的代码无法在定义了@StepScope的情况下运行

Spring Batch 4.0 ~ JdbcCursorItemReader方法下的代码无法在定义了@StepScope的情况下运行
EN

Stack Overflow用户
提问于 2018-05-15 23:31:57
回答 1查看 960关注 0票数 0

我的批处理作业中定义了一个sql查询,它需要在运行时从用户获取输入。

我的批处理作业中有以下项目阅读器,定义如下

代码语言:javascript
运行
复制
@StepScope
@Bean
public JdbcCursorItemReader<QueryCount> queryCountItemReader() throws Exception {

    ListPreparedStatementSetter preparedStatementSetter = new ListPreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement pstmt) throws SQLException {
            pstmt.setString(1, "#{jobparameters[fromDate]}");
            pstmt.setString(2, "#{jobparameters[toDate]}");
            pstmt.setString(3, "#{jobparameters[fromDate]}");
            pstmt.setString(4, "#{jobparameters[toDate]}");
            pstmt.setString(5, "#{jobparameters[fromDate]}");
            pstmt.setString(6, "#{jobparameters[toDate]}");
            pstmt.setString(7, "#{jobparameters[eventType]}");
            pstmt.setString(8, "#{jobparameters[businessUnit]}");
            pstmt.setString(9, "#{jobparameters[deviceCategory]}");
            pstmt.setString(10, "#{jobparameters[numberOfSearchIds]}");
        }
    };

    JdbcCursorItemReader<QueryCount> queryCountJdbcCursorItemReader = new JdbcCursorItemReader<>();
    queryCountJdbcCursorItemReader.setDataSource(dataSource);
    queryCountJdbcCursorItemReader.setSql(sqlQuery);
    queryCountJdbcCursorItemReader.setRowMapper(new QueryCountMapper());
    queryCountJdbcCursorItemReader.setPreparedStatementSetter(preparedStatementSetter);

    int counter = 0;
    ExecutionContext executionContext = new ExecutionContext();

    queryCountJdbcCursorItemReader.open(executionContext);

    try {

        QueryCount queryCount;

        while ((queryCount = queryCountJdbcCursorItemReader.read()) != null) {

            System.out.println(queryCount.toString());

            counter++;
        }

    }catch (Exception e){
        e.printStackTrace();
    }finally {
        queryCountJdbcCursorItemReader.close();
    }

    return queryCountJdbcCursorItemReader;
}

我从应用程序类中发送作业参数,如下所示

代码语言:javascript
运行
复制
JobParameters jobParameters = new JobParametersBuilder()
                .addString("fromDate", "20180410")
                .addString("toDate", "20180410")
                .addString("eventType", "WEB")
                .addString("businessUnit", "UPT")
                .addString("numberOfSearchIds", "10")
                .toJobParameters();

        JobExecution execution = jobLauncher.run(job, jobParameters);

问题是,当我运行我的批处理作业时,queryCountItemReader()方法中的代码从未执行过,并且作业完成时没有任何错误。本质上,我试图运行的sql查询永远不会执行。如果我删除@StepScope注释,代码将会运行,但会失败并出现错误,因为它可以将从应用程序类发送的参数绑定到sql查询。我知道@StepScope是使用作业参数所必需的,但是为什么我的方法中的代码不能执行呢?

EN

回答 1

Stack Overflow用户

发布于 2018-05-16 22:41:04

通过添加@EnableBatchProcessing@EnableAutoConfiguration注释并更改项阅读器方法定义解决了此问题,如下所示。

代码语言:javascript
运行
复制
@StepScope
@Bean
public JdbcCursorItemReader<QueryCount> queryCountItemReader(@Value("#{jobParameters['fromDate']}") String fromDate,
                                                             @Value("#{jobParameters['toDate']}") String toDate,
                                                             @Value("#{jobParameters['eventType']}") String eventType,
                                                             @Value("#{jobParameters['businessUnit']}") String businessUnit,
                                                             @Value("#{jobParameters['deviceCategory']}") String deviceCategory,
                                                             @Value("#{jobParameters['numberOfSearchIds']}") String numberOfSearchIds) throws Exception {
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50354064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档