Skip to content

Commit 085ad7c

Browse files
Mike Davisaliabbasrizvi
authored andcommitted
Catch errors thrown while sending notifications. (#295)
1 parent 06ab535 commit 085ad7c

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

core-api/src/main/java/com/optimizely/ab/notification/NotificationCenter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ public Class getNotificationTypeClass() {
9090
public NotificationCenter() {
9191
AtomicInteger counter = new AtomicInteger();
9292
Map<Class, NotificationManager> validManagers = new HashMap<>();
93-
validManagers.put(ActivateNotification.class, new NotificationManager<ActivateNotification>(counter));
94-
validManagers.put(TrackNotification.class, new NotificationManager<TrackNotification>(counter));
95-
validManagers.put(DecisionNotification.class, new NotificationManager<DecisionNotification>(counter));
93+
validManagers.put(ActivateNotification.class, new NotificationManager<>(ActivateNotification.class, counter));
94+
validManagers.put(TrackNotification.class, new NotificationManager<>(TrackNotification.class, counter));
95+
validManagers.put(DecisionNotification.class, new NotificationManager<>(DecisionNotification.class, counter));
9696

9797
notifierMap = Collections.unmodifiableMap(validManagers);
9898
}

core-api/src/main/java/com/optimizely/ab/notification/NotificationManager.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public class NotificationManager<T> {
3535

3636
private final Map<Integer, NotificationHandler<T>> handlers = new LinkedHashMap<>();
3737
private final AtomicInteger counter;
38+
private final Class<T> clazz;
3839

39-
NotificationManager(AtomicInteger counter) {
40+
NotificationManager(Class<T> clazz, AtomicInteger counter) {
41+
this.clazz = clazz;
4042
this.counter = counter;
4143
}
4244

@@ -56,9 +58,13 @@ public int addHandler(NotificationHandler<T> newHandler) {
5658
return notificationId;
5759
}
5860

59-
public void send(T message) {
60-
for (NotificationHandler<T> handler: handlers.values()) {
61-
handler.handle(message);
61+
void send(T message) {
62+
for (Map.Entry<Integer, NotificationHandler<T>> handler: handlers.entrySet()) {
63+
try {
64+
handler.getValue().handle(message);
65+
} catch (Exception e) {
66+
logger.warn("Catching exception sending notification for class: {}, handler: {}", clazz, handler.getKey());
67+
}
6268
}
6369
}
6470

core-api/src/test/java/com/optimizely/ab/notification/NotificationManagerTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ public class NotificationManagerTest {
3232
@Before
3333
public void setUp() {
3434
counter = new AtomicInteger();
35-
notificationManager = new NotificationManager<>(counter);
35+
notificationManager = new NotificationManager<>(TestNotification.class, counter);
3636
}
3737

3838
@Test
3939
public void testAddListener() {
40-
assertEquals(1, notificationManager.addHandler(new TestNotificationHandler()));
41-
assertEquals(2, notificationManager.addHandler(new TestNotificationHandler()));
42-
assertEquals(3, notificationManager.addHandler(new TestNotificationHandler()));
40+
assertEquals(1, notificationManager.addHandler(new TestNotificationHandler<>()));
41+
assertEquals(2, notificationManager.addHandler(new TestNotificationHandler<>()));
42+
assertEquals(3, notificationManager.addHandler(new TestNotificationHandler<>()));
4343
}
4444

4545
@Test
@@ -56,6 +56,18 @@ public void testSend() {
5656
assertEquals("message1", messages.get(0).getMessage());
5757
assertEquals("message2", messages.get(1).getMessage());
5858
assertEquals("message3", messages.get(2).getMessage());
59+
}
60+
61+
@Test
62+
public void testSendWithError() {
63+
TestNotificationHandler<TestNotification> handler = new TestNotificationHandler<>();
64+
assertEquals(1, notificationManager.addHandler(message -> {throw new RuntimeException("handle me");}));
65+
assertEquals(2, notificationManager.addHandler(handler));
5966

67+
notificationManager.send(new TestNotification("message1"));
68+
69+
List<TestNotification> messages = handler.getMessages();
70+
assertEquals(1, messages.size());
71+
assertEquals("message1", messages.get(0).getMessage());
6072
}
6173
}

0 commit comments

Comments
 (0)