More on Spring Batch Reader

Spring Batch Job is composed of Reader, Processor and Writer. This post shows some more details on Reader.

Reader is the implementation of org.springframework.batch.item.Reader. I recommend to use the following implementations.

  • AmqpItemReader (org.springframework.batch.item.amqp.AmqpItemReader) : to interact with queue such as RabbitMQ
  • FlatFileItemReader (org.springframework.batch.item.file.FlatFileItemReader) : to interact with a file
  • JdbcCursorItemReader (org.springframework.batch.item.database.JdbcCursorItemReader) : to interact with a database

Or you can make a custom Reader.

I’ll show you some examples with JdbcCursorItemReader. But the tips can be applied to the other implementations.

Passing parameters

When reading a table, we need to pass parameters to the sql. JdbcCursorItemReader use JDBC sql format. Therefore, parameters are defined as “?” marker. (For example, SELECT COL1, COL2, COL3 FROM TB_SOURCE WHERE COL1 = ?)

JdbcCursorItemReader use a PreparedStatementSetter bean to set parameter. The following is a sample Reader definition.

Notice that TestReader has preparedStatementSetter  property.

The following is the sample parameter setter bean.

Parameter setter must implement org.springframework.jdbc.core.PreparedStatementSetter.

Passing parameter from outer environment

Above sample is setting parameter value at Spring context.xml. But sometimes, we need to get the value from outer environment. Spring Batch supports it with JobParameter. It can be set when we invoke Spring Batch job.

The following is modified Reader definition.

Now, intead of static value, the parameter setter is using “#{jobParameters[‘param_name‘]}” syntax.

JobParameter values are set at job invocation time. The following is a sample invocation program.

Above sample shows that JobParameters.addString(“param_name”, “param_value”) is setting the job parameter.
And also notice that ParameterSetter’s bean scope is “step”. Spring Batch is extending bean scope with “step” and “job”. (More on Spring Batch Scope) To access to JobParameter, bean’s scope must be “step” or “job”.

You can download the full sources from github.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.