【发布时间】:2016-05-06 00:00:44
【问题描述】:
我们的应用程序是使用线程大小为 4 的 Spring Batch Step 分区实现的。 这意味着我们在 4 个线程中读取 800 万条记录的步骤,每个线程读取 200 万条记录。线程在代码的某个点基本上是应用程序代码中的最后一次调用等待比预期更长的时间大约 40 分钟而不是 5 到 10 分钟。
步骤配置如下(仅示例)。所有 4 个线程都已启动,但即使条件满足,某些方法语句/行(例如:println stmts)甚至都没有执行。
如果我遗漏了步骤分区配置中的任何内容,请在下面提出一些问题并需要帮助。
1) 我需要显式标记方法同步还是不需要?目前我们只是在 Java 类中编写 java 方法(stateless,即除了类中注入的 bean 引用之外没有共享字段值)。
2)如果我需要让我在spring批处理中使用的所有业务方法都必须为step分区同步,那么最好的方法是什么。
<!-- partitioner job -->
<job id="partitionJob" xmlns="http://www.springframework.org/schema/batch">
<!-- master step, 10 threads (grid-size) -->
<step id="masterStep">
<partition step="slave" partitioner="rangePartitioner">
<handler grid-size="10" task-executor="taskExecutor" />
</partition>
</step>
</job>
<!-- each thread will run this job, with different stepExecutionContext values. -->
<step id="slave" xmlns="http://www.springframework.org/schema/batch">
<tasklet>
<chunk reader="pagingItemReader" writer="flatFileItemWriter"
processor="itemProcessor" commit-interval="1" />
</tasklet>
</step>
<bean id="rangePartitioner" class="com.mkyong.partition.RangePartitioner" />
<bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
<!-- inject stepExecutionContext -->
<bean id="itemProcessor" class="com.mkyong.processor.UserProcessor" scope="step">
<property name="threadName" value="#{stepExecutionContext[name]}" />
</bean>
【问题讨论】:
-
能否为您的读写器添加配置?
-
@MichaelMinella,感谢您的回复。 reader 和 writers 配置是常规的,没有额外的参数。我唯一的问题是,如果我使用弹簧批处理范围分区,我需要使方法同步,该分区使用线程并行运行每个步骤。
-
我的应用程序代码与下面链接mkyong.com/spring-batch/spring-batch-partitioning-example中解释的方式完全相同
-
我在询问读者和作者,因为我想知道他们是否是步进范围的。如果它们是(并且可能应该用于分区),那么每个分区都有一个实例。如果不是,您将遇到问题。同步它们真的违背了多线程的目的......
-
感谢@MichaelMinella,它回答了我的问题,读者和作者是分步范围的
标签: java spring multithreading spring-batch