【问题标题】:Spring Batch + Spring Boot + CouchbaseSpring Batch + Spring Boot + Couchbase
【发布时间】:2018-11-12 13:31:59
【问题描述】:

我正在尝试创建一个 tasklet 并写入 couchbase。

@Configuration
@EnableBatchProcessing
public class BatchConfiguration extends 
   DefaultBatchConfigurer {
@Autowired
private JobBuilderFactory jobBuilders;

@Autowired
private StepBuilderFactory stepBuilders;

  @Bean
public Step updatedatabaseStep(){
return stepBuilders.get("updatedatabaseStep").tasklet(new UpdateLocalDbStep()).build();}

我的步骤

public class UpdateLocalDbStep implements Tasklet, StepExecutionListener {
@Autowired
private CouchBaseRepository 
   couchBaseRepository;
  @Override
public RepeatStatus execute(StepContribution arg0, ChunkContext chunkContext) throws Exception {
couchBaseRepository.save(this.table);
return RepeatStatus.FINISHED;
}

我的 Couchbase 服务

@Repository
@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "ownerDetails")
public class CouchBaseRepository {}

每次我在执行声明 CouchBaseRepository 为空的步骤时收到空指针异常

2018-11-12 18:59:10.435  INFO 56531 --- [                   
main] c.p.r.s.impl.steps.UpdateLocalDbStep     
: Updating local db with lated table 
info_incr
2018-11-12 18:59:10.440 ERROR 56531 --- [           
main] o.s.batch.core.step.AbstractStep         
: Encountered an error executing step 
updatedatabaseStep in job findOwnerJob

java.lang.NullPointerException: null
at 

com.restbatchApi.service.impl.steps.UpdateLocalDbStep.execute(UpdateLocalDbStep.java:58) ~[classes/:na] 在 org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:407) ~[spring-batch-core-4.1.0.RELEASE.jar:4.1.0.RELEASE] 在 org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:331) ~[spring-batch-core-4.1.0.RELEASE.jar:4.1.0.RELEASE] 在 org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140) ~[spring-tx-5.1.2.RELEASE.jar:5.1.2.RELEASE]

【问题讨论】:

  • 我们必须做一些批处理,而我刚刚开始了解 spring-batch。我们还有一个 couchbase 数据库。据我了解,spring-batch 并没有开箱即用地支持沙发底座。您在使用 spring batch + couchbase 时遇到了哪些挑战。还有你是如何处理jobrepository bean的,尤其是事务管理器(因为它们在couchbase中没有事务的概念)。

标签: spring spring-boot spring-batch couchbase


【解决方案1】:

您正在使用 new UpdateLocalDbStep() 创建 tasklet,因此该实例不由 Spring 容器管理,因此不会注入该对象的依赖项。

您需要将 tasklet 声明为 bean:

@Bean
public Tasklet tasklet() {
   return new UpdateLocalDbStep();
}

并在您的步骤定义中引用它:

@Bean
public Step updatedatabaseStep(){
   return stepBuilders.get("updatedatabaseStep").tasklet(tasklet()).build();
}

作为旁注,我建议为您的 tasklet 注入构造函数(使用 CouchBaseRepository 作为参数创建构造函数)。

【讨论】:

  • 感谢@Mahmoud Ben Hassine
猜你喜欢
  • 2018-07-10
  • 1970-01-01
  • 2017-09-27
  • 2018-06-13
  • 2016-10-10
  • 2015-07-07
  • 2018-01-17
  • 2019-06-06
  • 2015-10-27
相关资源
最近更新 更多