简体   繁体   中英

How do I add parameters to a Spring Batch item reader?

I feel like this is a really basic thing, but I can't find docs on how to do it. All the documentation/examples I'm finding assumes static queries. I can do this as a static query, but I want to know how to do it with variables. I'm trying to use Spring Batch with Postgres.

What I'm looking to do is a query like this:

SELECT * from SOME_TABLE WHERE SOURCE = ? AND (EXPIRES BETWEEN ? AND ?)

I've tried various ways of write the query, eg replacing question marks with variables like :source . I'm not even sure if I'm using the correct ItemReader classes or if I need to write my own. This is my config:

@Bean
protected JdbcPagingItemReader<JpaEntitlement> itemReader(DataSource dataSource)
        throws Exception {
    JdbcPagingItemReader<JpaEntitlement> pagingItemReader = new JdbcPagingItemReader<>();
    pagingItemReader.setDataSource(dataSource);
    pagingItemReader.setPageSize(1);

    PagingQueryProvider pagingQueryProvider = createQueryProvider(dataSource);
    pagingItemReader.setQueryProvider(pagingQueryProvider);
    pagingItemReader.setRowMapper(new BeanPropertyRowMapper<>(JpaClass.class));
    return pagingItemReader;
}

private PagingQueryProvider createQueryProvider(DataSource dataSource) throws Exception {
    SqlPagingQueryProviderFactoryBean pagingQueryProvider =
            new SqlPagingQueryProviderFactoryBean();
    pagingQueryProvider.setSelectClause("*");
    pagingQueryProvider.setFromClause("FROM SOME_TABLE");
    pagingQueryProvider.setWhereClause("WHERE SOURCE = ? AND (EXPIRES between ? AND ?)");
    pagingQueryProvider.setDataSource(dataSource);
    return pagingQueryProvider.getObject();
}

I guess the ultimate question is this: Is there something included in Spring Batch to do this? If not, what should I override to add this functionality?

To add, this is something that need to process in batches, as it's going to be potentially thousands of records.

If you know the parameters at start of batch then pass parameters as Jobparamters . You can now access Jobparamters in Reader using @ StepScope . Below is sample code for accessing job parameters in reader

@Bean
protected JdbcPagingItemReader<JpaEntitlement> itemReader(@Value("#{jobParameters['someparameter']}") String someparameter DataSource dataSource)
        throws Exception {
    JdbcPagingItemReader<JpaEntitlement> pagingItemReader = new JdbcPagingItemReader<>();
    pagingItemReader.setDataSource(dataSource);
    pagingItemReader.setPageSize(1);

    PagingQueryProvider pagingQueryProvider = createQueryProvider(dataSource);
    pagingItemReader.setQueryProvider(pagingQueryProvider);
    pagingItemReader.setRowMapper(new BeanPropertyRowMapper<>(JpaClass.class));
    return pagingItemReader;
}

Hope this helps

This is similar to this question

I feel like this is a really basic thing, but I can't find docs on how to do it.

The relevant section from the reference documentation is Late Binding of Job and Step Attributes which provides code examples of how to use the JobScope and StepScope . The idea is that you can dynamically bind your query attributes in your reader lately at runtime (instead of eagerly at configuration time) either from job parameters or from the job/step execution context.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM