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

Spring boot应用程序在Spring批处理作业完成后重新启动

充浩波
2023-03-14

我有一个spring boot应用程序,它应该将数据库(产品)中的一个表导出为CSV文件,所以我使用spring batch来实现这一点,我的问题是,当作业完成时,应用程序重新启动,我认为这是因为spring batch在完成后关闭了实体管理器,但我不确定

注意1:我使用JPapagingItemReader作为Reader。

注意2:我正在使用Jobluncher从controller启动作业

注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]
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.
<?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);
    }


}

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

共有1个答案

秦奇
2023-03-14

您能在下面配置applicaion.properties吗

spring.batch.job.enabled=false
spring.batch.initializer.enabled=false
 类似资料:
  • 我有一个spring批处理作业,从CSV文件读取并写入数据库。我想让它重新启动。例如,如果在读取文件或写入db时出现异常,导致作业失败,则应从失败的同一点/块重新开始,而不是从头开始读取整个文件。 我正在从一个endpoint触发作业启动器,并在我的控制器中配置了它。 目前,我正在通过控制器将参数(这是一个唯一的标识符/数字)传递给作业参数,以运行新的作业实例。如果作业失败,我将使用与GET请求中

  • 我需要从远程SFTP服务器下载一个文件,并使用spring batch处理它们。我已经实现了使用Spring集成下载文件的代码。但我无法从Spring集成组件启动Spring批处理作业。我有以下代码: 但这不起作用(上一个方法中的错误),因为找不到文件类型的bean。我不能把这两部分连在一起。如何连接集成和批处理?

  • 我在一个Spring Boot应用程序中定义了多个Spring批处理作业。例如,job1、Job2。等等。 null

  • 根据Spring batch JobOperator类的文档, https://docs.spring.io/spring-batch/4.0.x/api/org/springframework/batch/core/launch/JobOperator.html#restart-long- 重新启动失败或停止的作业执行。如果提供的id不存在或对应于在正常情况下已成功完成的JobInstance,

  • 我们的Spring Batch应用程序在重新启动失败的作业时,再次处理相同的记录,导致重复的行,我们希望了解如何避免这种情况。 启动批处理作业的Spring集成轮询器配置为每两个小时运行一次。第二次运行时,作业参数将相同,但如果上一次运行失败(例如,由于数据截断异常),Spring Batch不会抱怨作业已完成。 在故障点,几十万条记录已经被处理并从源表复制到目标表。在以后运行作业时,相同的行将复