Skip to content

Commit

Permalink
Update exception handling by MessageJob
Browse files Browse the repository at this point in the history
  • Loading branch information
pvannierop committed Jul 8, 2024
1 parent 7f74629 commit 1712147
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -60,10 +60,10 @@ public class MessageJob implements Job {
private final transient FcmDataMessageService dataMessageService;

public MessageJob(
List<NotificationTransmitter> notificationTransmitters,
List<DataMessageTransmitter> dataMessageTransmitters,
FcmNotificationService notificationService,
FcmDataMessageService dataMessageService) {
List<NotificationTransmitter> notificationTransmitters,
List<DataMessageTransmitter> dataMessageTransmitters,
FcmNotificationService notificationService,
FcmDataMessageService dataMessageService) {
this.notificationTransmitters = notificationTransmitters;
this.dataMessageTransmitters = dataMessageTransmitters;
this.notificationService = notificationService;
Expand All @@ -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<Exception> 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<Exception> 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<Exception> fcmException = exceptions.stream()
.filter(e -> e instanceof FcmMessageTransmitException)
.findFirst();
if (fcmException.isPresent()) {
throw new JobExecutionException("Could not transmit a message", fcmException.get());
}
}

}

Expand Down

0 comments on commit 1712147

Please sign in to comment.