Deciding between Spring Batch Step, Tasklet or Chunks

爱⌒轻易说出口 提交于 2019-12-03 03:17:19

Spring Batch is the perfect tool to do what you need.

The chunk oriented step let you configure how many items you want to read/process/write with the commit-interval property.

        <batch:step id="step1" next="step2">
        <batch:tasklet transaction-manager="transactionManager" start-limit="100">
            <batch:chunk reader="myReader" processor="myProcessor" writer="MyWriter" commit-interval="800" />
            <batch:listeners>
                <batch:listener ref="myListener" />
            </batch:listeners>
        </batch:tasklet>
    </batch:step>

Let say your reader will call a SELECT statement that returns 10 000 records. And you set a commit-interval=500.

MyReader will call the read() method 500 times. Let say that in reality, the reader implementation might in fact remove items from the resultSet. For each call to read(), it will also call the process() method of MyProcessor.

But it will not call the write() method of MyWriter until the commit-interval is reached.

If you look at the definition of the interface ItemWriter:

public interface ItemWriter<T> {

/**
 * Process the supplied data element. Will not be called with any null items
 * in normal operation.
 * 
 * @throws Exception if there are errors. The framework will catch the
 * exception and convert or rethrow it as appropriate.
 */
void write(List<? extends T> items) throws Exception;

}

You see that the write receive a List of items. This list will be the size of your commit-interval (or less if the end is reached)

And btw, 10 000 of records is nothing. You may consider multithreading if you have to deal with millions of records. But even then, just playing around with the sweet spot of the commit-interval value will probably be enough.

Hope it helps

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!