有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java chunkListener在运行spring批处理作业时不运行

我使用springBatch作业中的checkListener和readListener作为学习springBatch的示例。但当我运行作业时,chunkListener不会运行,只有readListener与作业一起运行

personChunkListener.java:



     package sb.dbToxml;

        import javax.batch.api.chunk.listener.ChunkListener;

        public class PersonChunkListener implements ChunkListener
        {

          @Override
          public void afterChunk() throws Exception
          {
            System.out.println("after chunk ....***************");

          }

          @Override
          public void beforeChunk() throws Exception
          {
            System.out.println("before chunk ....***************");

          }

          @Override
          public void onError(Exception arg0) throws Exception
          {
            // TODO Auto-generated method stub

          }

        }

this is the configuration xml file for spring batch context 
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>

<bean id="jobPersonListener"     class="sb.dbToxml.PersonListener" />
         <bean id="personReadListener"    class="sb.dbToxml.PersonReadListener"/>
         <bean id="chunkListener"    class="sb.dbToxml.PersonChunkListener"/>


        <bean id="transactionManager"
            class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

    </beans>

以下是运行作业的主要方法: 主要的爪哇

    package sb.dbToxml;

    import org.springframework.batch.core.Job;
    import org.springframework.batch.core.JobExecution;
    import org.springframework.batch.core.JobExecutionException;
    import org.springframework.batch.core.JobParameter;
    import org.springframework.batch.core.JobParameters;
    import org.springframework.batch.core.JobParametersBuilder;
    import org.springframework.batch.core.launch.JobLauncher;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class MainDbToXml
    {

      public static void main(String[] args)
      {
        ApplicationContext context=new ClassPathXmlApplicationContext("Spring/batch/config/spring-batch-contextOriginal.xml");

        JobLauncher jobLauncher= (JobLauncher) context.getBean("jobLauncher");

        Job job=(Job) context.getBean("personJob");

        try
        {
          JobParameters param=new JobParametersBuilder().addLong("idMax", (long) 298).toJobParameters();

          JobExecution execution=jobLauncher.run(job, param);
          System.out.println("Main/try :Job Person  Exit Status "+execution.getStatus());
        }
        catch (JobExecutionException e)
        {
          System.out.println("Main /catch :Job Person  failed");
          e.printStackTrace();
        }
      }

    }

以下是作业配置: 工作人员。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">

    <!--       *******    JdbcPagingItemReader ***** -->
    <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="50"></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:chunk>
                <batch:listeners>
                 <batch:listener ref="chunkListener"/>
               </batch:listeners>
            </batch:tasklet>
            <!-- <batch:listeners>
                 <batch:listener ref="chunkListener"/>
            </batch:listeners> --> 
        </batch:step>
        <batch:listeners>
               <batch:listener ref="jobPersonListener" />
               <batch:listener ref="chunkListener"/>
        </batch:listeners> 

    </batch:job>



 </beans>

共 (0) 个答案