在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>
这是运行作业的主要方法: main.java
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();
}
}
}
此作业配置: jobPerson.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 < :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 :(得分:1)
ChunkListener
与chunk
配置有关,因此需要在chunk
标签内定义,如下所示:
<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="chunkListener"/>
</batch:listeners>
</batch:chunk>
</batch:tasklet>
</batch:step>
<batch:listeners>
<batch:listener ref="jobPersonListener" />
</batch:listeners>
</batch:job>
此外,您的PersonChunkListener
实现了javax.batch.api.chunk.listener.ChunkListener
接口,但是应该实现org.springframework.batch.core.ChunkListener
。