博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring batch +boot
阅读量:7155 次
发布时间:2019-06-29

本文共 7585 字,大约阅读时间需要 25 分钟。

hot3.png

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

org.podcastpedia.batch
podcastpedia-batch
0.1.0
1.1.6.RELEASE
1.7
org.springframework.boot
spring-boot-starter-parent
1.1.6.RELEASE
org.springframework.boot
spring-boot-starter-batch
org.springframework.boot
spring-boot-starter-data-jpa
org.apache.httpcomponents
httpclient
4.3.5
org.apache.httpcomponents
httpcore
4.3.2
org.apache.velocity
velocity
1.7
org.apache.velocity
velocity-tools
2.0
org.apache.struts
struts-core
rome
rome
1.0
rome
rome-fetcher
1.0
org.jdom
jdom
1.1
xerces
xercesImpl
2.9.1
mysql
mysql-connector-java
5.1.31
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.boot
spring-boot-starter-remote-shell
javax.mail
mail
javax.mail
mail
1.4.7
javax.inject
javax.inject
1
org.twitter4j
twitter4j-core
[4.0,)
org.springframework.boot
spring-boot-starter-test
maven-compiler-plugin
org.springframework.boot
spring-boot-maven-plugin

</project>

package org.podcastpedia.batch.jobs.addpodcast;

import org.podcastpedia.batch.common.configuration.DatabaseAccessConfiguration; import org.podcastpedia.batch.common.listeners.LogProcessListener; import org.podcastpedia.batch.common.listeners.ProtocolListener; import org.podcastpedia.batch.jobs.addpodcast.model.SuggestedPodcast; 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.LineMapper; import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper; 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.context.annotation.Import; import org.springframework.core.io.ClassPathResource;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

@Configuration @EnableBatchProcessing @Import({DatabaseAccessConfiguration.class, ServicesConfiguration.class}) public class AddPodcastJobConfiguration {

@Autowiredprivate JobBuilderFactory jobs;@Autowiredprivate StepBuilderFactory stepBuilderFactory;// tag::jobstep[]@Beanpublic Job addNewPodcastJob(){	return jobs.get("addNewPodcastJob")			.listener(protocolListener())			.start(step())			.build();}@Beanpublic Step step(){	return stepBuilderFactory.get("step")			.
chunk(1) //important to be one in this case to commit after every line read .reader(reader()) .processor(processor()) .writer(writer()) .listener(logProcessListener()) .faultTolerant() .skipLimit(10) //default is set to 0 .skip(MySQLIntegrityConstraintViolationException.class) .build();}// end::jobstep[]// tag::readerwriterprocessor[]@Beanpublic ItemReader
reader(){ FlatFileItemReader
reader = new FlatFileItemReader
(); reader.setLinesToSkip(1);//first line is title definition reader.setResource(new ClassPathResource("suggested-podcasts.txt")); reader.setLineMapper(lineMapper()); return reader;}@Beanpublic LineMapper
lineMapper() { DefaultLineMapper
lineMapper = new DefaultLineMapper
(); DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer(); lineTokenizer.setDelimiter(";"); lineTokenizer.setStrict(false); lineTokenizer.setNames(new String[]{"FEED_URL", "IDENTIFIER_ON_PODCASTPEDIA", "CATEGORIES", "LANGUAGE", "MEDIA_TYPE", "UPDATE_FREQUENCY", "KEYWORDS", "FB_PAGE", "TWITTER_PAGE", "GPLUS_PAGE", "NAME_SUBMITTER", "EMAIL_SUBMITTER"}); BeanWrapperFieldSetMapper
fieldSetMapper = new BeanWrapperFieldSetMapper
(); fieldSetMapper.setTargetType(SuggestedPodcast.class); lineMapper.setLineTokenizer(lineTokenizer); lineMapper.setFieldSetMapper(suggestedPodcastFieldSetMapper()); return lineMapper;}@Beanpublic SuggestedPodcastFieldSetMapper suggestedPodcastFieldSetMapper() { return new SuggestedPodcastFieldSetMapper();}/** configure the processor related stuff */@Beanpublic ItemProcessor
processor() { return new SuggestedPodcastItemProcessor();}@Beanpublic ItemWriter
writer() { return new Writer();}// end::readerwriterprocessor[]@Beanpublic ProtocolListener protocolListener(){ return new ProtocolListener();}@Beanpublic LogProcessListener logProcessListener(){ return new LogProcessListener();}

}

@Bean public Job addNewPodcastJob(){ return jobs.get("addNewPodcastJob") .listener(protocolListener()) .start(step()) .build(); }

@Bean public Step step(){ return stepBuilderFactory.get("step") .<SuggestedPodcast,SuggestedPodcast>chunk(1) //important to be one in this case to commit after every line read .reader(reader()) .processor(processor()) .writer(writer()) .listener(logProcessListener()) .faultTolerant() .skipLimit(10) //default is set to 0 .skip(MySQLIntegrityConstraintViolationException.class) .build(); }

转载于:https://my.oschina.net/u/3503613/blog/1486005

你可能感兴趣的文章
第五章Android移植与驱动读书笔记
查看>>
Docker 常用命令
查看>>
计算机面试书籍与求职网站推荐
查看>>
export to excel
查看>>
Python爬虫2-检测编码(使用chardet)
查看>>
iOS 应用内付费(IAP)开发步骤
查看>>
计算平方根的算法
查看>>
读书笔记之: 操作系统概念(第6版)-第四部分 IO系统(I/O系统, 大容量存储结构)
查看>>
JAVA学习--Collections集合工具类使用
查看>>
Verilog学习----调试用系统任务和常用编译预处理语句
查看>>
个人知识管理工具 PinPKM
查看>>
GlusterFS分布式文件系统的使用
查看>>
[数据结构]之递归
查看>>
es6 Proxy对象详解
查看>>
设计一个精致按钮
查看>>
为IE8添加EventListener系列方法支持
查看>>
数据库基本语法
查看>>
环境监测小助手V1.1的Windows版
查看>>
bzoj1046(HAOI2007)上升序列
查看>>
bzoj 1898 [Zjoi2005]Swamp 沼泽鳄鱼——矩阵快速幂
查看>>