日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Batch:多种格式输出编写器

發布時間:2023/12/3 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Batch:多种格式输出编写器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

作為Spring Batch的堅定倡導者,我一直在談論Spring Batch的概念,它為開發人員提供了一個框架,使他們可以專注于解決業務需求。 這樣,它使開發人員不必花費過多的時間來解決所有技術問題以支持該解決方案。

為了說明我的意思,我們將采用我之前編寫的Spring Batch示例之一,并針對需要的其他業務需求進行一些增強。

新問題

在我的Spring Batch系列的第三部分中,我們介紹了一個處理大型Excel文件輸出的教程。

后來確定另一個業務部門需要相同的數據,但是他們需要以管道分隔的文本文件格式(只有三個字段)輸出數據。

有兩種不同的方法可以執行此操作,但是在本示例中,我將向您展示如何快速實現自己的ItemStreamReader ,該項目將寫作委派給各個作家。

我們需要做的第一件事是創建ItemStreamReader的外殼。 我稱之為MultiFormatItemWriter 。 這是外殼的樣子:

package com.keyhole.example;import java.io.IOException; import java.util.List;import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.annotation.AfterStep; import org.springframework.batch.core.annotation.BeforeStep; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemStreamException; import org.springframework.batch.item.ItemStreamWriter; import org.springframework.batch.item.file.FlatFileItemWriter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component;import com.keyhole.example.poi.StockData;@Component("multFormatItemWriter") @Scope("step") public class MultiFormatItemWriter implements ItemStreamWriter<StockData> {@Overridepublic void write(List<? extends StockData> items) throws Exception {}@BeforeSteppublic void beforeStep(StepExecution stepExecution) {}@AfterSteppublic void afterStep(StepExecution stepExecution) throws IOException {}@Overridepublic void open(ExecutionContext executionContext) throws ItemStreamException {}@Overridepublic void update(ExecutionContext executionContext) throws ItemStreamException {}@Overridepublic void close() throws ItemStreamException {}}

接下來,我們需要對上一個示例中的現有StockDataExcelWriter進行一些調整,以使其在新的MultiFormatItemWriter作為委托工作。 我還發現,前面的示例存在一些與來自Nasdaq的數據流有關的問題。 其中一個字段的格式已更改,該示例不再起作用,因此必須先解決該問題,然后我們才能繼續。

  • 錯誤修復:將StockData上marketCap的字段類型從BigDecimal更改為String。 這些值現在在數據Feed中顯示為“ $ 14.5M”和類似的值。
  • 錯誤修復:由于數據格式已更改并且這些博客文章大多使用靜態示例,因此我在src / test / resources下的data.stock包中創建了一個名為companylist.csv的股票數據輸入文件。
  • 錯誤修復:修改了庫存數據讀取器以使用此數據文件代替實時的Nasdaq提要。
  • 從StockDataExcelWriter刪除了@Scope (“步驟”)注釋。 這是必需的,因為MultiFormatItemWriter作用域是步驟級別。
  • 從StockDataExcelWriter刪除了@BeforeStep和@AfterStep批注,因為這些方法將直接從MultiFormatItemWriter中調用。
  • 注釋了將每個記錄寫入Excel文件300次的write方法內部的for循環。 這用于大型excel文件演示,因此需要還原該示例才能再次使用。

現在,我們已經解決了StockDataExcelWriter ,我們需要解決業務所需的其他格式輸出。 第二個輸出應該在管道分隔的文本文件中,并且僅包含符號,名稱和最后銷售字段。

對于這個委托作者,我們將使用FlatFileItemWriter ,它是Spring Batch提供的許多輸出組件之一。 要使用它,這是一個非常簡單的配置更改,看起來像這樣:

<bean name="pipeDelimitedExtractFile" class="org.springframework.batch.item.file.FlatFileItemWriter"><property name="resource" value="file:/data/example/excel/extract-example.txt" /><property name="lineAggregator"><bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator"><property name="delimiter" value="|" /><property name="fieldExtractor"><bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor"><property name="names" value="symbol,name,lastSale" /></bean></property></bean></property> </bean>

由于Spring Batch具有扎根于Spring框架的基礎,因此可以輕松配置提供的FlatFileItemWriter并將Bean連接到應用程序代碼中。 在這種情況下,我們將使用提供的DelimitedLineAggregator創建FlatFileItemWriter ,將管道字符指定為定界符,并將fieldExtractor設置為使用BeanWrapperFieldExtractor 。

BeanWrapperFieldExtractor獲取發送到ItemStreamWriter的StockData記錄列表,并提取由逗號分隔的StockData bean中的字段名稱列表指定的字段。 最后,指定用于輸出的資源,在這種情況下為文件extract-example.txt,并將其寫入/ data / example / excel目錄。

現在,我們要做的就是將兩個委托編寫器連接到我們的MultiFormatItemWriter 。 確保在適當的方法中調用了委托編寫器,然后完成! 這是MultiFormatITemWriter的最終代碼清單:

package com.keyhole.example;import java.io.IOException; import java.util.List;import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.annotation.AfterStep; import org.springframework.batch.core.annotation.BeforeStep; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemStreamException; import org.springframework.batch.item.ItemStreamWriter; import org.springframework.batch.item.file.FlatFileItemWriter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component;import com.keyhole.example.poi.StockData; import com.keyhole.example.poi.StockDataExcelWriter;@Component("multFormatItemWriter") @Scope("step") public class MultiFormatItemWriter implements ItemStreamWriter<StockData> {@Autowiredprivate StockDataExcelWriter stockDataExcelWriter;@Autowired@Qualifier("pipeDelimitedExtractFile")private FlatFileItemWriter<StockData> extractWriter;@Overridepublic void write(List<? extends StockData> items) throws Exception {stockDataExcelWriter.write(items);extractWriter.write(items);}@BeforeSteppublic void beforeStep(StepExecution stepExecution) {stockDataExcelWriter.beforeStep(stepExecution);}@AfterSteppublic void afterStep(StepExecution stepExecution) throws IOException {stockDataExcelWriter.afterStep(stepExecution);}@Overridepublic void open(ExecutionContext executionContext) throws ItemStreamException {extractWriter.open(executionContext);}@Overridepublic void update(ExecutionContext executionContext) throws ItemStreamException {extractWriter.update(executionContext);}@Overridepublic void close() throws ItemStreamException {extractWriter.close();}}

如您所見,這里確實沒有太多工作要做,這就是我想指出的。 我并沒有真正顯示出使用某些內置的讀取器和寫入器可以簡單地實現某些業務解決方案。

最后的想法

現在我確實提到了解決此問題的幾種方法。 第二個將利用Spring Batch隨附的CompositeItemWriter 。 它所做的幾乎和我在這里所做的完全相同,只是它獲取了ItemWriters的列表并在實現的每個方法中循環遍歷它們。

在那種情況下,我將轉換我的StockDataExcelWriter來實現ItemStreamReader接口,并且將用CompositeItemWriter代替MultiFormatOutputWriter ,它將在作業配置xml中進行配置。 更少的代碼。

因此,我今天在本文中的觀點是表達如何使用Spring Batch提供的幾個已經實現的組件輕松解決最常見的任務和業務解決方案。

這個示例和其他示例可以在GitHub的以下位置找到: https : //github.com/jonny-hackett/batch-example 。

翻譯自: https://www.javacodegeeks.com/2016/08/spring-batch-multiple-format-output-writer.html

總結

以上是生活随笔為你收集整理的Spring Batch:多种格式输出编写器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。