SpringAsBatch中的SimpleAsyncTaskExecutor与SyncTaskExecutor

时间:2012-07-04 14:34:25

标签: spring-batch

我有一个SpringBatch应用程序,我们正在尝试并行处理。在批处理中,它从表中读取并使用响应更新另一个表。如果输入表中有100条记录,则输出表也应该有100条记录。

现在,输入表中有13600条记录。当我尝试SyncTaskExecutor时,只有一个线程正在运行,输出表有13600条记录。当我尝试使用SimpleAsyncTaskExecutor时,输出表中只有900条记录。

下面的工作声明:

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

<import resource="applicationContext.xml" />


<bean id="itemReader"
    class="org.springframework.batch.item.database.JdbcCursorItemReader">
    <property name="dataSource" ref="dataSource" />
    <property name="sql" value="select REP_QMUT_KEY, DLN_DLNRNR, DLN_AFVDAT, F_IND_MEMO_DVB, MUT_MUTDAT_UM, MUT_VERWDAT_UM, MUT_SRT_MUT_UM from REP_QMUT" />
    <property name="rowMapper">
        <bean class="com.aegon.quinto.service.mapper.MutationInputRowMapper" />
    </property>
</bean>
<bean id="simpleStep"
    class="org.springframework.batch.core.step.item.SimpleStepFactoryBean">
    <property name="transactionManager" ref="transactionManager" />
    <property name="jobRepository" ref="jobRepository" />
    <property name="itemReader" ref="itemReader" />
    <property name="itemWriter" ref="itemWriter" />
    <property name="commitInterval" value="10" />
    <property name="startLimit" value="1" />
</bean>

 <bean id="itemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
    <property name="dataSource" ref="dataSource" />
    <property name="itemSqlParameterSourceProvider">
        <bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
    </property>
    <property name="sql" value="INSERT INTO MUT_TRIAL(DLNRNR, AFVDAT,  MEMO_MUTATION, MEMO_PARTICIPANT, MUTATION_DATE, PROCESSING_DATE, RUN_NR, SRT_MUT, REP_QMUT_CORTICON_KEY) VALUES (:dlnrnr,:afvDat,:memo,:participantMemo,:mutationDate,:processDate,:runNr,:mutationType,:mutationKey)" />
    </bean>

<bean id="simpleChunkListner" class="com.aegon.quinto.service.listener.SimpleChunkListener" />

<bean id="taskExecutor" class="org.springframework.core.task.SyncTaskExecutor" />
<bean id="itemProcessor" class="com.aegon.quinto.service.processor.SimpleItemProcessor" />

<!-- job id="simpleJob" xmlns="http://www.springframework.org/schema/batch">
    <step id="simpleStep">
        <tasklet>
            <chunk reader="itemReader"  writer="itemWriter"
                commit-interval="50">
            </chunk>
        </tasklet>
    </step>

</job-->

<job id="simpleJob" xmlns="http://www.springframework.org/schema/batch">
    <step id="simpleStep">
        <tasklet task-executor="taskExecutor" throttle-limit="25">
            <chunk reader="itemReader"  processor="itemProcessor" writer="itemWriter"
                commit-interval="50">
            </chunk>
        </tasklet>
    </step>

</job>


<!--  For running the BatchLauncher -->
<bean id="batchLauncher" class="com.aegon.quinto.service.BatchLauncher">
    <property name="jobLauncher" ref="jobLauncher" />
    <property name="jobRepository" ref="jobRepository" />
    <property name="job" ref="simpleJob" />
</bean>
</beans>

我正在尝试在多个线程中执行该步骤

映射器:

import java.sql.ResultSet;

import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.aegon.quinto.model.MutationInput;

公共类MutationInputRowMapper实现了RowMapper {

public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
    // TODO Auto-generated method stub
    MutationInput mutationInput = new MutationInput();
    mutationInput.setMutationKey(rs.getInt("REP_QMUT_KEY"));
    mutationInput.setDlnrnr(rs.getString("DLN_DLNRNR"));
    mutationInput.setMemo(rs.getString("F_IND_MEMO_MVM"));
    mutationInput.setParticipantMemo(rs.getString("F_IND_MEMO_DVB"));
    mutationInput.setProcessDate(rs.getInt("MUT_VERWDAT_UM"));
    mutationInput.setRunNr(new Integer("2"));
    mutationInput.setMutationType(rs.getString("MUT_SRT_MUT_UM"));

    return mutationInput;
}

}

我的总体要求如下: 我将从输入表中读取数据,使用外部服务验证数据并在输出表中更新验证响应。在输入表中,数据将采用扁平结构。即,对于学生,可以有多个考试的考试成绩。我需要在转到外部服务之前检索该参与者的所有考试结果。 由于网络延迟,与外部服务通信将成为瓶颈。因此,需要多线程。 如果有任何示例实施/任何指南,请告诉我方式。 P.S:我是SpringBatch的新手。

1 个答案:

答案 0 :(得分:1)

看起来您的一个或多个组件不是线程安全的