1

I need to execute seven distinctive processes sequently(One after the other). The data is stored in Mysql. I am thinking of the following options, Please correct me if I am wrong, or if there is a better solution.

Requirments:

  1. Read the data from the Db, do the seven processes(datavalidation, calculation1, calculation2 ...etc.) finally, write the processed data into the DB.

  2. Need to process the data in chunks.

My solution and issues: Data read:

  1. Read the data using JdbcCursorItemReader, because this is the best performing db reader - But, the SQL is very complex , so I may have to consider a custom ItemReader using JdbcTemplate? which gives me more flexibility in handling the data.

Process:

  1. Define seven steps and chunks, share the data between the steps using databean. But, this won't be a good idea, because the data processes in chunks and after each chunk the step1 writer will create a new set of data in the databean. When this databean shared across the other steps, data integrity will be an issue.

  2. Use StepExecutionContext to share the data between steps. But this may affect the performance as this involves Batch job repository.

  3. Define only one step, with one ItemReader, and a chain of processes (the seven processes), and create one ItemWriter which writes the processed data into the DB. But, I won't be able to administrate or monitor each different processes, all will be in one step.

4

2 に答える 2

5

これorg.springframework.batch.item.support.CompositeItemProcessorは、Spring Batch Framework のすぐに使えるコンポーネントであり、2 番目のオプションと同様の要件をサポートします。これにより、次のことが可能になります。- データベース (itemreader) から読み取るための設計/ソリューションで分離を維持する - 個々のプロセッサの「懸念事項」と構成を分離し続ける - 前のプロセスに関係なく、個々のプロセッサが null を返すことによってチャンクを「シャットダウン」できるようにする

デリゲートのCompositeItemProcessorループを反復処理するため、アクション パターンに「似ています」。これは、説明したシナリオで非常に役立ちますが、チャンクの利点 (例外、再試行、コミット ポリシーなど) を活用できます。

于 2013-04-14T11:44:07.583 に答える
1

提案:

1) JdbcCursorItemReader を使用してデータを読み取ります。

すぐに使用できるすべてのコンポーネントは、ステップを再開可能にする ItemStream インターフェイスを既に実装しているため、適切な選択です。しかし、あなたが言及したように、要求が単に複雑なものであったり、私のように、再利用できるサービスや DAO を既に持っている場合もあります。

ItemReaderAdapter を使用することをお勧めします。データを取得するために呼び出すデリゲート サービスを構成できます。

    <bean id="MyReader" class="xxx.adapters.MyItemReaderAdapter">
       <property name="targetObject" ref="AnExistingDao" />
       <property name="targetMethod" value="next" />        
   </bean>

targetMethod は ItemReaders の読み取りコントラクトを尊重する必要があることに注意してください (データがない場合は null を返します)。

ジョブを再開可能にする必要がない場合は、単純に次のクラスを使用できます: org.springframework.batch.item.adapter.ItemReaderAdapter

ただし、ジョブを再開可能にする必要がある場合は、次のように独自の ItemReaderAdapter を作成できます。

public class MyItemReaderAdapter<T> extends AbstractMethodInvokingDelegator<T> implements ItemReader<T>, ItemStream {




private long currentCount = 0;

private final String CONTEXT_COUNT_KEY = "count"; 

/**
 * @return return value of the target method.
 */
public T read() throws Exception {

    super.setArguments(new Long[]{currentCount++});
    return invokeDelegateMethod();
}
@Override
public void open(ExecutionContext executionContext)
        throws ItemStreamException {
    currentCount = executionContext.getLong(CONTEXT_COUNT_KEY,0);

}


@Override
public void update(ExecutionContext executionContext) throws ItemStreamException {
    executionContext.putLong(CONTEXT_COUNT_KEY, currentCount);
    log.info("Update Stream current count : " + currentCount);
}


@Override
public void close() throws ItemStreamException {
    // TODO Auto-generated method stub

}

}

すぐに使える itemReaderAdapter は再起動できないため、ItemStream を実装する独自のものを作成するだけです

2) 7 ステップ vs 1 ステップについて。

これでcompositeProcessorを1ステップ使用します。7つのステップのオプションは、IMOの問題のみを引き起こします。

1) 7 つのステップ databean : したがって、ライターはステップ 7 まで databean でコミットします。その後、ステップ 7 ライターは実際のデータベースにコミットしようとし、エラーが発生します!!! すべてが失われ、バッチはステップ 1 から再起動する必要があります!!

2) コンテキストを使用した 7 つのステップ: 状態が Spring Batch メタデータに保存されるため、より良い可能性があります..しかし、springBatch のメタデータに大きなデータを保存することはお勧めできません!!

3)IMOへの道です。;-)

于 2013-04-15T13:50:17.237 に答える