我运行作业时springBatch中的ItemReadListener无法运行

时间:2018-08-17 18:45:13

标签: java spring spring-batch

我在学习spring Batch时抛出一个简单的示例,其中我从数据库读取数据并将其写入xml文件。工作正常运行,但看不到放在ItemReadListener中的消息。是否在config中丢失了某些内容?谢谢你的帮助。我将放入最相关的代码,如果需要,我可以添加更多代码。

job.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:batch="http://www.springframework.org/schema/batch" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc">




            <bean id="itemReader" class="org.springframework.batch.item.database.JdbcPagingItemReader" scope="step">
      <property name="dataSource" ref="dataSource"/>
      <property name="queryProvider">
        <bean class="org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean">
          <property name="dataSource" ref="dataSource"></property>
          <property name="selectClause" value="SELECT internal_Id,individual_Id "></property>
          <property name="fromClause" value="FROM Person"></property>
          <property name="whereClause" value="where internal_Id &lt; :idMax"></property>
          <property name="sortKey" value="internal_Id"></property>
        </bean>
      </property>

      <property name="parameterValues">
        <map>
          <entry key="idMax" value="#{jobParameters['idMax']}"></entry>
        </map>
      </property>
      <property name="pageSize" value="10"></property>
      <property name="rowMapper">
        <bean class="sb.dbToxml.PersonRowMapper"></bean>
      </property>

    </bean>

    <bean id="itemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
       <property name="resource" value="file:xml/persons.xml"/>
       <property name="marshaller"  ref="personMarshaller"/>
       <property name="rootTagName" value="person"/>
    </bean>

    <bean id="personMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
       <property name="classesToBeBound">
          <value>sb.dbToxml.Person </value>
       </property>
    </bean>



     <batch:job id="personJob">
        <batch:step id="step1bb1" >
            <batch:tasklet transaction-manager="transactionManager">
                <batch:chunk reader="itemReader" writer="itemWriter"   commit-interval="10" />
            </batch:tasklet>
        </batch:step>

        <batch:listeners>
            <batch:listener ref="jobPersonListener" />
            <batch:listener ref="personReadListener"/>
        </batch:listeners> 
    </batch:job>



 </beans>

spring-batch-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
                           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <import resource="../jobs/jobPerson.xml" />
    <import resource="../config/spring-datasource.xml" />

    <!-- <context:annotation-config /> <tx:annotation-driven transaction-manager="transactionManager"/>a 
        PlatformTransactionManager is still required -->

    <!-- JobRepository and JobLauncher are configuration/setup classes -->
    <bean id="jobRepository"
        class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
        <property name="dataSource" ref="dataSourceBatch" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="databaseType" value="oracle" />
    </bean>

    <bean id="jobLauncher"
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>




    <!-- Optional JobExecutionListener to perform business logic before and 
        after the job -->
     <bean id="jobPersonListener"     class="sb.dbToxml.PersonListener" />
     <bean id="personReadListener"    class="sb.dbToxml.PersonReadListener"/>
    <!-- Step will need a transaction manager -->
    <bean id="transactionManager"
        class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

</beans>

PersonReadListener:

package sb.dbToxml;

import org.springframework.batch.core.ItemReadListener;

public class PersonReadListener implements ItemReadListener
{



  @Override
  public void beforeRead()
  {
    System.out.println("before read Person");

  }

  @Override
  public void onReadError(Exception arg0)
  {
    System.out.println("on  read Person ");

  }

  @Override
  public void afterRead(Object arg0)
  {
    System.out.println("after   read Person ");

  }

}

1 个答案:

答案 0 :(得分:1)

监听器配置不正确。

从此

 <batch:job id="personJob">
    <batch:step id="step1bb1" >
        <batch:tasklet transaction-manager="transactionManager">
            <batch:chunk reader="itemReader" writer="itemWriter"   commit-interval="10" />
        </batch:tasklet>
    </batch:step>

    <batch:listeners>
        <batch:listener ref="jobPersonListener" />
        <batch:listener ref="personReadListener"/>
    </batch:listeners> 
</batch:job>

应更改为

 <batch:job id="personJob">
    <batch:step id="step1bb1" >
        <batch:tasklet transaction-manager="transactionManager">
            <batch:chunk reader="itemReader" writer="itemWriter"   commit-interval="10" />
            <batch:listeners>
             <batch:listener ref="jobPersonListener" />
                 <batch:listener ref="personReadListener"/>
            </batch:listeners> 
        </batch:tasklet>
    </batch:step>
</batch:job>