| 維度 | 關鍵特性 | 業務價值 |
|---|---|---|
| Java 5+ 基線 | 泛型、參數化類型、類型安全 API | 編譯期發現錯誤、減少強轉、代碼可讀性提升 |
| 非順序執行 | 條件分支、暫停/恢復、並行 Step | 複雜作業流建模、人工審批節點、多核利用 |
| 可伸縮性 | 遠程分區、本地分區 | 海量數據並行處理、線性擴展吞吐 |
| 註解配置 | Reader/Writer/Processor/Listener 註解化 | 零 XML、類型安全、IDE 重構友好 |
// Spring Batch 1.x
ItemReader reader = ...;
Object item = reader.read(); // 需強轉
// Spring Batch 2.0
ItemReader reader = ...;
Trade trade = reader.read(); // 類型安全
// 自定義 Reader
@Component
public class TradeItemReader implements ItemReader {
@Override
public Trade read() { ... }
}
<job id="orderJob">
<step id="validate" next="decide">...</step>
<decision id="decide" decider="orderDecider">
<next on="HIGH_VALUE" to="manualReview"/>
<next on="STANDARD" to="autoProcess"/>
<next on="FAILED" to="notifyError"/>
</decision>
<step id="manualReview" next="notify">...</step>
<step id="autoProcess" next="notify">...</step>
<step id="notifyError" next="end">...</step>
<step id="notify">...</step>
</job>
JobExecutionDecider 實現業務規則決策,支持 FAILED 狀態下繼續執行補償邏輯。
<step id="waitApproval">
<tasklet ref="waitApprovalTasklet"/>
<stop on="PAUSED" restart="waitApproval"/> <!-- 外部觸發恢復 -->
</step>
JobOperator.restart(executionId) 或重新啟動 Job 參數相同。<split id="parallelSplit" task-executor="batchTaskExecutor" next="aggregate">
<flow>
<step id="stepA" next="stepB"/>
<step id="stepB"/>
</flow>
<flow>
<step id="stepC"/>
</flow>
<flow>
<step id="stepD"/>
</flow>
</split>
獨立 Step 併行執行,需配置 TaskExecutor(如 SimpleAsyncTaskExecutor 或線程池)。
StepExecution → 發送至 Worker 節點 (JMS/AMQP) → Worker 處理 → 異步回覆結果。<step id="partitionStep">
<partition step="workerStep" partitioner="rangePartitioner">
<handler grid-size="10" task-executor="batchTaskExecutor"/>
</partition>
</step>
<bean id="rangePartitioner" class="org.springframework.batch.core.partition.support.RangePartitioner">
<property name="column" value="id"/>
<property name="dataSource" ref="dataSource"/>
<property name="table" value="orders"/>
</bean>
PartitionHandler + StepExecutionSplitter 核心接口。@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Bean
@StepScope // 晚綁定 JobParameters
public FlatFileItemReader reader(@Value("#{jobParameters['inputFile']}") Resource input) {
return new FlatFileItemReaderBuilder()
.name("tradeReader")
.resource(input)
.delimited()
.names("id", "symbol", "price", "quantity")
.targetType(Trade.class)
.build();
}
@Bean
public TradeProcessor processor() { return new TradeProcessor(); }
@Bean
public JdbcBatchItemWriter writer(DataSource ds) {
return new JdbcBatchItemWriterBuilder()
.dataSource(ds)
.sql("INSERT INTO trade (id, symbol, price, qty) VALUES (:id, :symbol, :price, :quantity)")
.beanMapped()
.build();
}
@Bean
public Step step1(StepBuilderFactory steps, ItemReader reader,
ItemProcessor processor, ItemWriter writer) {
return steps.get("step1")
.chunk(100)
.reader(reader)
.processor(processor)
.writer(writer)
.build();
}
@Bean
public Job job(JobBuilderFactory jobs, Step step1) {
return jobs.get("tradeJob")
.start(step1)
.build();
}
}
@StepScope 支持 #{jobParameters[...]} 晚綁定,解決 Job 參數注入時機問題。spring-batch-xml 命名空間,註解與 XML 可混用。read.count, write.count, filter.count, skip.read, skip.write, skip.process。Batch MBean + Micrometer/Prometheus 適配器。Spring Batch 2.0 確立了現代批處理框架基石:類型安全 API、靈活流控、水平擴展分區、聲明式註解配置。新專案直接使用 Spring Boot 3.x + Spring Batch 5.x,繼承並強化了這些核心能力(GraalVM 原生、虛擬線程、Observability 原生支援)。