当前位置: 首页 > 知识库问答 >
问题:

Spring Boot:Spring Batch作业不会自动启动

万浩淼
2023-03-14

主类

@SpringBootApplication
public class BatchApplication {

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

作业配置

@Configuration
@EnableTask
@EnableBatchProcessing
@Profile("master")
public class BatchJob {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    public static final String JOB_NAME = "myJob";
    
    private static final Logger LOGGER = Logger.getLogger(BatchJob .class);

    @Bean(name = JOB_NAME )
    public Job job() {
        LOGGER.info("Creating job bean...");
        return jobBuilderFactory.get(JOB_NAME).incrementer(new RunIdIncrementer())
                .start(helloWorldStep()).build();
    }

    @Bean
    public Step helloWorldStep() {
        LOGGER.info("Creating Step bean...");
        return stepBuilderFactory.get("helloWorldStep").tasklet(helloWorldTasklet()).build();
    }

    @Bean
    public HelloWorldTasklet helloWorldTasklet() {
        LOGGER.info("Creating tasklet...");
        return new HelloWorldTasklet();
    }
}

HelloWorldTasklet

public class HelloWorldTasklet implements Tasklet {

    private static final Logger LOGGER = Logger.getLogger(HelloWorldTasklet.class);

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

        LOGGER.info("Hello World");
        System.out.println("Hello World Again");
        return RepeatStatus.FINISHED;

    }

}
spring.application.name=my-batch-app
spring.datasource.url=jdbc:h2:mem:dataflow
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.cloud.task.closecontext_enabled=true
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>my-project-parent</artifactId>
        <groupId>com.myapp</groupId>
        <version>MAR21_1.0</version>
    </parent>

    <groupId>com.myapp</groupId>
    <artifactId>my-batch-app</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
            <version>2.3.11.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.3.11.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.3.11.RELEASE</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
         <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17-cloudera1</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.199</version>
            <!--<scope>runtime</scope>-->
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.myapp</groupId>
    <artifactId>my-project-parent</artifactId>
    <version>MAR21_1.0</version>
    <packaging>pom</packaging>
    <name>my-project-parent</name>

    <properties>
        <java.version>1.8</java.version>            
        <spring.kafka.version>2.5.5.RELEASE</spring.kafka.version>          <spring.cloud.starter.task.version>2.2.4.RELEASE</spring.cloud.starter.task.version>            <spring.boot.starter.data.jpa.version>2.2.1.RELEASE</spring.boot.starter.data.jpa.version>
        <database.h2.version>1.4.199</database.h2.version>          <spring.cloud.dataflow.rest.client.version>2.7.1</spring.cloud.dataflow.rest.client.version>
        <jackson-datatype-jsr310.version>2.10.0</jackson-datatype-jsr310.version>
        <spring.batch.version>4.2.4.RELEASE</spring.batch.version>
        <log4jversion>1.2.17-cloudera1</log4jversion>
    </properties>

    <build>
        <plugins>
            <!-- Maven Compiler Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>
Picked up _JAVA_OPTIONS: -Djdk.tls.maxCertificateChainLength=20

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.5.RELEASE)

