春季批。如何获取正在处理的元素的编号 [英] Spring Batch. How to get the number of the element being processed
问题描述
我们使用spring batch将XML文件的元素存储到我们的数据库中。在插入数据库时,是否可以检索正在处理的元素的数量?
We're storing the elements of a XML file into our database using spring batch. Is it possible to retrieve the number of the element being processed when inserting it into the database?
要澄清,这是我的工作配置:
To clearify, this is my job configuration:
<bean id="xmlItemReader" scope="step"
class="org.springframework.batch.item.xml.StaxEventItemReader">
<property name="fragmentRootElementName" value="person" />
<property name="resource" value="#{jobParameters[xmlPath]}" />
<property name="unmarshaller" ref="personUnmarshaller" />
</bean>
<!-- Read and map values to object, via jaxb2 -->
<bean id="personUnmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.batch.input.Person</value>
</list>
</property>
</bean>
<bean id="personItemWriter" scope="step"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource" />
<property name="sql">
<value>
<![CDATA[
insert into PERSON (
ID_XML, PERSON_ID, ITEM_NUM,
NAME, SURNAME1, SURNAME2)
values (
#{jobParameters[ID_XML]}, :personID,
#{stepExecution.writeCount}, -- This is what we want
:name, :surname1, :surname2)
]]>
</value>
</property>
<!-- It will take care matching between object property and sql name parameter -->
<property name="itemSqlParameterSourceProvider">
<bean
class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
</property>
</bean>
如您所见,我们尝试使用 stepExecution.writeCount ,但它总是返回0,也有readCount属性。
As you can see, we have try to get this number using stepExecution.writeCount, but it always returns 0, also with the readCount property.
数据库序列不是解决方案,因为它必须是当前XML文件中的数字,从0开始。
A database sequence is not the solution because it has to be the number inside the current XML file, starting with 0.
它必须独立于正在处理的块,因为自动提交。我们的自动提交是1000.如果我们有10000个元素,我们需要存储从0到9999的数字,而不是从0到999的10组。
It has to be independent of the chunk being processed because the auto-commit. Our auto-commit is 1000. If we have 10000 elements, we need to store the numbers from 0 to 9999, and not 10 groups from 0 to 999.
里面的XML文件并不重要,我们只需要对元素进行数字。
The order inside the XML file is not important, we just need to number the elements.
任何帮助?感谢
推荐答案
检查 ItemCountAware :let Person
实现这个接口并使用 Person.itemCount
属性作为sql参数值。
Check ItemCountAware: let Person
implements this interface and use Person.itemCount
property as sql parameter value.
这篇关于春季批。如何获取正在处理的元素的编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!