Cron Scheduler with WebClient



我正在使用弹簧靴。我正试图从一个数据库发送数据到另一个。首先,我通过发出get请求从第一个数据库获取数据,并通过Web Client应用post将数据发送到另一个数据库。它工作!但是,当我试图用@Scheduled注释的cron调度器做这件事时,它不会将数据发布到数据库。虽然这个函数工作得很好,因为我试图通过这个函数打印东西,但是WebClient没有发布数据(也检查了数据,很好)。

Cron类是:

@Component
public class NodeCronScheduler {


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

saveAlltoGraph函数从Product表中获取所有元组,并向图数据库的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);
}

首先,标签"产品";用于获取与该表相关的jparerepository。然后我们从列表中获取该表的所有元组,并据此创建对象(我们可以使用序列化器来获取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调度器开始工作了。

最新更新