2021-06-07 16:23:58.021  INFO 1 --- [           main] .r.c.b.w.b.c.o.BatchApplication : Starting BatchApplication  on batch-controller-test-5wg6eryzpj with PID 1 (/tmp/my-batch-app.jar started by ? in /tmp)
2021-06-07 16:23:58.026  INFO 1 --- [           main] .r.c.b.w.b.c.o.BatchApplication  : The following profiles are active: master
2021-06-07 16:24:03.031  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-06-07 16:24:03.316  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 185ms. Found 0 JPA repository interfaces.
2021-06-07 16:24:04.917  INFO 1 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.
2021-06-07 16:24:04.935  INFO 1 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.
2021-06-07 16:24:04.949  INFO 1 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
2021-06-07 16:24:06.814  INFO 1 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.integration.config.IntegrationManagementConfiguration' of type [org.springframework.integration.config.IntegrationManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-06-07 16:24:07.047  INFO 1 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationChannelResolver' of type [org.springframework.integration.support.channel.BeanFactoryChannelResolver] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-06-07 16:24:07.113  INFO 1 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationDisposableAutoCreatedBeans' of type [org.springframework.integration.config.annotation.Disposables] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-06-07 16:24:09.125  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-06-07 16:24:09.822  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-06-07 16:24:10.512  INFO 1 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-06-07 16:24:11.220  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.4.8.Final}
2021-06-07 16:24:12.724  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2021-06-07 16:24:14.019  INFO 1 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2021-06-07 16:24:16.335  INFO 1 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-06-07 16:24:16.429  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-06-07 16:24:19.814  INFO 1 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2021-06-07 16:24:21.721  INFO 1 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2021-06-07 16:24:21.721  INFO 1 --- [           main] o.s.i.channel.PublishSubscribeChannel    : Channel 'application.errorChannel' has 1 subscriber(s).
2021-06-07 16:24:21.722  INFO 1 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started bean '_org.springframework.integration.errorLogger'
2021-06-07 16:24:21.818  INFO 1 --- [           main] .r.c.b.w.b.c.o.BatchApplication  : Started BatchApplication  in 27.903 seconds (JVM running for 31.116)
2021-06-07 16:24:21.828  INFO 1 --- [extShutdownHook] o.s.i.endpoint.EventDrivenConsumer       : Removing {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2021-06-07 16:24:21.830  INFO 1 --- [extShutdownHook] o.s.i.channel.PublishSubscribeChannel    : Channel 'application.errorChannel' has 0 subscriber(s).
2021-06-07 16:24:21.830  INFO 1 --- [extShutdownHook] o.s.i.endpoint.EventDrivenConsumer       : stopped bean '_org.springframework.integration.errorLogger'
2021-06-07 16:24:21.832  INFO 1 --- [extShutdownHook] o.s.s.c.ThreadPoolTaskScheduler          : Shutting down ExecutorService 'taskScheduler'
2021-06-07 16:24:21.916  INFO 1 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-06-07 16:24:21.917  INFO 1 --- [extShutdownHook] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
2021-06-07 16:24:21.920  INFO 1 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2021-06-07 16:24:22.014  INFO 1 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

为了使事情更清楚,这是我在jar中看到的文件夹列表。看起来spring boot maven插件是用来创建jar的?那么,为什么批处理作业不在Eclipse之外启动:

--my-batch-app.jar
 - BOOT-INF
   - classes
   - lib
 - META-INF
 - org

注意:如您所见,日志还显示确实选择了主概要文件,因为在启动任务时,我传递了--spring.profiles.active=master作为输入参数。

共有1个答案

杨骁
2023-03-14

很少跟进问题-

您是按计划还是在启动时运行此应用程序?

您是否添加了此启动依赖项?

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-batch</artifactId>
</dependency>
 类似资料:
  • 我有两个不同的工作(实际上更多,但为了简单起见,假设2)。每个作业可以与另一个作业并行运行,但同一作业的每个实例应该顺序运行(否则实例将共享彼此的资源)。 基本上,我希望这些作业中的每一个都有自己的作业实例队列。我想我可以使用两个不同的线程池作业启动程序(每个都有一个线程),并将一个作业启动程序与每个作业相关联。 在从Spring Batch Admin web UI中启动作业时,是否有一种方法可

  • 在Apache Hadoop中使用job.waitForCompletion(true)方法和通过ToolRunner.run(new MyClass(),args)启动map reduce作业有什么区别? 我通过以下两种方式执行MapReduce作业: 首先如下: 其次是: 两种方法的输出都是一样的。但是我不明白这两种方法有什么区别?哪一种比另一种更受欢迎?

  • 我有一个用Spring Boot(1.4.1.Release)开发的Spring批处理作业。 它成功地从命令行运行,并将作业执行数据写入MySQL。它在Spring Batch Admin(2.0.0.M1,指向MySQL)中显示为不可启动的作业,我可以看到作业执行度量。 我想知道以前有没有人这样做过。文档中有一节添加您自己的启动作业。但是它没有指定在哪里为作业添加实现jar? 是spring-b

  • 我将JProfiler 7配置为与本地运行的Weblogic 8.1集成。 似乎weblogic在使用JProfiler生成的脚本(名为startweblog\u JProfiler.cmd)时启动得很好 然而,janalyiler部分错误并带有以下消息(分析从未发生): 我为PATH添加了以下内容: 路径=C:\Program Files\jprofiler7\bin\windows; 修改了我

  • 当我尝试启动嵌入式tomcat时,我的应用程序将一直运行,直到tomcat达到以下行: "Dez11, 2012 3:28:06PMorg.apache.coyote.AbstractProtocol启动信息:启动协议处理程序["超文本传输协议-bio-8080"]" 有人能帮我嵌入Tomcat吗? 顺致敬意,

  • 我在开始时遇到了问题!事实上,它并没有启动,我也没有得到更多的信息: 有人能帮我吗?