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) {}}}
阅读全文