spring-batch 相关问题

Spring Batch是一个轻量级,全面的框架,旨在实现对企业系统日常运营至关重要的批处理应用程序的开发。此上下文中的批处理应用程序是指针对批量数据处理的自动离线系统。

我如何知道在 Spring Batch 步骤开始时要处理的块/项目的总数?

我想尝试一下我的批处理作业,并为我的批处理作业使用进度条,就像这里的一样:https://github.com/ctongfei/progressbar 要准确填充进度条,例如...

回答 3 投票 0

如何解决JobBuilderFactory和StepBuilderFactory的弃用警告

我使用以下工厂来设置我的 Spring Batch 应用程序: 私有 JobBuilderFactory jobBuilderFactory; 私有 StepBuilderFactory stepBuilderFactory; 但是,我得到了以下信息...

回答 2 投票 0

在单线程Spring批处理作业中读取和更新同一个Oracle表有什么问题吗?

我编写了一个单线程 Spring Batch 作业来修复表列中的数据。 也就是说,这是一项简单的工作,没有并行或多线程步骤。它只是使用 JdbcCursorItemReader 来“流&

回答 1 投票 0

如何在不破坏自动配置的情况下自定义 Spring Batch 默认配置

如何在不破坏SpringBootBatchConfiguration中Spring Boot提供的自动配置的情况下自定义Spring Batch的DefaultBatchConfiguration? 我正在 Sprin 中使用 Spring Batch 作业...

回答 1 投票 0

启动第一个作业会导致 - 声明多个 DirectChannel 时调度程序没有频道异常订阅者

我有一个 Spring Batch 应用程序,它成功运行 Spring Batch 作业,但在声明多个 DirectChannel 时出现异常。 当我启动“firstJob&...

回答 1 投票 0

Spring Batch 5 - 不希望它在数据库中自动创建表

我正在从 Spring Boot 2 迁移到 Spring Boot 3,并从 Spring Batch 4 迁移到 Spring Batch 5。我不希望 Spring Batch 在我的数据库中创建 BATCH_JOB_INSTANCE 表。 @Autowired 私人

回答 1 投票 0

Spring batch5不在数据库中创建表

我正在从 Spring Boot2 迁移到 Spring Boot3,从 Spring Batch4 迁移到 Spring Batch5。我不希望 Spring Batch 在我的数据库中创建 BATCH_JOB_INSTANCE 表。 @Autowired 私有数据源

回答 1 投票 0

如何在 Spring Batch 中从 ItemReader 访问作业参数?

