springbatch和定时器读取txt文件批量导入数据库

来源:互联网 发布:2017年nba新秀数据排名 编辑:程序博客网 时间:2024/05/16 23:38
1、定时器把txt文件中的数据导入数据库,文件名:
/** * 每天批量扫描添加消息 时间暂定 *  * @throws Exception */@Scheduled(cron = "0 0/1 * * * ?") // 每分钟public void readMsgTxt() throws Exception {// 1、生成文件名称e_eal_cust_remd_20171110// 读取txt文件String today = DateUtil.getTimeStr();String addr = path + "e_eal_cust_remd_" + today + ".txt";System.out.println(addr);String line = "";File file = new File(addr);BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));while ((line = br.readLine()) != null) {String[] arr = line.split("@\\|@");//行内间隔符为@|@,@\\|@为转义后的间隔符McMessage msg = new McMessage();msg.setTellerCode(arr[0]);msg.setMsgType(arr[1]);msg.setMsgContent(arr[2]);//执行sql语句mcmessageservice.addMessageBatch(msg);}}

2、spring batch读取txt文件:

(1)

package com.feeling.mc.batch.control;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.batch.core.Job;import org.springframework.batch.core.JobParameters;import org.springframework.batch.core.JobParametersBuilder;import org.springframework.batch.core.launch.JobLauncher;import org.springframework.beans.BeansException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import com.feeling.mc.common.utils.DateUtil;import com.feeling.mc.db.entity.BatchManager;import com.feeling.mc.db.mapper.messager.BatchManagerMapper;@Componentpublic class DemoController implements ApplicationContextAware{@Autowired(required = false)JobLauncher jobLauncher;private ApplicationContext applicationContext;public JobParameters jobParameters;@Autowiredprivate BatchManagerMapper managerMapper;//@Scheduled(cron = "0 0 8 * * ?") // 早上8点// 每分钟跑一次@Scheduled(cron = "0 0/1 * * * ?") public void imp() throws Exception {Job job = (Job)this.applicationContext.getBean("ReaderBatchMessage");jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters();jobLauncher.run(job, jobParameters);}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;}}
(2)job的class类

package com.feeling.mc.batch.batch;import java.io.File;import java.util.Date;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.batch.core.Job;import org.springframework.batch.core.Step;import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;import org.springframework.batch.item.file.FlatFileItemReader;import org.springframework.batch.item.file.mapping.DefaultLineMapper;import org.springframework.batch.item.file.mapping.FieldSetMapper;import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;import org.springframework.batch.item.file.transform.FieldSet;import org.springframework.batch.repeat.exception.DefaultExceptionHandler;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.core.io.FileSystemResource;import org.springframework.core.task.SimpleAsyncTaskExecutor;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.validation.BindException;import com.feeling.mc.batch.listener.JobCompletionNotificationListener;import com.feeling.mc.batch.writer.ReaderMessageBatchWriter;import com.feeling.mc.common.utils.DateUtil;import com.feeling.mc.core.module.McMessage;@Configuration@PropertySource(value = "classpath:application.properties")public class ReaderMessageBatchJob {private static final Logger log = LoggerFactory.getLogger(ReaderMessageBatchJob.class);@Value("${msg_txt_path}")private String path;@Value("${msg_push_type}")private String msg_push_type;@Autowired(required = false)public JobBuilderFactory jobBuilderFactory;@Autowired(required = false)public StepBuilderFactory stepBuilderFactory;@Autowired(required = false)public PlatformTransactionManager platformTransactionManager;@Autowiredpublic ReaderMessageBatchWriter readermessagebatchwriter;public FlatFileItemReader<McMessage> fileRead() {FlatFileItemReader<McMessage> fileRead = new FlatFileItemReader<>();String today = DateUtil.getTimeStr();String addr = path + "e_eal_cust_remd_" + today + ".txt";System.out.println(addr);fileRead.setEncoding("UTF-8");fileRead.setResource(new FileSystemResource(new File(addr)));DefaultLineMapper<McMessage> lineMapper = new DefaultLineMapper<McMessage>();lineMapper.setLineTokenizer(new DelimitedLineTokenizer("@|@"));lineMapper.setFieldSetMapper(new FieldSetMapper<McMessage>() {@Overridepublic McMessage mapFieldSet(FieldSet fieldset) throws BindException {McMessage msg = new McMessage();try {if (msg_push_type.indexOf(fieldset.readString(1)) >= 0) {msg.setTellerCode(fieldset.readString(0));msg.setMsgType(fieldset.readString(1));msg.setMsgContent(fieldset.readString(2));msg.setDeliverTime(DateUtil.stringToDate2(fieldset.readString(3)));msg.setDoneTime(DateUtil.stringToDate2(fieldset.readString(4)));msg.setPeriod("Z");msg.setCreateTime(new Date());msg.setType("DB");// typemsg.setTitle("代办事项");}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return msg;}});fileRead.setLineMapper(lineMapper);return fileRead;}/** * 构建job *  * @param listener * @return */@Bean("ReaderBatchMessage")public Job MessageBatchInsertJob(JobCompletionNotificationListener listener) {return jobBuilderFactory.get("MessageBatchInsertJob").listener(listener).flow(MessageBatchInsertStep()).end().build();}/** * 声明发送到MQ step *  * @return */@Beanpublic Step MessageBatchInsertStep() {log.info("MessageBatchInsertStep");return stepBuilderFactory.get("MessageBatchInsertStep").<McMessage, McMessage>chunk(100).reader(fileRead()).writer(readermessagebatchwriter).faultTolerant().skip(Exception.class).skipLimit(100).taskExecutor(new SimpleAsyncTaskExecutor()).startLimit(2).exceptionHandler(new DefaultExceptionHandler()) // 设置并发方式执行.throttleLimit(10) // 并发任务数为 10,默认为4.transactionManager(platformTransactionManager).build();}}
(3)writer

package com.feeling.mc.batch.writer;import java.util.List;import org.springframework.batch.core.configuration.annotation.StepScope;import org.springframework.batch.item.ItemWriter;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import com.feeling.mc.batch.service.McMessageService;import com.feeling.mc.core.module.McMessage;@Component@StepScopepublic class ReaderMessageBatchWriter implements ItemWriter<McMessage>{@Autowiredprivate McMessageService mcMessageService;@Overridepublic void write(List<? extends McMessage> items) throws Exception {try {for (McMessage msg : items) {mcMessageService.addMessageBatch(msg);}} catch (Exception e) {}}}




阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 东邪西毒经典台词 倚天之东邪传人 东邪西毒导演 83版东邪西毒20集免费观看 东邪西毒在线播放 东邪西毒8码公开网址 东邪西毒南帝北丐 射雕之东邪与倾馨 东邪西毒台词 83东邪西毒完整播放 南帝北丐东邪西毒中神通 东邪西毒图片 东邪西毒粤语 83版东邪西毒免费观看 83板东邪西毒手机免费看 射雕之东邪之子 山西大雁徐良 东邪西毒香肠嘴 东邪西毒1994国语 83版东邪西毒免费欢看 东邪西狂南僧北侠 东邪西毒粤语版 东邪西狂南僧北侠中顽童 83版东邪西毒免费版 东邪西毒插曲 东邪酉毒心中8码 谢雕英雄传83版东邪西毒 东郡 鲁郡东石门送杜二甫 保利东郡 太原保利东郡 东合逸海郡 宜都东郡酒店 东郡花园 泰和东郡 荣盛紫提东郡 天津万科东郡 无锡万科东郡 东郡快捷 东郭先生 东郭 东郭镇