ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

如何安排Spring Boot Batch应用程序

2019-05-16 10:05:30  阅读:194  来源: 互联网

标签:spring scheduled-tasks spring-boot-2 spring-batch


我有一个Spring Boot Batch应用程序:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LoadApplication {

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

在也使用@EnableBatchProcessing注释的@Configuration类中,我定义了以下批处理作业bean:

@Bean
public Job loadJob(JobBuilderFactory jobs, Step stepLoadFile, Step stepArchiveFile) {
    return jobs.get("loadJob")
            .incrementer(new RunIdIncrementer())
            .start(stepLoadFile)
            .next(stepArchiveFile)
            .build();
}

批处理作业的stepLoadFile读取一个平面文件(见下文)并将文件数据写入数据库. stepArchiveFile然后将文件移动到另一个目录.

通常,工作需要在指定时间每天(周二至周六)运行一次.但是,如果找不到平面文件,则作业失败并需要每30分钟重新运行一次,直到成功或达到最大数量(例如5)的尝试.重新运行成功后,作业不应再次运行,直到下一个正常运行时间.此外,理想情况下,系统应防止同一作业的并发运行.如何才能做到这一切?

注意:重新运行不需要选择上一个作业运行失败的位置.这是因为块大小设置为大于文件中的项目数.

我在@Configuration类中尝试了这个(注意:我还在配置和主类中添加了@EnableRetry):

@Bean
public ItemReader<Test> reader(LineMapper<Test> lineMapper, ApplicationProperties properties) {
    FlatFileItemReader<Test> flatFileItemReader = new FlatFileItemReader<Test>() {
        @Override
        @Retryable(value = {ItemStreamException.class}, maxAttempts=5)
        public void open(ExecutionContext executionContext) throws ItemStreamException {
            super.open(executionContext);
        }

        @Override
        @Retryable(maxAttempts=5)
        public Holding read() throws UnexpectedInputException, ParseException, Exception {
            return super.read();
        }

    };
    flatFileItemReader.setResource(new FileSystemResource(properties.getLoadFile()));
    flatFileItemReader.setLineMapper(lineMapper);
    return flatFileItemReader;
}

抛出ItemStreamException并退出应用程序而不重试.

最佳答案:

您可以通过在LoadApplication类中添加以下组件来安排

@Component
@EnableScheduling
class ScheduledTasks {
@Autowired
JobLauncher jobLauncher;

@Autowired
Job job;

/**The pattern is a list of six single space-separated fields: 
 * representing second, minute, hour, day, month, weekday. 
 * Month and weekday names can be given as the first three letters of the English names.
 * Example patterns:

    "0 0 * * * *" = the top of every hour of every day.*
    "*\/10 * * * * *" = every ten seconds. Remove 2nd character, it is escape
    "0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
    "0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.
    "0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
    "0 0 0 25 12 ?" = every Christmas Day at midnight

 */
@Scheduled(cron = "0 0/30 * * * TUE-SAT")
public void runJob() throws Exception {
    jobLauncher.run(job, new JobParameters());
}
}

要重试失败的步骤,您可能必须在作业步骤中对其进行配置
< chunk reader =“itemReader”writer =“itemWriter”
             commit-interval =“2”retry-limit =“5”>
         &LT重试的异常类&GT
            < include class =“java.io.FileNotFoundException”/>
         &LT /可重试的异常类&GT
      &LT /块&GT

此外,如果您将弹簧批次元数据表存储在光盘而不是内存存储中,则弹出批处理不会再次运行相同的作业.

标签:spring,scheduled-tasks,spring-boot-2,spring-batch
来源: https://codeday.me/bug/20190516/1115148.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有