From 17121477a298f14056daa0593535e7d29dfa3a17 Mon Sep 17 00:00:00 2001 From: Pim van Nierop Date: Mon, 8 Jul 2024 15:14:06 +0200 Subject: [PATCH] Update exception handling by MessageJob --- .../factory/NotificationHandlerFactory.java | 1 - .../service/scheduler/quartz/MessageJob.java | 66 +++++++++++-------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/radarbase/appserver/service/questionnaire/protocol/factory/NotificationHandlerFactory.java b/src/main/java/org/radarbase/appserver/service/questionnaire/protocol/factory/NotificationHandlerFactory.java index 85b2c6f3..7c883495 100644 --- a/src/main/java/org/radarbase/appserver/service/questionnaire/protocol/factory/NotificationHandlerFactory.java +++ b/src/main/java/org/radarbase/appserver/service/questionnaire/protocol/factory/NotificationHandlerFactory.java @@ -22,7 +22,6 @@ package org.radarbase.appserver.service.questionnaire.protocol.factory; -import org.radarbase.appserver.service.FcmNotificationService; import org.radarbase.appserver.service.questionnaire.protocol.ProtocolHandler; import org.radarbase.appserver.service.questionnaire.protocol.DisabledNotificationHandler; import org.radarbase.appserver.service.questionnaire.protocol.SimpleNotificationHandler; diff --git a/src/main/java/org/radarbase/appserver/service/scheduler/quartz/MessageJob.java b/src/main/java/org/radarbase/appserver/service/scheduler/quartz/MessageJob.java index 94647e3c..79170960 100644 --- a/src/main/java/org/radarbase/appserver/service/scheduler/quartz/MessageJob.java +++ b/src/main/java/org/radarbase/appserver/service/scheduler/quartz/MessageJob.java @@ -34,12 +34,12 @@ import org.radarbase.appserver.service.FcmNotificationService; import org.radarbase.appserver.service.MessageType; import org.radarbase.appserver.service.transmitter.DataMessageTransmitter; -import org.radarbase.appserver.service.transmitter.EmailNotificationTransmitter; -import org.radarbase.appserver.service.transmitter.NotificationTransmitter; import org.radarbase.appserver.service.transmitter.FcmTransmitter; +import org.radarbase.appserver.service.transmitter.NotificationTransmitter; import java.util.ArrayList; import java.util.List; +import java.util.Optional; /** * A {@link Job} that sends messages to the device or email when executed. @@ -60,10 +60,10 @@ public class MessageJob implements Job { private final transient FcmDataMessageService dataMessageService; public MessageJob( - List notificationTransmitters, - List dataMessageTransmitters, - FcmNotificationService notificationService, - FcmDataMessageService dataMessageService) { + List notificationTransmitters, + List dataMessageTransmitters, + FcmNotificationService notificationService, + FcmDataMessageService dataMessageService) { this.notificationTransmitters = notificationTransmitters; this.dataMessageTransmitters = dataMessageTransmitters; this.notificationService = notificationService; @@ -85,46 +85,54 @@ public MessageJob( @Override @SuppressWarnings("PMD.DataflowAnomalyAnalysis") public void execute(JobExecutionContext context) throws JobExecutionException { - JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); - MessageType type = MessageType.valueOf(jobDataMap.getString("messageType")); - String projectId = jobDataMap.getString("projectId"); - String subjectId = jobDataMap.getString("subjectId"); - Long messageId = jobDataMap.getLong("messageId"); - List exceptions = new ArrayList<>(); + JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); + MessageType type = MessageType.valueOf(jobDataMap.getString("messageType")); + String projectId = jobDataMap.getString("projectId"); + String subjectId = jobDataMap.getString("subjectId"); + Long messageId = jobDataMap.getLong("messageId"); + List exceptions = new ArrayList<>(); + try { switch (type) { case NOTIFICATION: Notification notification = - notificationService.getNotificationByProjectIdAndSubjectIdAndNotificationId( - projectId, subjectId, messageId); + notificationService.getNotificationByProjectIdAndSubjectIdAndNotificationId( + projectId, subjectId, messageId); notificationTransmitters.forEach(t -> { - try { - t.send(notification); - } catch (MessageTransmitException e) { - exceptions.add(e); - } + try { + t.send(notification); + } catch (MessageTransmitException e) { + exceptions.add(e); + } }); break; case DATA: DataMessage dataMessage = - dataMessageService.getDataMessageByProjectIdAndSubjectIdAndDataMessageId( - projectId, subjectId, messageId); + dataMessageService.getDataMessageByProjectIdAndSubjectIdAndDataMessageId( + projectId, subjectId, messageId); dataMessageTransmitters.forEach(t -> { - try { - t.send(dataMessage); - } catch (MessageTransmitException e) { - exceptions.add(e); - } + try { + t.send(dataMessage); + } catch (MessageTransmitException e) { + exceptions.add(e); + } }); break; default: break; } - + } catch (Exception e) { + log.error("Could not transmit a message", e); + throw new JobExecutionException("Could not transmit a message", e); + } finally { // Here handle the exceptions that occurred while transmitting the message via the // transmitters. At present, only the FcmTransmitter affects the job execution state. - if (exceptions.stream().anyMatch(e -> e instanceof FcmMessageTransmitException)) { - throw new JobExecutionException("Could not transmit a message", exceptions.get(0)); + Optional fcmException = exceptions.stream() + .filter(e -> e instanceof FcmMessageTransmitException) + .findFirst(); + if (fcmException.isPresent()) { + throw new JobExecutionException("Could not transmit a message", fcmException.get()); } + } }