cron调度程序

v440hwme  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(286)

我在用Spring Boot工作。我正在尝试将数据从一个数据库发送到另一个数据库。首先,我通过发出get请求从第一个数据库获取数据,并通过web客户端应用post将数据发送到另一个数据库。成功了!但是,当我尝试使用cron scheduler和@scheduled annotation来完成时,它并没有将数据发布到数据库。尽管该函数运行良好,因为我尝试通过该函数打印内容,但是webclient没有发布数据(也检查了数据,很好)。
cron类是:

@Component
public class NodeCronScheduler {

    @Autowired
    GraphService graphService;

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

}

savealltograph函数从一个产品表中提取所有元组,并向图形数据库的api发送post请求,由元组生成节点。
函数如下:

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请求Map调用savealltograph时,一切都正常,但不使用调度器。

cgh8pdjw

cgh8pdjw1#

我试着添加.block()和.subscribe()。开始使用cron调度器。

相关问题