Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧靴&x2B;无数据源的spring批处理_Java_Spring_Spring Boot_Spring Batch - Fatal编程技术网

Java 弹簧靴&x2B;无数据源的spring批处理

Java 弹簧靴&x2B;无数据源的spring批处理,java,spring,spring-boot,spring-batch,Java,Spring,Spring Boot,Spring Batch,我正在尝试在SpringBoot项目中配置SpringBatch,我想在没有数据源的情况下使用它。我发现ResourcelessTransactionManager是一个不错的选择,但我无法让它发挥作用。问题是我已经定义了另外3个数据源,但我不想在springBatch中使用它们中的任何一个 我已经检查了默认实现DefaultBatchConfigurer,如果找不到数据源,它将完全按照我的要求执行。问题是我有三个,不想使用任何 请不要建议使用hsql或其他内存数据库,因为我不希望这样。您可以尝

我正在尝试在SpringBoot项目中配置SpringBatch,我想在没有数据源的情况下使用它。我发现
ResourcelessTransactionManager
是一个不错的选择,但我无法让它发挥作用。问题是我已经定义了另外3个数据源,但我不想在springBatch中使用它们中的任何一个

我已经检查了默认实现
DefaultBatchConfigurer
,如果找不到数据源,它将完全按照我的要求执行。问题是我有三个,不想使用任何


请不要建议使用hsql或其他内存数据库,因为我不希望这样。

您可以尝试在@SpringBootApplication中排除DataSourceAutoConfiguration。请参见下面的示例代码

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableBatchProcessing
public class SampleBatchApplication {

@Autowired
private JobBuilderFactory jobs;

@Autowired
private StepBuilderFactory steps;

@Bean
protected Tasklet tasklet() {

    return new Tasklet() {
        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext context) {
            return RepeatStatus.FINISHED;
        }
    };
}

@Bean
public Job job() throws Exception {
    return this.jobs.get("job").start(step1()).build();
}

@Bean
protected Step step1() throws Exception {
    return this.steps.get("step1").tasklet(tasklet()).build();
}

public static void main(String[] args) throws Exception {
    System.exit(SpringApplication.exit(SpringApplication.run(SampleBatchApplication.class, args)));
   }
}
和样本测试类

import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.rule.OutputCapture;
import static org.assertj.core.api.Assertions.assertThat;
public class SampleBatchApplicationTests {
@Rule
public OutputCapture outputCapture = new OutputCapture();

@Test
public void testDefaultSettings() throws Exception {
    assertThat(SpringApplication.exit(SpringApplication.run(SampleBatchApplication.class))).isEqualTo(0);
    String output = this.outputCapture.toString();
    assertThat(output).contains("completed with the following parameters");
  }
}

如果配置中有多个
数据源
(无论是否要使用它们),则需要定义自己的
BatchConfigurer
。这是框架知道在这种情况下该做什么的唯一方法


您可以在此处的文档中阅读有关
BatchConfigurer
的更多信息:

我通过扩展DefaultBatchConfigurer类,使其忽略任何数据源,从而配置了一个基于映射的JobRepository,解决了这个问题

例如:

@Configuration
@EnableBatchProcessing
public class BatchConfig extends DefaultBatchConfigurer {   

    @Override
    public void setDataSource(DataSource dataSource) {
        //This BatchConfigurer ignores any DataSource
    }
}

我们遇到了类似的问题,我们使用的是SpringBootJDBC,我们不想在DB中存储spring批处理表,但我们仍然希望对数据源使用spring的事务管理

我们最终实现了自己的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
}
并将初始值设定项设置为false

spring.batch.initializer.enabled=false

在我的例子中,我将数据持久化到Cassandra。如果您使用的是spring boot starter批处理,那么它将提供一个尚未实现的数据源,但您可以像以下步骤一样欺骗配置:

步骤1:

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class SampleSpringBatchApplication{

    public static void main(String[] args) {
        System.setProperty("spring.devtools.restart.enabled", "true");
        SpringApplication.run(SampleSpringBatchApplication.class, args);
    }

}
步骤2:

    @Configuration
    @EnableBatchProcessing
    public class SampleBatchJob extends DefaultBatchConfigurer {

        //..

        @Override
        public void setDataSource(DataSource dataSource) {
        }

        //..
    }

我认为你想要的是不可能的。至少使用和内存中的DBA,如果我理解正确的话,这应该是可能的。然后手动配置Spring批处理,不要使用自动配置。只需创建自己的
BatchConfigurer
即可。将其注册为bean,批处理将在没有数据源的情况下进行配置。我已经完成了配置,但它仍在寻找数据源,找到其中3个,然后失败。我的回答有帮助吗?我添加了示例类和测试类。它对我有用。因为我不想使用datasource,所以我甚至没有使用ResourcesStransActionManager。当我运行这个程序时,它会像“没有提供数据源…使用基于映射的JobRepository”和“没有设置TaskExecutor,默认为synchronous executor”这样打印出来。没错,但我有3个数据源,但不想将其中任何一个用于批处理。抱歉更新了我的问题。@Majky请尝试这个答案和这个答案:这是我让这个场景工作的唯一方法。不过,为了澄清这一点,您必须完全实现自己的
BatchConfigurer
类。仅仅扩展
DefaultBatchConfigurer
是不够的,因为这仍然会查找
数据源。我刚刚从
DefaultBatchConfigurer
中复制了相关的代码片段。只要您的类被标记为
@组件
,它就应该比其他组件更受欢迎。另一个警告是,我需要使用模块化批处理配置,并控制配置类的加载方式。我在GitHub上放了一个非常简单的演示项目,解释了其中涉及的部分:谢谢你,Nandish,这对我来说很有用。我在Springboot应用程序中尝试了这个。我还必须在主类中的排除列表中添加一个类:
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
这对我来说很有用。
spring.batch.initializer.enabled
已被弃用。可能
spring.batch.initializeschema=never
是替换。