Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppression timeout sur lecture des messages suite à fausse manip #94

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 4 additions & 36 deletions src/main/java/fr/abes/bestppn/kafka/TopicConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Service
Expand Down Expand Up @@ -67,40 +69,6 @@ public void kbartFromkafkaListener(ConsumerRecord<String, String> ligneKbart) {
//nouveau fichier trouvé dans le topic, on initialise les variables partagées
workInProgress.put(filename, new KafkaWorkInProgress(ligneKbart.key().contains("_FORCE"), ligneKbart.key().contains("_BYPASS")));
}
if (conditionOnLastMessage(filename, ligneKbart))
traiterMessage(ligneKbart, filename);
else {
scheduleTimeOutCheck(filename);
}
}

/**
*
* @param filename
* @param ligneKbart
* @return
*/
private boolean conditionOnLastMessage(String filename, ConsumerRecord<String, String> ligneKbart) {
return workInProgress.get(filename).getNbLignesTraitees().get() <= Integer.parseInt(new String(ligneKbart.headers().lastHeader("nbLinesTotal").value()));
}

/**
* Méthode permettant d'effectuer certaines actions si un message n'est pas reçu au bout d'un certain temps sur un fichier donné
*/
private void scheduleTimeOutCheck(String filename) {
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
scheduledExecutorService.schedule(() -> {
//si pas de message reçu dans les 5 minutes suivant la réception du message précédent, on supprime l'objet temporaire, et on lève une erreur
workInProgress.get(filename).setIsOnError(true);
log.error("Une erreur s'est produit dans le processus de traitement des messages, veuillez relancer le traitement.");
if (workInProgress.get(filename).getSemaphore().tryAcquire()) {
handleFichier(filename);
}
}, 5, TimeUnit.SECONDS);

}

private void traiterMessage(ConsumerRecord<String, String> ligneKbart, String filename) {
try {
//traitement de chaque ligne kbart
LigneKbartDto ligneKbartDto = mapper.readValue(ligneKbart.value(), LigneKbartDto.class);
Expand Down Expand Up @@ -151,8 +119,8 @@ private void traiterMessage(ConsumerRecord<String, String> ligneKbart, String fi
workInProgress.get(filename).addLineKbartToMailAttachementWithErrorMessage(new LigneKbartDto(), e.getMessage());
workInProgress.get(filename).addNbLinesWithInputDataErrorsInExecutionReport();
}
}

}

private void handleFichier(String filename) {
//on attend que l'ensemble des threads aient terminé de travailler avant de lancer le commit
Expand Down
Loading