使用 Camel 拆分 ArrayList 并最多 10 个线程并行处理每个项目。
以下是配置。
线程池配置文件设置为最大线程数 = 10。
<camel:route id="ReportsRoute">
<camel:from uri="direct:processReportsChannel" />
<camel:to uri="bean:reportRepository?method=getPendingTransactions" />
<camel:split parallelProcessing="true" executorServiceRef="ReportThreadPoolProfile">
<camel:simple>${body}</camel:simple>
<camel:doTry>
<camel:to uri="direct:processReportChannel" />
<camel:doCatch>
<camel:exception>java.lang.Exception</camel:exception>
<camel:handled>
<camel:constant>true</camel:constant>
</camel:handled>
<camel:to uri="bean:ReportRepository?method=markAsFailed"/>
<camel:wireTap uri="direct:loggingAndNotificationChannel" />
</camel:doCatch>
</camel:doTry>
</camel:split>
</camel:route>
bean:reportRepository?method=getPendingTransactions
获取 ArrayList 并传递给 Splitter。
processReportChannel
是处理物品的处理器。
Problem:作业启动时它正在启动 10 个线程,但有些线程正在拾取相同的项目。例如,如果我在 ArrayList 中有 item_no_1 到 10,则 thread_no_1 和 thread_no_2 或有时有更多线程正在拾取 item_no_2。是因为数组列表不是线程安全的并且 Splitter 不管理它吗?
我不是这方面的专家,需要帮助指出问题所在。
我使用以下(更简单的)设置进行了测试:
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route id="ReportsRoute">
<from uri="direct:start" />
<!-- By default a pool size of 10 is used. -->
<split parallelProcessing="true">
<simple>${body}</simple>
<to uri="direct:sub" />
</split>
</route>
<route>
<from uri="direct:sub"/>
<log message="Processing item ${body}" />
</route>
</camelContext>
Testing:
List<Object> list = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
list.add("And we go and go: " + (i + 1));
}
template.sendBody("direct:start", list);
通过此设置,不会对任何条目进行两次处理。因此,您的处理器中一定存在导致问题的原因,即同一个列表项被多个线程选取。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)