Spring Cassandra存储库-将记录保存在后台线程中

时间:2018-08-22 18:52:55

标签: java spring cassandra

我一直在尝试在后台线程中的数据库中创建记录,但是在控制台中没有任何响应(没有错误,异常或日志)。

下面是代码


在我的spring组件中,我有:

ExecutorService tPool = Executors.newFixedThreadPool(15);

//This is a repository that extends CassandraRepository
@Autowired
MyRepository myRepository;

CompletableFuture<Boolean> myBool = CompletableFuture.supplyAsync(() -> {

            //processing here

            return new doSomeProcessing(arg1, arg2, arg3).process();
        }, tPool);

myBool.whenComplete((isTrue, throwThis) -> {
   if(isTrue) {
      // do something
   }
});

在我的doSomeProcessing类中,我具有方法process():

public boolean process() {

//This appears in the console
LOG.info("About to try and save the record on the background thread");

//After setting the repository in the thread class constructor
myRepository.save(arg1, arg2, arg3);

//This doesn't appear in the console
LOG.info("The record should be saved");

}

但是数据库不显示任何新记录,并且控制台不显示任何错误或异常或最后一个日志语句。

您将如何使用Spring和Cassandra在后台线程上保存记录?

任何解释都将不胜感激。

我已经使用异步服务,事务性事务以及其他一些方法查看并尝试了以下方法: How do I use JpaRepository in a backend thread?

How do I properly do a background thread when using Spring Data and Hibernate?

1 个答案:

答案 0 :(得分:1)

CompletableFuture异常完成时,由于仍然未处理异常,因此没有堆栈跟踪。它会一直存储到用户执行“激活”该异常的操作为止。例如,调用get()将直接引发该异常。

使用CompletableFuture做更复杂的事情时,异常会随将来的结果一起存储(因此BiConsumer会将结果和异常作为参数),但是要由您检查是否存在一个异常并进行处理。

由于您可以链接期货并因此遇到多个例外,因此最终得到如下文档:

  

如果提供的操作本身遇到异常,则   除非有此例外,否则返回阶段将异常完成   阶段也异常完成。

如果您在初读时了解到这一点,就很有才华。