簡體   English   中英

SpringBatch讀寫塊

[英]SpringBatch Reading and Writing in chunks

我有一個包含一些for循環的程序。 該程序的想法是使用多個帳戶登錄網站並檢索一個列表(每次登錄都會帶來一個不同的列表)。 所以我設置的方式是使用增強的for循環:

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();

基本上這就是我想要的流程:

read()->獲取列表---->將列表發送到write()方法以將其寫入數據庫中->循環並獲得下一次登錄信息-> read()->獲取列表- >將其發送到write()等。

但是,我遇到的問題是我的循環在read方法中運行,並且直到所有帳戶中的所有列表都經過遍歷后才進入write方法。 從本質上講,寫入操作只在最后被調用一次,所以我現在所擁有的是這樣的東西(這是有缺陷的設計):

read()--->獲取列表->下一個帳戶--->獲取列表---下一個帳戶--->獲取列表---> write()

僅讀取塊后,如何在Spring中組織塊處理以編寫?

for ( Entry<String, String> nextLogin : logins.entrySet() ) {
    String nextUser = nextLogin.getKey();
    String nextPass = nextLogin.getValue();
     //do something
       ......
     //call write function 
writeValues(x, y, z); 
}

這就是你想要的嗎? 否則,它看起來像傳統的SpringBatch:閱讀>處理>繼續進行。 您將擁有您的讀者=獲取一條記錄處理器>保存一條記錄

如果沒有錯誤,Spring批處理會將您移至下一條記錄。

    <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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM