想要在没有数据源的情况下配置Job

时间:2018-11-09 19:04:40

标签: spring-boot spring-batch

当在SpringbatchConfiguration类中配置了数据源时,我能够运行Springbatch应用程序。但是我不希望配置数据源。因此,我使用了 ResourcelessTransactionManager 。请参阅下面的配置类。有人指导我如何在不将数据源配置为Batchjob配置的一部分的情况下启动Jobs。

@Configuration
@EnableBatchProcessing
@EnableAutoConfiguration
//@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class SprintgBatchConfiguration {

    /*@Autowired
    private DBConfiguration dbConfig;*/

    /*@Autowired
    private DataSource dataSource;

    @Autowired
    private DataSourceTransactionManager transactionManager;
    */
    //Tomcat relaated configuration//
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("124MB");
        factory.setMaxRequestSize("124MB");
        return factory.createMultipartConfig();
    }

    @Bean(name="csvjob")
    public Job job(JobBuilderFactory jobBuilderFactory,StepBuilderFactory stepBuilderFactory,ItemReader<List<CSVPojo>> itemReader,ItemProcessor<List<CSVPojo>,CsvWrapperPojo> itemProcessor,AmqpItemWriter<CsvWrapperPojo> itemWriter){
        Step step=stepBuilderFactory.get("ETL-CSV").<List<CSVPojo>,CsvWrapperPojo>chunk(100)
                .reader(itemReader)
                .processor(itemProcessor)
                .writer(itemWriter)
                .build();



        Job csvJob= jobBuilderFactory.get("ETL").incrementer(new RunIdIncrementer())
        .start(step).build();

        return csvJob;
    }

    @Bean(name="exceljob")
    public Job jobExcel(JobBuilderFactory jobBuilderFactory,StepBuilderFactory stepBuilderFactory,ItemReader<List<ExcelPojo>> itemReader,ItemProcessor<List<ExcelPojo>,ExcelWrapperPojo> itemProcessor,AmqpItemWriter<ExcelWrapperPojo> itemWriter){
        Step step=stepBuilderFactory.get("ETL-Excel").<List<ExcelPojo>,ExcelWrapperPojo>chunk(100)
                .reader(itemReader)
                .processor(itemProcessor)
                .writer(itemWriter)
                .build();

        Job ExcelJob= jobBuilderFactory.get("ETL-Excel").incrementer(new RunIdIncrementer())
        .start(step).build();

        return ExcelJob;
    }

    /*@Override
    public void setDataSource(DataSource dataSource){
        System.out.println("overriden");
    }*/

    /*@Bean
    public FlatFileItemReader<CSVPojo> fileItemReader(Resource resource){

        return null;
    }*/
    /*@Bean(name="dataSource")
    public DataSource dataSource() throws SQLException
    {


        //BasicDataSource  dataSource = new BasicDataSource();

        return dataSource;

    }*/
    @Bean(name="transactionManager")
    public ResourcelessTransactionManager transactionManager() throws SQLException{

        return new ResourcelessTransactionManager();

    }
    /*@Bean(name="transactionManager")
    public DataSourceTransactionManager transactionManager() throws SQLException{

        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(this.dataSource());
        return transactionManager;

    }*/


    /*@Bean
    public JobRepository jobRepository() throws Exception{
        JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
        factoryBean.setDatabaseType("ORACLE");
        factoryBean.setDataSource(dataSource);
        factoryBean.setTransactionManager(transactionManager);
        factoryBean.setIsolationLevelForCreate("ISOLATION_READ_UNCOMMITTED");
        return factoryBean.getObject();

    }*/

    @Bean
    public JobRepository jobRepository(ResourcelessTransactionManager transactionManager) throws Exception {
        MapJobRepositoryFactoryBean mapJobRepositoryFactoryBean = new MapJobRepositoryFactoryBean(transactionManager);
        mapJobRepositoryFactoryBean.setTransactionManager(transactionManager);
        return mapJobRepositoryFactoryBean.getObject();
    }


}

但是我在运行应用程序时遇到了异常。

ationConfigEmbeddedWebApplicationContext:上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ fileProcessController”的bean时出错:未通过字段“ jobLauncher”表示依赖关系;嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration”的bean时出错:通过字段“ dataSources”表示的依赖关系未满足;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建在类路径资源[org / springframework / boot / autoconfigure / jdbc / DataSourceConfiguration $ Tomcat.class]中定义的名称为“ dataSource”的bean时出错:通过工厂方法实例化Bean失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.apache.tomcat.jdbc.pool.DataSource]:工厂方法'dataSource'引发了异常;嵌套的异常是org.springframework.boot.autoconfigure.jdbc.DataSourceProperties $ DataSourceBeanCreationException:无法确定数据库类型NONE的嵌入式数据库驱动程序类。如果您想要嵌入式数据库,请在类路径上放置一个受支持的数据库。如果您要从特定配置文件加载数据库设置,则可能需要激活它(当前没有配置文件处于活动状态)。

提前谢谢!!!!

1 个答案:

答案 0 :(得分:1)

Spring Boot适用于构建生产级应用程序。当用于构建Spring Batch应用程序时,它需要数据源来保留Spring Batch元数据(请参见BATCH-2704)。

但是您始终可以使用以下任何一种方式:

  • Spring Boot支持的嵌入式数据源(H2,HSQL或Derby),只需将其添加到类路径中即可。此数据源将由Spring Batch自动提取
  • 或提供自定义BatchConfigurer并使用MapJobRepository(请参阅here

希望这会有所帮助。