带有 WebClient 的 Cron 调度程序

问题描述

我正在使用弹簧靴。我正在尝试将数据从一个数据库发送到另一个数据库。 首先,我通过发出一个 get 请求从第一个数据库获取数据并通过 Web 客户端应用 post 将数据发送到另一个数据库来做到这一点。有效! 但是,当我尝试使用带有 @Scheduled 注释的 cron 调度程序执行此操作时,它并没有将数据发布到数据库中。即使该函数运行良好,因为我尝试通过该函数打印内容,但 WebClient 没有发布数据(也检查了数据,很好)。

Cron 类是:

@Component
public class NodeCronScheduler {
    
    
    @Autowired
    GraphService graphService;

    @Scheduled(cron = "*/10 * * * * *")
    public void createallNodesFiveSeconds()
    {
        graphService.saveAlltoGraph("Product");
    }

}

saveAlltoGraph 函数从 Product 表中获取所有元组,并将 post 请求发送到图数据库的 api,从而从元组生成节点。

功能如下:

public Mono<Statements> saveAlltoGraph(String label) {
        JpaRepository currentRepository = repositoryService.getRepository(label);
        List<Model> allModels = currentRepository.findAll();
        Statements statements = statementService.createallNodes(allModels,label);
        //System.out.println(statements);
        return webClientService.sendStatement(statements);
    }

首先,标签“Product”用于获取与该表相关的 JpaRepository。然后我们获取列表中该表的所有元组,并根据它创建对象,(我们可以使用序列化程序来获取 JSON)。

这是sendStatement函数

public Mono<Statements> sendStatement(Statements statements){
        System.out.println(statements);
        return webClient.post().uri("http://localhost:7474/db/data/transaction/commit")
                .body(Mono.just(statements),Statements.class).retrieve().bodyToMono(Statements.class);
    }

当我们使用 get 请求映射调用此 saveAlltoGraph 时,一切正常,但不能与调度程序一起使用。

解决方法

我尝试将 .block() 和 .subscribe() 添加到其中。然后开始使用 cron 调度程序。