Spring Batch –使用JavaConfig替换XML作业配置
我最近協(xié)助一個客戶啟動并運行了Spring Batch實現(xiàn)。 該團隊決定繼續(xù)為批處理作業(yè)使用基于JavaConfig的配置,而不是傳統(tǒng)的基于XML的配置。 隨著這越來越成為配置Java應(yīng)用程序的一種常用方法,我覺得是時候更新Keyhole的Spring Batch系列了 ,向您展示如何將現(xiàn)有的基于XML的Spring Batch配置轉(zhuǎn)換為新的基于JavaConfig批注的配置。
本教程將使用在我們的第二批Spring教程( https://keyholesoftware.com/2012/06/25/getting-started-with-spring-batch-part-two/ )中找到的簡單批處理作業(yè)。
房子清潔
在開始轉(zhuǎn)換過程之前,我們需要對項目進(jìn)行一些房屋清潔。
構(gòu)建基于JavaConfig的配置
現(xiàn)在,我們已經(jīng)刪除或禁用了現(xiàn)有的基于XML的配置,我們可以開始構(gòu)建基于JavaConfig的配置。 為此,我們需要創(chuàng)建一個帶有一些注釋的新類,這些注釋為配置奠定了基礎(chǔ)。
package com.keyhole.example.config;import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration;@Configuration @EnableBatchProcessing public class TickerPriceConversionConfig {@Autowiredprivate JobBuilderFactory jobs;@Autowiredprivate StepBuilderFactory steps;}@Configuration批注使Spring容器知道該類將包含一個或多個@Bean批注的方法,這些方法將在運行時進(jìn)行處理以生成Bean定義和服務(wù)請求。
@EnableBatchProcessing批注提供了用于構(gòu)建批處理作業(yè)配置的基本配置。 Spring Batch使用此注釋來設(shè)置默認(rèn)的JobRepository,JobLauncher,JobRegistry,PlatformTransactionManager,JobBuilderFactory和StepBuilderFactory。
現(xiàn)在是時候為組成批處理作業(yè)的組件添加@Bean注釋的方法了。 作為參考,我為每個bean包括了相應(yīng)的XML配置。
ItemReader配置
<bean name="tickerReader"class="org.springframework.batch.item.file.FlatFileItemReader"> <property name="resource" value="http://finance.yahoo.com/d/quotes.csv?s=XOM+IBM+JNJ+MSFT&f=snd1ol1p2" /><property name="lineMapper" ref="tickerLineMapper" /> </bean><bean name="tickerLineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper"> <property name="fieldSetMapper" ref="tickerMapper" /><property name="lineTokenizer" ref="tickerLineTokenizer" /> </bean><bean name="tickerLineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer" />@Beanpublic ItemReader<TickerData> reader() throws MalformedURLException {FlatFileItemReader<TickerData> reader = new FlatFileItemReader<TickerData>();reader.setResource(new UrlResource("http://finance.yahoo.com/d/quotes.csv?s=XOM+IBM+JNJ+MSFT&f=snd1ol1p2"));reader.setLineMapper(new DefaultLineMapper<TickerData>() {{setLineTokenizer(new DelimitedLineTokenizer());setFieldSetMapper(new TickerFieldSetMapper());}});return reader;}ItemProcessor和ItemWriter之前使用Spring容器的@Component批注來拾取bean并將其加載到應(yīng)用程序上下文中。
@Beanpublic ItemProcessor<TickerData, TickerData> processor() {return new TickerPriceProcessor();}@Beanpublic ItemWriter<TickerData> writer() {return new LogItemWriter();}現(xiàn)在我們已經(jīng)定義了Spring bean,我們可以創(chuàng)建表示步驟和工作的@Bean注釋方法。 作為參考,我包括了相應(yīng)的XML配置。
<batch:job id="TickerPriceConversion"><batch:step id="convertPrice"><batch:tasklet transaction-manager="transactionManager"><batch:chunk reader="tickerReader"processor="tickerPriceProcessor"writer="tickerWriter" commit-interval="10" /></batch:tasklet></batch:step></batch:job>@Beanpublic Job TickerPriceConversion() throws MalformedURLException {return jobs.get("TickerPriceConversion").start(convertPrice()).build();}@Beanpublic Step convertPrice() throws MalformedURLException {return steps.get("convertPrice").<TickerData, TickerData> chunk(5).reader(reader()).processor(processor()).writer(writer()).build();}我將在文章末尾包含TickerPriceConversionConfig類的完整代碼,以供參考,但基本上,這就是全部!
一旦定義了Spring bean并使用JobBuilderFactory和StepBuilderFactory為批處理作業(yè)和步驟創(chuàng)建Bean配置,就可以運行該作業(yè)并測試配置了。 為了運行作業(yè),我們將利用Spring Boot來測試新轉(zhuǎn)換的作業(yè)配置的執(zhí)行情況。 為此,我們將在測試包中創(chuàng)建一個名為TickerPriceConversionJobRunner的新類。
源代碼如下所示:
package com.keyhole.example;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class TickerPriceConversionJobRunner {public static void main(String[] args) {SpringApplication.run(TickerPriceConversionJobRunner.class, args);}}@SpringBootApplication注釋本質(zhì)上是一個便捷注釋,它提供了通常可以使用@ Configuration,@ EnableAutoConfiguration和@ComponentScan獲得的功能。 TickerPriceConversionJobRunner是一個簡單的Java應(yīng)用程序,它將主要方法處理委托給Spring Boot的SpringApplication類來運行該應(yīng)用程序。
現(xiàn)在,您可以將該項目導(dǎo)出為jar并從命令行運行TickerPriceConversionJobRunner,或者,如果您想在Spring STS中運行它,則可以右鍵單擊該類,然后選擇Run As→Spring Boot Application。
最終想法和代碼清單
如您所見,創(chuàng)建Spring Batch作業(yè)配置并不需要很多工作,但是如果您決定將所有現(xiàn)有的作業(yè)從基于XML的配置轉(zhuǎn)換為較新的基于JavaConfig的配置,擺在您前面的工作。 大部分工作將花費大量時間來充分回歸測試轉(zhuǎn)換后的批處理作業(yè)。
如果您擁有大量的Spring Batch作業(yè)庫,那將值得嗎? 可能不是,但是如果您剛開始使用或擁有可管理的批處理庫,那么這絕對是我會采用的方法。
TickerPriceConversionConfig的代碼清單
package com.keyhole.example.config;import java.net.MalformedURLException;import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.item.ItemProcessor; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemWriter; import org.springframework.batch.item.file.FlatFileItemReader; import org.springframework.batch.item.file.mapping.DefaultLineMapper; import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.UrlResource;import com.keyhole.example.LogItemWriter; import com.keyhole.example.TickerData; import com.keyhole.example.TickerFieldSetMapper; import com.keyhole.example.TickerPriceProcessor;@Configuration @EnableBatchProcessing public class TickerPriceConversionConfig {@Autowiredprivate JobBuilderFactory jobs;@Autowiredprivate StepBuilderFactory steps;@Beanpublic ItemReader<TickerData> reader() throws MalformedURLException {FlatFileItemReader<TickerData> reader = new FlatFileItemReader<TickerData>();reader.setResource(new UrlResource("http://finance.yahoo.com/d/quotes.csv?s=XOM+IBM+JNJ+MSFT&f=snd1ol1p2"));reader.setLineMapper(new DefaultLineMapper<TickerData>() {{setLineTokenizer(new DelimitedLineTokenizer());setFieldSetMapper(new TickerFieldSetMapper());}});return reader;}@Beanpublic ItemProcessor<TickerData, TickerData> processor() {return new TickerPriceProcessor();}@Beanpublic ItemWriter<TickerData> writer() {return new LogItemWriter();}@Beanpublic Job TickerPriceConversion() throws MalformedURLException {return jobs.get("TickerPriceConversion").start(convertPrice()).build();}@Beanpublic Step convertPrice() throws MalformedURLException {return steps.get("convertPrice").<TickerData, TickerData> chunk(5).reader(reader()).processor(processor()).writer(writer()).build();} }TickerPriceConversionJobRunner的代碼清單
- 代碼項目
翻譯自: https://www.javacodegeeks.com/2015/07/spring-batch-replacing-xml-job-configuration-with-javaconfig.html
總結(jié)
以上是生活随笔為你收集整理的Spring Batch –使用JavaConfig替换XML作业配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓语言编程软件(安卓语言编程)
- 下一篇: jaxb xsd生成xml_使用JAXB