我正在使用Spring批量项阅读器。在Item阅读器中读取方法调用存储过程。一旦我们从存储过程中得到结果,它就会返回到writer。但它又会读取方法。所以读取是反复执行。
任何人都可以建议我如何限制read方法只调用一次作业执行。
*注意:StoredprocedureitemReader不是我们的要求。我们需要使用itemreader只读
请帮助解决此问题
答案 0 :(得分:0)
如果我理解正确,你需要阅读一次并写多次。您可以使用存储过程在第一次调用read()
方法时获取结果,并且所有剩余时间都可以逐个返回已重命名的对象。
答案 1 :(得分:0)
这是一个读取器的示例,它一次加载一个项目队列,并从同一队列中读取。这类似于一次调用存储过程和多次“写入”的用例。
package de.incompleteco.spring.batch.item;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.UUID;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import de.incompleteco.spring.batch.domain.Record;
public class SingleItemReader implements ItemReader<Record> {
private Queue<String> values = null;
public Record read() throws Exception, UnexpectedInputException,ParseException, NonTransientResourceException {
//check the queue
if (values == null) {
//load
loadQueue();
}//end if
//return
return getRecord(values.poll());
}
private Record getRecord(String value) {
//init
Record record = null;
//check for null
if (value != null) {
record = new Record();
record.setId(UUID.randomUUID());
record.setValue(value);
}//end if
//return
return record;
}
private synchronized void loadQueue() {
//generate a bunch of data and add to the queue
if (values == null) {
System.out.println("calling load of the queue");
values = new PriorityQueue<String>();
for (int i=0;i<100;i++) {
values.add("hello " + i);
}//end for
}//end if
}
}
这是示例的作者
package de.incompleteco.spring.batch.item;
import java.util.List;
import org.springframework.batch.item.ItemWriter;
import de.incompleteco.spring.batch.domain.Record;
public class SystemItemWriter implements ItemWriter<Record> {
public void write(List<? extends Record> items) throws Exception {
System.out.println("starting write...");
System.out.println(items);
System.out.println("...finished write");
}
}
批量配置如下;
<?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:batch="http://www.springframework.org/schema/batch"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<batch:job id="singleReadJob">
<batch:step id="singleReadJob.step1">
<batch:tasklet>
<batch:chunk reader="singleReader" writer="multiWriter" commit-interval="10"/>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="singleReader" class="de.incompleteco.spring.batch.item.SingleItemReader"/>
<bean id="multiWriter" class="de.incompleteco.spring.batch.item.SystemItemWriter"/>
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository"/>
<property name="taskExecutor" ref="taskExecutor"/>
</bean>
<bean id="jobExplorer"
class="org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean">
<property name="repositoryFactory" ref="&jobRepository"/>
</bean>
<task:executor id="taskExecutor"/>
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
</beans>