【问题标题】:Spring boot app restart after spring batch job complete春季批处理作业完成后春季启动应用程序重新启动
【发布时间】:2020-04-23 09:13:20
【问题描述】:

我有 spring boot 应用程序,它应该将数据库(产品)中的表导出为 csv 文件,所以我使用 spring batch 来执行此操作,我的问题是,当作业完成时,应用程序重新启动我认为它是因为春季批处理完成后关闭实体管理器,但我不确定

注意 1:我使用 JpaPagingItemReader 作为阅读器。

注意 2:我正在使用 JobLuncher 从控制器开始工作

注释 3:我知道批处理是异步工作的,所以控制器在作业完成之前返回结果,我也希望找到改变这一点的方法。

注意 4:我在我的应用程序属性中使用 spring.batch.job.enabled=false 停止了作业的自动午餐

注释 5:我发现了一个类似问题的问题,但它没有答案并关闭(重复)但它不是,这是不同的问题。

我的代码:

-批量配置:

    @Configuration
    public class BatchConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @PersistenceUnit
    EntityManagerFactory entityManagerFactory;

    @Bean
    public JpaPagingItemReader<Product> reader() {
        JpaPagingItemReader<Product> ir = new JpaPagingItemReaderBuilder<Product>().name("productReader")
                .entityManagerFactory(entityManagerFactory).queryString("select p from Product p").pageSize(500)
                .build();
        return ir;
    }

    @Bean
    public ProductItemProcessor processor() {
        return new ProductItemProcessor();
    }

    @Bean
    public FlatFileItemWriter<Product> writer() {
        DelimitedLineAggregator<Product> aggregator = new DelimitedLineAggregator<Product>();
        BeanWrapperFieldExtractor<Product>  extractor = new BeanWrapperFieldExtractor<>();
        extractor.setNames(new String[] { "id", "name", "price" });
        aggregator.setFieldExtractor(extractor);

        FlatFileItemWriter<Product> writer = new FlatFileItemWriter<Product>();
        writer.setResource(new ClassPathResource("prices.csv"));
        writer.setLineAggregator(aggregator);

        return writer;
    }

     @Bean
     public Step step1() {
      return stepBuilderFactory.get("step1").<Product, Product> chunk(100)
        .reader(reader())
        .processor(processor())
        .writer(writer())
        .build();
     }

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


    }

-控制器:

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    Job job;

    @RequestMapping("/test")
    @ResponseBody
    public String test(HttpSession session) {
        try {
            JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
                    .toJobParameters();
            jobLauncher.run(job, jobParameters);
        } catch (Exception e) {
        System.out.println(e.getMessage());
        }

        return "Done";

    }

这是控制台:

工作完成后:

2020-01-06 14:02:03.570  INFO 4456 --- [nio-8080-exec-9] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=exportUserJob]] completed with the following parameters: [{time=1578312121885}] and the following status: [COMPLETED]

它关闭并关闭 jpa:

2020-01-06 14:02:04.911  INFO 4456 --- [       Thread-7] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2020-01-06 14:02:04.918  INFO 4456 --- [       Thread-7] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-01-06 14:02:04.955  INFO 4456 --- [       Thread-7] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-01-06 14:02:04.989  INFO 4456 --- [       Thread-7] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

pom.xml

<?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>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.co-mada</groupId>
    <artifactId>Mada_Website</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Mada_Website</name>
    <description>Mada Company Website</description>
    <!-- Packaging -->
    <packaging>war</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>


        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            <!-- <version>3.0.4.RELEASE</version> -->
        </dependency>


        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.4.1</version>
        </dependency>


        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.3.1</version>
        </dependency>


        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>font-awesome</artifactId>
            <version>5.10.1</version>
        </dependency>


        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>popper.js</artifactId>
            <version>1.15.0</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>runtime</scope>
        </dependency>



        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-search-orm</artifactId>
            <version>5.11.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-jdbc</artifactId>
        </dependency>

        <!-- spring batch dependency -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

主类:


@SpringBootApplication
@EnableBatchProcessing
public class MadaWebsiteApplication extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // TODO Auto-generated method stub
        return builder.sources(MadaWebsiteApplication.class);
    }


}

我希望一切都清楚,谢谢。

【问题讨论】:

  • 你能显示日志文件吗?我从来没有见过这样的行为
  • 是的,先生,我添加了控制台输出,请检查
  • 你能显示你的应用程序类和 pom.xml 或 gradle 文件吗?
  • 我也遇到了同样的问题
  • @SanjayNaik 所以请投票支持这个问题!

标签: spring-boot spring-batch


【解决方案1】:

你能在下面配置到 applicaion.properties

spring.batch.job.enabled=false
spring.batch.initializer.enabled=false

【讨论】:

  • 又添加了一个属性,请检查一下吗?
猜你喜欢
  • 1970-01-01
  • 2017-10-06
  • 2018-09-27
  • 2019-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-15
  • 2018-05-20
相关资源
最近更新 更多