gpt4 book ai didi

java - 当我运行作业时,springBatch 中的 ItemReadListener 未运行

转载 作者:行者123 更新时间:2023-12-02 10:54:16 25 4
gpt4 key购买 nike

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

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 ");

}

}

最佳答案

监听器配置不正确。

由此,

 <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>

关于java - 当我运行作业时,springBatch 中的 ItemReadListener 未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51901246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com