Dict.CN 在线词典, 英语学习, 在线翻译 ------------- MyGitee 朱秋贵内科诊所 My腾云code

springboot集成Spring Batch 20220902

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

posted @ 2022-09-02 10:16  cn2024  阅读(252)  评论(0编辑  收藏  举报