springboot集成springbatch启动job和退出分析

springboot中启动:

启动类中的run方法往下运行会调用callRunner方法:

    private void callRunners(ApplicationContext context, ApplicationArguments args) {
        List<Object> runners = new ArrayList();
        runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
        runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
        AnnotationAwareOrderComparator.sort(runners);
        Iterator var4 = (new LinkedHashSet(runners)).iterator();

        while(var4.hasNext()) {
            Object runner = var4.next();
            if (runner instanceof ApplicationRunner) {
                this.callRunner((ApplicationRunner)runner, args);
            }

            if (runner instanceof CommandLineRunner) {
                this.callRunner((CommandLineRunner)runner, args);
            }
        }

    }

这里会获取到SpringBatch 自动配置, BatchAutoConfiguration中注册的CommandLineRunner bean。进而执行。

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(
        prefix = "spring.batch.job",
        name = {"enabled"},
        havingValue = "true",
        matchIfMissing = true
    )
    public JobLauncherCommandLineRunner jobLauncherCommandLineRunner(JobLauncher jobLauncher, JobExplorer jobExplorer, JobRepository jobRepository) {
        JobLauncherCommandLineRunner runner = new JobLauncherCommandLineRunner(jobLauncher, jobExplorer, jobRepository);
        String jobNames = this.properties.getJob().getNames();
        if (StringUtils.hasText(jobNames)) {
            runner.setJobNames(jobNames);
        }

        return runner;
    }

如果配置文件中没有配置:

spring.batch.job.enabled = false

job任务会在springboot启动时执行

退出:

@SpringBootApplication
public class MainClass {
 
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MainClass.class);
        int exitCode = SpringApplication.exit(context );
        System.exit(exitCode);
    }
}
参考JobLauncherCommandLineRunner 可知,在execute方法中发布了ApplicationEvent
    protected void execute(Job job, JobParameters jobParameters) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException, JobParametersNotFoundException {
        JobParameters parameters = this.getNextJobParameters(job, jobParameters);
        JobExecution execution = this.jobLauncher.run(job, parameters);
        if (this.publisher != null) {
            this.publisher.publishEvent(new JobExecutionEvent(execution));
        }

    }
public void onApplicationEvent(JobExecutionEvent event) {
        this.executions.add(event.getJobExecution());
    }
JobExecutionEvent 事件由JobExecutionExitCodeGenerator监听。并以此生成退出码。
public static int exit(ApplicationContext context, ExitCodeGenerator... exitCodeGenerators) {
        Assert.notNull(context, "Context must not be null");
        byte exitCode = 0;

        int exitCode;
        try {
            try {
                ExitCodeGenerators generators = new ExitCodeGenerators();
                Collection<ExitCodeGenerator> beans = context.getBeansOfType(ExitCodeGenerator.class).values();
                generators.addAll(exitCodeGenerators);
                generators.addAll(beans);
                exitCode = generators.getExitCode();
                if (exitCode != 0) {
                    context.publishEvent(new ExitCodeEvent(context, exitCode));
                }
            } finally {
                close(context);
            }
        } catch (Exception var9) {
            var9.printStackTrace();
            exitCode = exitCode != 0 ? exitCode : 1;
        }

        return exitCode;
    }

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于搭建一个使用Spring Boot、Spring Batch、MyBatis和MySQL的项目,您可以按照以下步骤进行操作: 1. 创建一个Spring Boot项目:您可以使用Spring Initializr(https://start.spring.io/)或者在IDE中创建一个新的Spring Boot项目。 2. 添加所需的依赖:在项目的pom.xml文件中添加以下依赖: ```xml <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Batch --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> ``` 3. 配置数据源:在application.properties或application.yaml文件中配置MySQL数据库连接信息,例如: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/mydatabase username: your-username password: your-password ``` 4. 创建数据库表:使用MySQL客户端或其他工具创建您需要的数据库表。 5. 创建实体类和Mapper接口:根据您的数据表结构创建对应的Java实体类,并使用MyBatis的注解或XML配置文件创建Mapper接口。 6. 创建Spring Batch的Job配置:创建一个继承自org.springframework.batch.core.configuration.annotation.JobConfiguration的配置类,在其中定义Batch Job的步骤和任务。 7. 编写Batch Job的业务逻辑:根据需求在Batch Job的步骤中编写相应的业务逻辑,例如读取数据、处理数据、写入数据等。 8. 运行Batch Job:在应用程序中启动Batch Job,可以通过命令行、定时任务或其他方式触发。 以上是一个简单的搭建步骤,您可以根据具体需求进行更详细的配置和开发。希望能对您有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值