В настоящее время моя партия имеет статический интервал фиксации (1000). Мы попросили меня изменить его, чтобы зафиксировать, как только данные в моем плоском файле изменятся. Поэтому у меня должен быть ридер, который читает строки из плоского файла, как только он заметит, что эти данные изменились, он должен обработать прочитанные строки и записать в базу данных.
Я попытался с завершениемPolicy следующим образом:
ReceptionCompletionPolicy.java:public class ReceptionCompletionPolicy extends SingleItemPeekableItemReader<ReceptionLineFieldHelper> implements CompletionPolicy{ private ReceptionLineFieldHelper current; public boolean isComplete(RepeatContext context) { return ((ReaderRepeatContext) context).isComplete(); } public boolean isComplete(RepeatContext context, RepeatStatus result) { return ((ReaderRepeatContext) context).isComplete(); } public RepeatContext start(RepeatContext parent) { /* * Set first item of the chunk for later comparison. */ this.current = invokePeek(); return new ReaderRepeatContext(parent); } public void update(RepeatContext context) { /* * Check if the step should finish. * In this case when there are no more records to process. */ if (current == null) { context.setCompleteOnly(); } } private ReceptionLineFieldHelper invokePeek() { ReceptionLineFieldHelper peeked = null; try { peeked = peek(); } catch (Exception e) { e.printStackTrace(); } return peeked; } //custom RepeatContext protected class ReaderRepeatContext extends RepeatContextSupport{ public ReaderRepeatContext(RepeatContext parent) { super(parent); } public boolean isComplete() { ReceptionLineFieldHelper next = null; next = invokePeek(); if (next == null || !(next.getCode().equals(current.getCode()))) { current = next; return true; } return false; } } }ReceptionReader.java:public class ReceptionReader implements ItemReader { public Object read() { return this.filterFile(inputFile); } }reception.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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:batch="http://www.springframework.org/schema/batch" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd"> <!-- import config Spring générale --> <import resource="classpath*:spring/batch-spring.xml" /> <batch:job id="reception" parent="simpleJob" job-repository="jobRepository"> <!-- Déclaration step T01 --> <batch:step id="t01" parent="simpleStep"> <batch:tasklet transaction-manager="transactionManager"> <batch:chunk reader="receptionReader" writer="receptionProcessor" chunk-completion-policy="receptionCompletionPolicy" /> </batch:tasklet> </batch:step> </batch:job> <!-- ***************** * ETAPE T01 * ***************** --> <!-- reader --> <bean id="receptionReader" class="com.reception.reader.ReceptionReader"> </bean> <!-- writer --> <bean id="receptionProcessor" class="com.reception.processor.ReceptionProcessor"> </bean> <!-- completionPolicy --> <bean id="receptionCompletionPolicy" class="com.reception.completionPolicy.ReceptionCompletionPolicy"> </bean> </beans>
Видимо у меня проблема с методом completionPolicy at invokePeek(). У кого-нибудь есть идея?
Если есть другой способ достичь моей цели, он тоже будет приветствоваться.