2

In order to speed up the data consumption in my application(Spring Boot, Neo4j database, Spring Data Neo4j 4) I have introduced Apache ActiveMQ and configured 10 concurrent consumers.

Right after that I ran into the issue with a counter updates.

I execute the following createDecisions method from my Apache ActiveMQ consumer :

@Override
public Decision create(String name, String description, String url, String imageUrl, boolean multiVotesAllowed, Long parentDecisionId, User user) {

    Decision parentDecision = null;
    if (parentDecisionId != null) {
        parentDecision = ofNullable(findById(parentDecisionId)).orElseThrow(() -> new EntityNotFoundException("Parent decision with a given id not found"));
    }

    Decision decision = decisionRepository.save(new Decision(name, description, url, imageUrl, multiVotesAllowed, parentDecision, user), user);

    if (parentDecision != null) {
        updateTotalChildDecisions(parentDecision, 1);
    }

    return decision;

}

inside createDecision method I do some logic and then update parentDecision.totalChilDecisions counter:

@Override
public Decision updateTotalChildDecisions(Decision decision, Integer increment) {
    decision.setTotalChildDecisions(decision.getTotalChildDecisions() + increment);

    return decisionRepository.save(decision);
}

After execution in the concurrent environments this counter doesn't match the real things at database but in a single-threaded env(1 ActiveMQ consumer) everything works fine.

I think the main issue is that during totalChildDecisions update the parentDecision refers to the old SDN 4 object with not actual data(totalChildDecisions). How to correctly update parentDecision.totalChildDecisions ?

How to properly synchronize my code in order to get the counters working on the concurrent ActiveMQ consumers ?

alexanoid
  • 24,051
  • 54
  • 210
  • 410

0 Answers0