Spring Batch分区不适用于复合项目处理器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Batch分区不适用于复合项目处理器相关的知识,希望对你有一定的参考价值。

我有一个Spring Batch分区作业。我正在使用CompositeProcessor,从数据库中读取数据,并将这些项目保存到CopyOnWriteArrayList中。因为环境是并发的,但是我的CopyOnWriteArrayList被用于其他线程和混合信息,所以我不知道我做错的原因和原因,以及输出将它们写入每个线程的文件中的信息。

public class CustomerItemProcessor implements ItemProcessor<beangenerico,CopyOnWriteArrayList<beanCustomer>> 

 private CustomerDAO customerDAO;
 private CopyOnWriteArrayList<beanCustomer> listbean;

 public CopyOnWriteArrayList<beanCustomer> process(beangenerico rangos) throws Exception 

      listbean = customerDAO.getAccAgentes(rangos);

      if(listbean != null) 
          //customer.setId(currentCustomer.getId());
         return listbean;
      else
         return null;
         
    

我的批处理即时消息XML的配置:

<batch:job id="partitionJob" xmlns="http://www.springframework.org/schema/batch">
      <batch:step id="masterStep">
         <batch:partition step="slave" partitioner="rangePartitioner">
                 <batch:handler grid-size="10" task-executor="taskExecutor"/>
         </batch:partition>
      </batch:step>
</batch:job>

<!-- each thread will run this job, with different stepExecutionContext values. -->
<batch:step id="slave" xmlns="http://www.springframework.org/schema/batch">
              <batch:tasklet task-executor="taskExecutor" throttle-limit="1">
                 <batch:chunk reader="beaniniendreader" writer="tempRecordsWriter"  processor="completeItemProcessor" commit-interval="1" />
              </batch:tasklet>
        </batch:step>

        <bean id="taskExecutor"  class="org.springframework.core.task.SimpleAsyncTaskExecutor" />

        <bean id="rangePartitioner" class="my.package.springbatch.RangePartitioner" />  
    <bean id="beaniniendreader" class="my.package.springbatch.FormiikReader" scope="step"></bean>

    <bean id="beanprocessor" class="my.package.springbatch.FormiikProcessor" scope="step">
       <property name="accountExecutiveDao" ref="accountExecutiveDao"/>
    </bean>

    <bean id="beanprocessor2" class="my.package.springbatch.CustomerItemProcessor" scope="step">
       <property name="customerDAO" ref="customerAccDao"/>
    </bean>

    <bean id="completeItemProcessor"  class="org.springframework.batch.item.support.CompositeItemProcessor">
       <property name="delegates">
           <list>
                <ref bean="beanprocessor2"/>
                <ref bean="accItemprocessor"/>
                <ref bean="beanaccDataItem"/>
           </list>
      </property>
    </bean> 

    <bean id="tempRecordsWriter" class="my.package.springbatch.ListDelegateWriter" scope="step">
       <property name="delegate" ref="flatFileItemWriterPartition"/>
    </bean>

    <!-- csv file writer -->
    <bean id="flatFileItemWriterPartition" class="org.springframework.batch.item.file.FlatFileItemWriter"
     scope="step" >
      <property name="resource"
     value="file:csv/outputs/users.processed#stepExecutionContext[fromId]-#stepExecutionContext[toId].csv" />
         <property name="appendAllowed" value="false" />
         <property name="lineAggregator">
         <bean  class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
               <property name="delimiter" value="," />
               <property name="fieldExtractor">
               <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
    <property name="names" value="cuenta, name, purchasedPackage" />    
               </bean>
           </property>
        </bean>
    </property>
    </bean>
答案

我回过头来讨论代码的主题,建议我使用Threadlocal来存储特定于线程的数据,以使其起作用。在这里,我再次输入代码。感谢您的答复。

public class CustomerItemProcessor implements ItemProcessor<beangenerico,ThreadLocal<CopyOnWriteArrayList<beanCustomer>>> 
    private CustomerDAO customerDAO;
    private ThreadLocal<CopyOnWriteArrayList<beanCustomer>> listbean = new ThreadLocal<CopyOnWriteArrayList<beanCustomer>>();      

    public ThreadLocal<CopyOnWriteArrayList<beanCustomer>> process(beangenerico rangos) throws Exception 

            listbean.set(new CopyOnWriteArrayList<beanCustomer>());
            listbean = customerDAO.getAccAgentes(rangos);

            if(listbean != null) 

                return listbean;
             else 
                return null;
            

    

    public void setCustomerDAO(CustomerDAO customerDAO) 
            this.customerDAO = customerDAO;
    

以上是关于Spring Batch分区不适用于复合项目处理器的主要内容,如果未能解决你的问题,请参考以下文章

Spring Batch 分区处理程序错误

在 Spring Batch 中动态设置 gridSize(线程数)

Spring Batch StepScope Bean

MAX_BATCH_ROWS 不适用于雪花中的外部函数

Spring Batch 远程分区和远程分块的区别

为啥递归联合不适用于 PostgreSQL 中的复合类型