问题是:在读取器类中,我从数据库读取数据,而写入器类仅打印列表,但它运行了很多次。 它无法完成该过程。 我附加了读取器处理器writer和xml类。 有人建议我提供春季批处理建议,我无法理解读者和作家的概念。
我在这里错过的任何事情都可以为我推荐这份工作
这是我的xml文件:
<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"
xsi:schemaLocation = "http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<import resource="context-model.xml"/>
<!-- JobRepository and JobLauncher are configuration/setup classes -->
<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" />
</bean>
<!--THis is my reader writer and processor xml -->
<bean id="Reader" class="com.ibm.dao.Reader"/>
<bean id="Processor" class="com.ibm.dao.Processor" />
<!-- com.ibm.dao.Writer -->
<bean id="Writer_pro" class=" com.ibm.dao.Writer">
<!-- <property name="sessionFactory" ref="sessionFactory" /> -->
</bean>
<!-- Defining a job-->
<batch:job id = "CancellationLetterJob">
<!-- Defining a Step -->
<batch:step id = "step1">
<batch:tasklet>
<batch:chunk reader="Reader" processor="Processor" writer="Writer_pro" commit-interval='0' />
</batch:tasklet>
</batch:step>
</batch:job>
</beans>
****************and this is my writer class code********************:
公共类Writer实现ItemWriter {
@Override
public void write(List<? extends TPOL> pol) throws Exception {
System.out.println( pol.get(0).getId());
for(TPOL list :pol )
{
System.out.println("list");
}
return;
}
*******我的阅读器课程
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.batch.api.chunk.ItemProcessor;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
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 org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.itextpdf.text.log.SysoCounter;
import com.javatpoint.mypackage.TPOL;
import com.javatpoint.mypackage.test;
import com.javatpoint.mypackage.writer;
import com.ibm.dao.*;
public class Reader implements ItemReader<TPOL> {
public static Logger logger = Logger.getLogger(test.class);
/*public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception {
}
*/
@Override
public TPOL read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
TPOL pol= new TPOL();
//pol.setId(102);
logger.info("TPOL data fetched");
pol.setSystemid(1001);
logger.info(pol);
System.out.println("data updated in DB");
return pol;
}
***********我的处理器课程
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import com.javatpoint.mypackage.TPOL;
import com.javatpoint.mypackage.test;
import com.javatpoint.mypackage.writer;
public class Processor implements ItemProcessor<TPOL, TPOL>
{
public static Logger logger = Logger.getLogger(test.class);
@Override
public TPOL process(TPOL pol) throws Exception {
Configuration config= new AnnotationConfiguration().addAnnotatedClass(TPOL.class).configure();
SessionFactory factory = config.buildSessionFactory();
Session sf=factory.openSession();
Transaction tr=sf.beginTransaction();
logger.info(tr);
sf.save(pol);
logger.info("save"+sf.save(pol));
Criteria cr = sf.createCriteria(TPOL.class);
//cr.add(Restrictions.eq("systemid", 1000));
ArrayList<TPOL> list = (ArrayList<TPOL>) cr.list();
for(TPOL pol1: list)
{
//System.out.println("OUTPUT VALUE::"+pol1.getId()+" "+pol1.getSystemid());
System.out.println(pol1.getSystemid());
}
//System.out.println("size"+list.size());
//System.out.println("successfully saved ");
tr.commit();
sf.close();
return pol;
}
}
MY output is :
log4j:WARN No such property [maxBackupIndex] in org.apache.log4j.FileAppender.
log4j:WARN No such property [datePattern] in org.apache.log4j.FileAppender.
log4j:WARN No such property [maxFileSize] in org.apache.log4j.FileAppender.
data updated in DB
Hibernate: insert into T_S2M_MAPPING (SYS_ID) values (?)
Hibernate: select this_.ID as ID0_0_, this_.SYS_ID as SYS2_0_0_ from T_S2M_MAPPING this_
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
765
list
data updated in DB
Hibernate: insert into T_S2M_MAPPING (SYS_ID) values (?)
Hibernate: select this_.ID as ID2_0_, this_.SYS_ID as SYS2_2_0_ from T_S2M_MAPPING this_
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
766
list
data updated in DB
Hibernate: insert into T_S2M_MAPPING (SYS_ID) values (?)
Hibernate: select this_.ID as ID4_0_, this_.SYS_ID as SYS2_4_0_ from T_S2M_MAPPING this_
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
1001
767
list
data updated
它无法结束该过程。 有人可以建议我做这份工作
答案 0 :(得分:1)
当数据源用尽时,商品阅读器的合同应返回null:https://docs.spring.io/spring-batch/4.0.x/reference/html/readersAndWriters.html#itemReader
您的商品阅读器永远不会返回null,因此工作将永远不会结束。
在ItemReader#read
方法的javadoc中,实现必须必须在输入数据集的末尾返回null:https://docs.spring.io/spring-batch/4.0.x/api/org/springframework/batch/item/ItemReader.html