Skip to content

Commit 04e84dd

Browse files
committed
Make all Notifier extend AbstractEventNotifier
Since the Notifiers shouldn't throw exceptions, we let all extend the AbstractEventNotifier which gets us a enabled property and catching exceptions which may occur when notifiying.
1 parent c410be8 commit 04e84dd

File tree

7 files changed

+47
-15
lines changed

7 files changed

+47
-15
lines changed

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/notify/AbstractEventNotifier.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public void notify(ClientApplicationEvent event) {
4444
}
4545

4646

47-
protected abstract boolean shouldNotify(ClientApplicationEvent event);
47+
protected boolean shouldNotify(ClientApplicationEvent event) {
48+
return true;
49+
}
4850

4951
protected abstract void doNotify(ClientApplicationEvent event) throws Exception;
5052

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/notify/CompositeNotifier.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,26 @@
1515
*/
1616
package de.codecentric.boot.admin.notify;
1717

18+
import org.springframework.util.Assert;
19+
1820
import de.codecentric.boot.admin.event.ClientApplicationEvent;
1921

2022
/**
2123
* A notifier delegating notifications to all specified notifiers.
2224
*
2325
* @author Sebastian Meiser
2426
*/
25-
public class CompositeNotifier implements Notifier {
26-
private final Iterable<Notifier> notifiers;
27+
public class CompositeNotifier extends AbstractEventNotifier {
28+
private final Iterable<Notifier> delegates;
2729

28-
public CompositeNotifier(Iterable<Notifier> notifiers) {
29-
this.notifiers = notifiers;
30+
public CompositeNotifier(Iterable<Notifier> delegates) {
31+
Assert.notNull(delegates, "'delegates' must not be null!");
32+
this.delegates = delegates;
3033
}
3134

3235
@Override
33-
public void notify(ClientApplicationEvent event) {
34-
for (Notifier notifier : notifiers) {
36+
public void doNotify(ClientApplicationEvent event) {
37+
for (Notifier notifier : delegates) {
3538
notifier.notify(event);
3639
}
3740
}

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/notify/RemindingNotifier.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.concurrent.ConcurrentHashMap;
2121
import java.util.concurrent.TimeUnit;
2222

23+
import org.springframework.util.Assert;
24+
2325
import de.codecentric.boot.admin.event.ClientApplicationDeregisteredEvent;
2426
import de.codecentric.boot.admin.event.ClientApplicationEvent;
2527
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
@@ -29,18 +31,19 @@
2931
*
3032
* @author Johannes Edmeier
3133
*/
32-
public class RemindingNotifier implements Notifier {
34+
public class RemindingNotifier extends AbstractEventNotifier {
3335
private final ConcurrentHashMap<String, Reminder> reminders = new ConcurrentHashMap<>();
3436
private long reminderPeriod = TimeUnit.MINUTES.toMillis(10L);
3537
private String[] reminderStatuses = { "DOWN", "OFFLINE" };
3638
private final Notifier delegate;
3739

3840
public RemindingNotifier(Notifier delegate) {
41+
Assert.notNull(delegate, "'delegate' must not be null!");
3942
this.delegate = delegate;
4043
}
4144

4245
@Override
43-
public void notify(ClientApplicationEvent event) {
46+
public void doNotify(ClientApplicationEvent event) {
4447
delegate.notify(event);
4548
if (shouldEndReminder(event)) {
4649
reminders.remove(event.getApplication().getId());

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/notify/filter/FilteringNotifier.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@
2525

2626
import org.slf4j.Logger;
2727
import org.slf4j.LoggerFactory;
28+
import org.springframework.util.Assert;
2829

2930
import de.codecentric.boot.admin.event.ClientApplicationEvent;
31+
import de.codecentric.boot.admin.notify.AbstractEventNotifier;
3032
import de.codecentric.boot.admin.notify.Notifier;
3133

3234
/**
3335
* Notifier that allows to filter certain events based on policies.
3436
*
3537
* @author Johannes Edmeier
3638
*/
37-
public class FilteringNotifier implements Notifier {
39+
public class FilteringNotifier extends AbstractEventNotifier {
3840
private static final Logger LOGGER = LoggerFactory.getLogger(FilteringNotifier.class);
3941
private final ConcurrentMap<String, NotificationFilter> filters = new ConcurrentHashMap<>();
4042
private final Notifier delegate;
@@ -43,11 +45,17 @@ public class FilteringNotifier implements Notifier {
4345
private AtomicLong counter = new AtomicLong();
4446

4547
public FilteringNotifier(Notifier delegate) {
48+
Assert.notNull(delegate, "'delegate' must not be null!");
4649
this.delegate = delegate;
4750
}
4851

4952
@Override
50-
public void notify(ClientApplicationEvent event) {
53+
protected boolean shouldNotify(ClientApplicationEvent event) {
54+
return !filter(event);
55+
}
56+
57+
@Override
58+
public void doNotify(ClientApplicationEvent event) {
5159
if (!filter(event)) {
5260
delegate.notify(event);
5361
}

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/notify/CompositeNotifierTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.Assert.assertThat;
55

66
import java.util.Arrays;
7+
import java.util.Collections;
78

89
import org.junit.Test;
910

@@ -19,6 +20,10 @@ public class CompositeNotifierTest {
1920
.withStatusInfo(StatusInfo.ofDown()).build(),
2021
StatusInfo.ofUp(), StatusInfo.ofDown());
2122

23+
@Test(expected = IllegalArgumentException.class)
24+
public void test_ctor_assert() {
25+
new CompositeNotifier(null);
26+
}
2227

2328
@Test
2429
public void test_all_notifiers_get_notified() throws Exception {
@@ -28,7 +33,7 @@ public void test_all_notifiers_get_notified() throws Exception {
2833

2934
compositeNotifier.notify(APP_DOWN);
3035

31-
assertThat(notifier1.getEvents(), is(Arrays.asList(APP_DOWN)));
32-
assertThat(notifier2.getEvents(), is(Arrays.asList(APP_DOWN)));
36+
assertThat(notifier1.getEvents(), is(Collections.singletonList(APP_DOWN)));
37+
assertThat(notifier2.getEvents(), is(Collections.singletonList(APP_DOWN)));
3338
}
3439
}

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/notify/RemindingNotifierTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.Assert.assertThat;
55

66
import java.util.Arrays;
7+
import java.util.Collections;
78

89
import org.junit.Test;
910

@@ -29,6 +30,11 @@ public class RemindingNotifierTest {
2930
.withStatusInfo(StatusInfo.ofUp()).build(),
3031
StatusInfo.ofDown(), StatusInfo.ofUp());
3132

33+
@Test(expected = IllegalArgumentException.class)
34+
public void test_ctor_assert() {
35+
new CompositeNotifier(null);
36+
}
37+
3238
@Test
3339
public void test_remind() throws Exception {
3440
TestNotifier notifier = new TestNotifier();
@@ -67,7 +73,7 @@ public void test_no_remind_before_end() throws Exception {
6773
reminder.notify(APP_DOWN);
6874
reminder.sendReminders();
6975

70-
assertThat(notifier.getEvents(), is(Arrays.asList(APP_DOWN)));
76+
assertThat(notifier.getEvents(), is(Collections.singletonList(APP_DOWN)));
7177
}
7278

7379
}

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/notify/filter/FilteringNotifierTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package de.codecentric.boot.admin.notify.filter;
22

3+
import static org.junit.Assert.assertThat;
34
import static org.hamcrest.Matchers.hasItem;
45
import static org.hamcrest.Matchers.hasKey;
56
import static org.hamcrest.Matchers.not;
6-
import static org.junit.Assert.assertThat;
77

88
import org.junit.Test;
99

@@ -17,6 +17,11 @@ public class FilteringNotifierTest {
1717
private static final ClientApplicationRegisteredEvent EVENT = new ClientApplicationRegisteredEvent(
1818
Application.create("foo").withHealthUrl("http://health").build());
1919

20+
@Test(expected = IllegalArgumentException.class)
21+
public void test_ctor_assert() {
22+
new FilteringNotifier(null);
23+
}
24+
2025
@Test
2126
public void test_expired_removal() {
2227
FilteringNotifier notifier = new FilteringNotifier(new TestNotifier());

0 commit comments

Comments
 (0)