这是我的 job.xml 的一部分: 这是我的一部分job.xml: <job id="foo" job-repository="job-repository"> <step id="bar"> <tasklet transaction-manager="transaction-manager"> <chunk commit-interval="1" reader="foo-reader" writer="foo-writer" /> </tasklet> </step> </job> 这是项目阅读器: import org.springframework.batch.item.ItemReader; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("foo-reader") public final class MyReader implements ItemReader<MyData> { @Override public MyData read() throws Exception { //... } @Value("#{jobParameters['fileName']}") public void setFileName(final String name) { //... } } 这就是 Spring Batch 在运行时所说的: Field or property 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' 这里出了什么问题?在哪里可以阅读有关 Spring 3.0 中这些机制的更多信息? 如前所述,您的读者需要处于“步骤”范围内。您可以通过 @Scope("step") 注释来完成此操作。如果您将该注释添加到阅读器中,它应该对您有用,如下所示: import org.springframework.batch.item.ItemReader; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("foo-reader") @Scope("step") public final class MyReader implements ItemReader<MyData> { @Override public MyData read() throws Exception { //... } @Value("#{jobParameters['fileName']}") public void setFileName(final String name) { //... } } 默认情况下此范围不可用,但如果您使用 batch XML 命名空间,则该范围将可用。如果不是,则根据 Spring Batch 文档,将以下内容添加到 Spring 配置中将使范围可用: <bean class="org.springframework.batch.core.scope.StepScope" /> 如果您想在单个 JavaConfig 类中定义 ItemReader 实例和 Step 实例。您可以使用 @StepScope 和 @Value 注释,例如: @Configuration public class ContributionCardBatchConfiguration { private static final String WILL_BE_INJECTED = null; @Bean @StepScope public FlatFileItemReader<ContributionCard> contributionCardReader(@Value("#{jobParameters['fileName']}")String contributionCardCsvFileName){ .... } @Bean Step ingestContributionCardStep(ItemReader<ContributionCard> reader){ return stepBuilderFactory.get("ingestContributionCardStep") .<ContributionCard, ContributionCard>chunk(1) .reader(contributionCardReader(WILL_BE_INJECTED)) .writer(contributionCardWriter()) .build(); } } 技巧是将 null 值传递给 itemReader,因为它将通过 @Value("#{jobParameters['fileName']}") 注释注入。 感谢 Tobias Flohre 的文章:Spring Batch 2.2 – JavaConfig 第 2 部分:JobParameters、ExecutionContext 和 StepScope 很晚了,但你也可以通过注释 @BeforeStep 方法来做到这一点: @BeforeStep public void beforeStep(final StepExecution stepExecution) { JobParameters parameters = stepExecution.getJobExecution().getJobParameters(); //use your parameters } 为了能够使用 jobParameters,我认为您需要将读者定义为范围“步骤”,但我不确定您是否可以使用注释来做到这一点。 使用 xml-config 它会像这样: <bean id="foo-readers" scope="step" class="...MyReader"> <property name="fileName" value="#{jobExecutionContext['fileName']}" /> </bean> 请参阅 Spring Batch 文档进一步了解。 也许它可以通过使用 @Scope 并在 xml 配置中定义步骤范围来工作: <bean class="org.springframework.batch.core.scope.StepScope" /> 补充一个额外的示例,您可以访问 JavaConfig 类中的所有作业参数: @Bean @StepScope public ItemStreamReader<GenericMessage> reader(@Value("#{jobParameters}") Map<String,Object> jobParameters){ .... } 执行作业时我们需要传递作业参数如下: JobParameters jobParameters= new JobParametersBuilder().addString("file.name", "filename.txt").toJobParameters(); JobExecution execution = jobLauncher.run(job, jobParameters); 通过使用表达式语言,我们可以按如下方式导入值: #{jobParameters['file.name']} ‘技术’写作。 配置类。 @Autowired @Qualifier("testReader") private testReader reader; @Bean(name = "testJob") public Job testJob(@Autowired @Qualifier("testStep") Step step) { return jobBuilderFactory .get("testJob") .incrementer(new RunIdIncrementer()) // .listener(new JobCompletionListener()) .start(step) .build(); } @Bean("testStep") @JobScope public Step testStep(@Value("#{jobParameters['key']}") String key) { return stepBuilderFactory.get("testStep") .<UniqueUserVO, List<UniqueUser>>chunk(500) .reader(reader.setKey(key).reader()) .processor(processor.processor()) .writer(writer.writer()) .build(); } 阅读器接口类。 public interface Reader<T> { /** * reader * @return */ ItemReader<T> reader(); } 读书班 @Component public class TestReader implements Reader<UniqueUserVO> { private String key; public TestReader setKey(String key) { this.key= key; return this; } @Override public ItemReader<UniqueUserVO> reader() { xxxxx } } 让我们考虑一个场景,您需要访问 ItemWriter 或 ItemReader 中的 JobParameters 来设置写入的文件路径等值。在这种情况下,您可以利用 JobParameterExecutionContextCopyListener。 假设您有一个非 bean 类,您可以在其中创建 STEP 读取器/编写器。然后,您可以创建一个 JobParameterExecutionContextCopyListener 并指定要提取的键,例如 myDate: private TaskletStep internalBuild() { try { JobParameterExecutionContextCopyListener listener = new JobParameterExecutionContextCopyListener(); listener.setKeys(new String []{"myDate"}); return new StepBuilder("MY_STEP", jobRepository) .<T, T>chunk(batchSize, transactionManager) .reader(itemReader()) .processor(new PassThroughItemProcessor<>()) .writer(itemWriter()) .listener(listener) .build(); } catch (IOException e) { throw new SqlToCsvException(e); } } 接下来,在您的编写器(例如,FlatFileItemWriter)中,如果您想根据从 JobParameters 获取的日期设置资源,您可以重写 open 方法来处理此逻辑: public FlatFileItemWriter itemWriter() { FlatFileItemWriter writer = new FlatFileItemWriter<T>(){ // Override the resource given from the executionContext. @Override public void open(ExecutionContext executionContext) throws ItemStreamException { this.setResource(new FileSystemResource(getMyFileNameFromDate((LocalDate) executionContext.get("myDate")))); super.open(executionContext); } }; // Other configurations for the writer... return writer; } 这种方法允许您根据作业执行期间提供的作业参数(例如日期)动态设置资源(在本例中为文件路径)。 这可能是一种更简单的方法: @Configuration @Setter @StepScope public class Reader extends FlatFileItemReader<Object> { public Reader(@Value("#{jobParameters['filePath']}") String resource){ setResource(new FileSystemResource(resource)); } } 您可以在项目阅读器内使用 @BeforeStep 注释进行注释的方法中使用 StepExecution 上下文来获取作业参数并将其设置为变量,您可以在 read 方法中使用该变量。 就我而言,我写了这样的东西:- @Component @RequiredArgsConstructor public class SpelItemReader implements ItemReader<BatchRequest>{ private String requestId; @Override public BatchRequest read() { //access the request-id variable here } @BeforeStep public void beforeStep(StepExecution stepExecution) { requestId = stepExecution.getJobParameters().getString("requestId"); } } 您是否将作业参数正确声明为 bean 的映射? 或者您是否意外地实例化了一个 JobParameters 对象,该对象没有文件名的 getter? 有关表达式语言的更多信息,您可以在 Spring 文档中找到信息这里。

回答 11 投票 0

仅使用一条消息并执行远程步骤

我正在做一个 Spring Batch 应用程序,其中经理和工作人员使用 ActiveMQ Arterims 进行远程分区。我希望我的工作人员仅在完成远程操作后才使用队列中的消息...

回答 1 投票 0

为什么 Spring Boot 忽略我的 schema.sql 文件?

org.springframework.boot spring-boot-starter-parent 3.2.2 ...

回答 1 投票 0

SpringBatch ItemRepositoryReader,如何更改 JPARepository 中的数据源?

我正在使用 SpringBatch 和 JPARepository。 我有 2 个连接 DB2 和 Postgres。 我需要切换 JpaRepository 内的数据源 因此,为了保存数据源上下文,我遵循...

回答 1 投票 0

Spring Integration 和 Spring Batch 事务管理器冲突

我正在使用 Spring 集成流程从文件服务器读取文件。我的应用程序将在多个实例上运行。我想确保每个实例仅处理任何文件一次。这就是我

回答 1 投票 0

使用Spring Batch调用存储过程通过jdbc添加/更新记录

我有一个任务要调度并触发数据库调用,并且此数据库调用返回的数据必须批量发送以调用存储过程。我怎样才能使用 Spring Batch 和 JDBC 来实现这一点

回答 1 投票 0

bean 如何从类的 @Bean 注解方法返回

我正在开发几年前由以前的团队开发的Spring批处理程序。 主要的两个文件如下。 RewardsAccumulationReaderConfig 类实现 ItemReader 接口,并带有 r...

回答 1 投票 0

Spring Batch 5 中的多个数据源

在 Spring 4 中,可以使用 BatchConfigurer 将 JobRepository 指向预期的 DataSource。 但到了 Spring 5,我迷失了。 我在 application.properties 中有 spring.datasource.* 属性...

回答 2 投票 0

Spring Batch 5 和 Cucumber(BDD) 集成

我们正在迁移到 Spring 第 5 批和 Spring Boot 3.2.2。 由于 Spring Batch 不创建表,我们现有的 Cucumber(BDD) 测试用例失败 出现以下错误: org.h2.jdbc.

回答 1 投票 0

如何使用BeanWrapperFieldSetMapper来映射字段子集?

我有一个 Spring 批处理应用程序,其中 BeanWrapperFieldSetMapper 用于使用原型对象映射字段。但是,正在读取的 CSV 文件(通过 FlatFileItemReader)包含一个 (

回答 4 投票 0

根据记录类型使 JMSTemplate 中的队列名称动态化

我在 Spring Batch 应用程序中使用 JSMItemWriter。 是否可以在运行时动态设置队列名称或者动态定义一个jmsTemplate,而不是在代码中将其定义为bean

回答 1 投票 0

Spring Batch - EDI 文件阅读器

我想在Spring Batch中读取EDI文件并将其转换为POJO。我找不到任何与 Spring Batch 一起使用的示例。 我的 Edi 文件最多可以包含数百个片段...

回答 1 投票 0

我们可以在 Spring Batch Processor 中一起处理整个块吗

我有一个场景,员工暂存表中有数百万条记录,我需要丰富该值并将其存储在员工最终表中。现在我正在使用块大小为 10,000 的块处理....

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.