Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

java - Cron Scheduler with WebClient

I am working with a spring boot. I am trying to send data from one database to the other. First, I did this by making a get request to get the data from the first database and applied post through Web Client to send the data to the other database. It worked! But when I tried to do it with cron scheduler with @Scheduled annotation it's not posting the data to the database. Even though the function is working fine, as i tried printing stuff through that function, but the WebClient is not posting the data (also checked the data, it was fine).

The Cron class is:

@Component
public class NodeCronScheduler {
    
    
    @Autowired
    GraphService graphService;

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

}

saveAlltoGraph function takes all the tuples from a Product table and send post request to the api of graph database, which makes node from the tuples.

Here is the function:

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);
    }

First, the label "Product" is used to get the JpaRepository related to that table. Then we fetch all the tuples of that table in the list, and we create objects according to that, (We can use a serializer to get the JSON).

Here is the sendStatement function:

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);
    }

Everything is working when we call this saveAlltoGraph using a get request mapping, but not working with the scheduler.

question from:https://stackoverflow.com/questions/65869560/cron-scheduler-with-webclient

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I tried with adding .block() and .subscribe() to that. And things started working with the cron scheduler.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...