搜索

springboot集成Spring Batch 20220902 - cn2022 - 博客园


发布时间: 2022-11-24 18:03:05    浏览次数:8 次

1、pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

2、application.properties
# 应用名称
spring.application.name=batch2029
# 应用服务 WEB 访问端口
server.port=2029

 

3、Reader
package com.sc.batch2029.reader;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;

public class Reader implements ItemReader {

private String[] messages={"篮球之神 Michael Jordan,欢来到Batch示例 Webcome to Spring batch Example"};
private int count=0;

@Override
public Object read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
if(count<messages.length){
return messages[count++];
}else {
count=0;
}
return null;
}
}

4、Write
package com.sc.batch2029.write;
import org.springframework.batch.item.ItemWriter;
import java.util.List;

public class Write implements ItemWriter<String> {

@Override
public void write(List<? extends String> messages) throws Exception {
for(String msg:messages){
System.out.println("输出信息:"+msg);
}
}
}


5、BatchConfig
package com.sc.batch2029.config;
import com.sc.batch2029.listen.JobCompletionLister;
import com.sc.batch2029.processor.Processor;
import com.sc.batch2029.reader.Reader;
import com.sc.batch2029.write.Write;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
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.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BatchConfig {

@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;

@Bean
public JobExecutionListener listener(){
return new JobCompletionLister();
}

@Bean
public Step orderStep1(){
return stepBuilderFactory.get("orderStep1").<String,String> chunk(1)
.reader(new Reader()).processor(new Processor())
.writer(new Write()).build();
}

@Bean
public Job processJob(){
return jobBuilderFactory.get("processJob").incrementer(new RunIdIncrementer())
.listener(listener()).flow(orderStep1()).end().build();
}
}


6、JobCompletionLister
package com.sc.batch2029.listen;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
public class JobCompletionLister extends JobExecutionListenerSupport {

@Override
public void beforeJob(JobExecution jobExecution) {
if(jobExecution.getStatus()==BatchStatus.STARTED){
System.out.println("批处理执行开始.........");
}
}

@Override
public void afterJob(JobExecution jobExecution) {
if(jobExecution.getStatus()==BatchStatus.COMPLETED){
System.out.println("批处理执行结束.........");
}
}
}


7、Processor
package com.sc.batch2029.processor;

import org.springframework.batch.item.ItemProcessor;

public class Processor implements ItemProcessor<String,String> {

@Override
public String process(String data) throws Exception {
return data.toUpperCase();
}
}

 


8、JobInvokeController
package com.sc.batch2029.controller;
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.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableScheduling
public class JobInvokeController {

@Autowired
JobLauncher jobLauncher;
@Autowired
Job processJob;

@Scheduled(cron = "*/3 * * * * *")
public void handle()throws Exception{
JobParameters jobParameters=new JobParametersBuilder()
.addLong("time",System.currentTimeMillis()).toJobParameters();
jobLauncher.run(processJob,jobParameters);
}
}


9、Batch2029Application
package com.sc.batch2029;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class Batch2029Application {

public static void main(String[] args) {
SpringApplication.run(Batch2029Application.class, args);
}

}

 

参考:https://www.cnblogs.com/slimshady/p/10064898.html

免责声明 springboot集成Spring Batch 20220902 - cn2022 - 博客园,资源类别:文本, 浏览次数:8 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-24 06:03:05。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/smallfa/p/16648893.html