Java SpringBatch读写模块
我有一个包含一些for循环的程序。该程序的思想是使用多个帐户登录到一个网站并检索一个列表(每次登录都会带来不同的列表)。因此,我的设置方式是使用增强的for循环:Java SpringBatch读写模块,java,spring,spring-batch,Java,Spring,Spring Batch,我有一个包含一些for循环的程序。该程序的思想是使用多个帐户登录到一个网站并检索一个列表(每次登录都会带来不同的列表)。因此,我的设置方式是使用增强的for循环: loginsList.put( "firstUsername", "firstPassword" ); loginsList.put( "secondUsername", "secondPassword" ); loginsList.put( "thirdUsername", "thirdPassword" ); loginsList
loginsList.put( "firstUsername", "firstPassword" );
loginsList.put( "secondUsername", "secondPassword" );
loginsList.put( "thirdUsername", "thirdPassword" );
loginsList.put( "fourthUsername", "fourthPassword" );
loginsList.put( "fifthUsername", "fifthPassword" );
for ( Entry<String, String> nextLogin : logins.entrySet() ) {
String nextUser = nextLogin.getKey();
String nextPass = nextLogin.getValue();
Response authenticateUserResponse = Jsoup.connect( WEBSITE_I_NEED_TO_LOGIN_TO )
.data( "username", nextUser )
.data( "password", nextPass )
.execute();
loginsList.put(“firstUsername”、“firstPassword”);
loginsList.put(“secondUsername”、“secondPassword”);
loginsList.put(“第三个用户名”、“第三个密码”);
loginsList.put(“fourthUsername”、“fourthPassword”);
loginsList.put(“第五个用户名”、“第五个密码”);
对于(条目nextLogin:logins.entrySet()){
字符串nextUser=nextLogin.getKey();
字符串nextPass=nextLogin.getValue();
响应authenticateUserResponse=Jsoup.connect(网站需要登录)
.data(“用户名”,nextUser)
.数据(“密码”,下一步)
.execute();
基本上,我希望流程是这样的:
read()-->get list-->send list to write()方法将其写入数据库-->循环并获取下一个登录-->read()-->get list-->将其发送到write()…等等
然而,我遇到的问题是,我的循环在read方法中运行,并且在所有帐户中遍历所有列表之前不会转到write方法。实际上,write在最后只被调用一次,所以我现在遇到的是类似这样的情况(这是有缺陷的设计):
read()-->获取列表-->下一个帐户-->获取列表-->下一个帐户-->获取列表-->写入()
如何组织Spring中的数据块处理,以便在仅读取数据块后写入?for(Entry nextLogin:logins.entrySet()){
for ( Entry<String, String> nextLogin : logins.entrySet() ) {
String nextUser = nextLogin.getKey();
String nextPass = nextLogin.getValue();
//do something
......
//call write function
writeValues(x, y, z);
}
字符串nextUser=nextLogin.getKey();
字符串nextPass=nextLogin.getValue();
//做点什么
......
//调用写函数
写值(x,y,z);
}
这就是你想要的吗?
否则它看起来就像一个传统的SpringBatch:读取>处理>处理案例。
您将让您的reader=获取一条记录
Procesor>保存记录
如果没有错误,Spring batch会将您移动到下一条记录
<step id="processUpdates">
<tasklet task-executor="batchThreadPoolTaskExecutor" throttle-limit="${batch.cviscoreupdate.threadcount}">
<chunk reader="Reader" processor="ItemProcessor" writer="ItemWriter" commit-interval="${batch.commit.interval}" skip-limit="${batch.skip.limit}" >
<skippable-exception-classes>
<include class="batch.support.SkipRecordException" />
</skippable-exception-classes>
</chunk>
</tasklet>
<next on="FAILED" to="errorExit"/>
<next on="*" to="moveFilesFromWorkToDone" />
<listeners>
<listener ref="UpdateSkipListener"/>
</listeners>
</step>
<bean id="CVIScoreUpdateItemProcessor" class="com.batch.MyUpdateItemProcessor" scope="step" init-method="init" />
您需要这样一个步骤,所以您是说,如果它保存了记录,那么会自动调用编写器?是和否,就像您想做的一样,我会在处理器内保存到DB。编写器传统上会写入该记录。但是,处理器和编写器是由您编写的类。请参阅editright…在我保存之后e到写入程序运行的db,我只调用oncees,所以把它放到处理器中,处理器为记录集中的每个记录运行。