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

无数据源的Spring boot Spring批处理

施知
2023-03-14

我试图在spring boot项目中配置spring批处理,我想在没有数据源的情况下使用它。我发现ResourcelessTransactionManager是一条可行之路,但我无法让它发挥作用。问题是我已经定义了另外3个数据源,但我不想在springBatch中使用其中任何一个。

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

请不要建议使用hsql或其他内存DB,因为我不想这样。

共有3个答案

史逸春
2023-03-14

您可以尝试在@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");
  }
}
宇文温文
2023-03-14

在我的情况下,我将数据持久化到Cassandra。如果您使用的是spring-boot-starter-批处理,它预计会提供一个尚未实现的DataSource,但您可以像以下步骤一样欺骗配置

第一步:

@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);
    }

}

Step2:

    @Configuration
    @EnableBatchProcessing
    public class SampleBatchJob extends DefaultBatchConfigurer {

        //..

        @Override
        public void setDataSource(DataSource dataSource) {
        }

        //..
    }
孔弘盛
2023-03-14

我绕过了这个问题,扩展了DefaultBatchConfigurer类,使其忽略任何数据源,因此它将配置一个基于地图的JobRepository。

示例:

@Configuration
@EnableBatchProcessing
public class BatchConfig extends DefaultBatchConfigurer {   

    @Override
    public void setDataSource(DataSource dataSource) {
        //This BatchConfigurer ignores any DataSource
    }
}
 类似资料:
  • 我需要访问两个数据源: Spring批处理存储库:在内存H2中 我的步骤需要访问。 我在那里看到了几个关于如何创建自定义

  • 我有一个Spring Boot(面向批处理)应用程序,它使用一个数据源来完成批处理作业,并将内容写入数据库。 我在类似: 问题是,当我尝试将数据源注入一个Spring配置文件时: ...它告诉我: 无法自动连线。存在多个“DataSource”类型的bean。 Beans:数据源 我还尝试注入数据源,例如: ...但是没有运气:(,尽管这两个数据源的问题最终消失了。 有什么线索可以“绕过”吗?

  • 我试图在Spring批处理中配置几个数据源。启动时,Spring批处理抛出以下异常: 批处理配置的代码段 不知道为什么我会看到这个异常,因为我看到了一些基于xml的Spring批处理配置,这些配置声明了多个数据源。我使用的是Spring批处理核心版本3.0.1.发行版和Spring Boot版本1.1.5.发行版。如有任何帮助,将不胜感激。

  • 我正在编写Spring批的Spring Boot应用程序,其中ItemReader从Oracle数据库读取数据并将数据写入postgres sql,但我得到了以下错误 我不想创建spring批处理元数据表,我的应用程序不需要监视作业,请就此向我提出建议。提前谢谢!!

  • 基于Spring boot和batch的项目。我需要使用h2数据库来存储spring批处理表的元数据,以及spring JPA的postgre(由ItemWriter使用)。 我在属性文件中定义了两个独立的属性。 现在,在@配置文件中,为了链接我定义的批处理数据源, 但是,我没有在其他任何地方使用另一个jpa(postgre)db配置(计划将其与JPA链接)。 当我试图运行的项目,我得到下面的异常

  • 我有一个spring批处理应用程序,可以将5M条记录从一个文件加载到SQL Server数据库中。我有根据国家代码区分的不同数据源。当我使用带有@primary注释的单个数据源时,spring batch writer在5分钟内写入5M条记录。但是,当我使用@bean注释给出多个数据源并使用一个非主数据源将文件数据写入数据库时,perforamnce变得非常慢,对于同样的500万条记录,大约需要1