Spring boot + spring batch without DataSource

前端 未结 5 764
余生分开走
余生分开走 2020-12-19 00:51

I\'m trying to configure spring batch inside spring boot project and I want to use it without data source. I\'ve found that ResourcelessTransactionManager is th

5条回答
  •  忘掉有多难
    2020-12-19 00:58

    We had the similar problem, we were using spring boot JDBC and we did not want to store spring batch tables in the DB, but we still wanted to use spring's transaction management for our DataSource.

    We ended up implementing own BatchConfigurer.

    @Component
    public class TablelessBatchConfigurer implements BatchConfigurer {
        private final PlatformTransactionManager transactionManager;
        private final JobRepository jobRepository;
        private final JobLauncher jobLauncher;
        private final JobExplorer jobExplorer;
        private final DataSource dataSource;
    
        @Autowired
        public TablelessBatchConfigurer(DataSource dataSource) {
            this.dataSource = dataSource;
            this.transactionManager = new DataSourceTransactionManager(this.dataSource);
    
            try {
                final MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean(this.transactionManager);
                jobRepositoryFactory.afterPropertiesSet();
                this.jobRepository = jobRepositoryFactory.getObject();
    
                final MapJobExplorerFactoryBean jobExplorerFactory = new MapJobExplorerFactoryBean(jobRepositoryFactory);
                jobExplorerFactory.afterPropertiesSet();
                this.jobExplorer = jobExplorerFactory.getObject();
    
                final SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
                simpleJobLauncher.setJobRepository(this.jobRepository);
                simpleJobLauncher.afterPropertiesSet();
                this.jobLauncher = simpleJobLauncher;
            } catch (Exception e) {
                throw new BatchConfigurationException(e);
            }
        }
        // ... override getters
    }
    

    and setting up initializer to false

    spring.batch.initializer.enabled=false
    

提交回复
热议问题