spring - 有没有办法跳过仅针对特定作业的 Spring Batch 持久元数据?

标签 spring spring-boot spring-batch

有几个示例说明我们如何在不将元数据持久化到数据库的情况下使用 Spring Batch。以下是有关此事的一些示例和相关问题:

  • Spring-Batch without persisting metadata to database?
  • How to avoid Spring batch persistence of metadata in DB
  • Spring Batch - How to prevent batch from storing transactions in DB
  • Spring boot + spring batch without DataSource

  • 但是我有一个稍微不同的用例:我有一些工作每小时运行一次,我想将元数据保存到我的数据库中(例如,创建报告,运行一些测试,这两者在处理过程中可能有点繁重)。我有一些其他类型的作业每分钟左右运行一次(例如,解锁由于重复输入错误密码而被锁定的用户帐户等),它们不涉及太多处理,而是一个简单的 sql 查询。

    这里的问题分为两部分:
  • 有没有办法将第一类作业(例如报告处理)的元数据保留在数据库中,而对第二类作业(例如解锁用户帐户)根本不使用数据库持久性?
  • 或者,甚至将 Spring Batch 用于第二类工作是否会矫枉过正/根本不需要?有 @Scheduled 的方法吗?注释就够了吗?
  • 最佳答案

    1. Is there a way to keep metadata of the first type of jobs (e.g. reports processing) in the database while not using database persistence at all for second type of jobs (e.g. unlocking user accounts)?


    这是配置问题。您可以使用 Spring profiles对于数据源。这个想法是定义一个持久数据源和一个内存中的数据源。然后像往常一样使用数据源配置所有作业,并在运行时使用正确的配置文件运行它们。这是一个快速示例:
    import javax.sql.DataSource;
    
    import com.zaxxer.hikari.HikariDataSource;
    
    import org.springframework.batch.core.Job;
    import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
    import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
    
    @Configuration
    @EnableBatchProcessing
    public class JobsConfiguration {
    
        @Bean
        public Job job1() {
            return null;
        }
    
        @Bean
        public Job job2() {
            return null;
        }
    
        @Bean
        @Profile("in-memory")
        public DataSource embeddedDataSource() {
            return new EmbeddedDatabaseBuilder()
                    .setType(EmbeddedDatabaseType.HSQL)
                    .addScript("/org/springframework/batch/core/schema-hsqldb.sql")
                    .build();
        }
    
        @Bean
        @Profile("persistent")
        public DataSource persistentDataSource() {
            HikariDataSource hikariDataSource = new HikariDataSource();
            // TODO configure datasource
            return hikariDataSource;
        }
    }
    

    如果您使用 -Dspring.profiles.active=in-memory 运行您的应用程序,在您的应用程序上下文中将只有嵌入的数据源 bean,它将由 @EnableBatchProcessing 自动配置的作业存储库使用。 .

    1. Or, would even using Spring Batch for second type of jobs be overkill/not needed at all? Simply a method with @Scheduled annotation would be enough?

    which do not involve much of processing but a simple sql query : 如果是简单的 sql 查询,并且不需要持久化元数据,那么使用带有 @Scheduled 注释的方法可能会更好使用 jdbcTemplate运行查询。

    关于spring - 有没有办法跳过仅针对特定作业的 Spring Batch 持久元数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58536721/

    相关文章:

    java - Hibernate - Bitronix - Spring - 无法征用多个非 XA 资源

    java - Undertow 和 Tomcat 的 Spring Boot .war 上下文路径

    spring-batch - Spring 批处理 : restarting a completed job with same parameters

    java - Spring Boot抽象自动配置问题

    java - Spring OAuth2详解授权服务器配置

    java - 接收器类 o.s.c.b.BootstrapApplicationListener$CloseContextOnFailureApplicationListener 未定义或继承 impl

    spring-boot - SpringBoot JPA错误

    java - Spring Boot 执行器和 Log4j2

    architecture - 微软相当于 Spring Batch

    spring-integration - csv 文件的 Spring Batch : How to set the first line columns (header), 为我的文件阅读器标记名称