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

Spring批处理作业存储库

阚吕恭
2023-03-14

我已经开始探索Spring Batch,并遇到了一些基本问题。

共有1个答案

丌官向荣
2023-03-14

如果您使用的是Spring Boot,那么就像这样做。注下面我们正在配置getDatasource()方法,它提供我们需要的数据源。这将强制引导不使用默认数据源。

package com.demo.configuration;

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.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

import javax.sql.DataSource;

/**
 * Created by Sushil Behera on 12/26/2017.
 */
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job job1(){
        return jobBuilderFactory.get("job1")
            .start(step1())
            .build();
    }

    @Bean
    public Step step1(){
        return stepBuilderFactory.get("job1step1")
            .tasklet(
                    new Tasklet(){
                        @Override
                        public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                            System.out.println("Tasklet executed");
                            return RepeatStatus.FINISHED;
                        }
                    }
            ).build();
    }

    @Bean
    @ConfigurationProperties(prefix = "demo.datasource")
    public DataSource getDatasource(){
        return new SimpleDriverDataSource();
    }

}

Application.Properties

spring.application.name=Demo
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.url=URL1
spring.datasource.username=ABC
spring.datasource.password=ABC1
demo.datasource.url=URL2
demo.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
demo.datasource.username=XYZ
demo.datasource.password=XYZ1
 类似资料:
  • 我正在创建一个由groovy bean builder支持的spring批处理(spring boot)。但是,我无法找到以下xml结构的正确语法: 更新:豆子。棒极了 我想使用选项1技术,如果我这样做,我会得到“beanName must not empty”错误。我没有使用选项2中的bean,而是它们似乎在工作。 我假设使用选项1,将使用其他定义的bean自动配置jobRepository等。

  • 我有以下工作要处理在一定的时间间隔或特别的基础上。 作业中的步骤如下: 我也想要用户界面,在那里我可以触发一个特别的基础上的工作,而且我应该能够提供参数从用户界面。 我想用Spring batch来完成这个任务,但它更多的是用于读->处理->写之类的工作。这里,在第一步中,我正在生成由第二步读取的数据。我不确定我是否还可以使用Spring batch来实现这个,或者有更好的方法来实现这个。

  • 这似乎是一个愚蠢的问题。我正在尝试为Spring Batch作业存储库(Spring Batch 2.1.7)配置Oracle10g数据库,我能够使用在core中的org/spring框架/批/core/schema-oracle10g.sql可用的脚本创建表。我还将属性batch.data.source.init设置为false。 在干净的数据库上,我的批处理程序运行良好,成功地创建了所有批处理

  • 我正在使用sping-xd通过批处理作业进行数据摄取。大量作业在4个容器中并行运行。任何地方都在10到40个作业之间。其中大多数在不到一分钟的时间内完成。我使用redis(而不是Rabbitmq)和mysql进行数据存储。Spring-xd-批处理使用不同的mysql-db进行作业/步骤统计,我的应用程序使用不同的mysql-db用于自己的目的。两个mysql-db都在同一台服务器上。所有4个容器