Commit d8d478fd by Johannes Edmeier

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.
parent 2e8683ee
...@@ -44,7 +44,9 @@ public abstract class AbstractEventNotifier implements Notifier { ...@@ -44,7 +44,9 @@ public abstract class AbstractEventNotifier implements Notifier {
} }
protected abstract boolean shouldNotify(ClientApplicationEvent event); protected boolean shouldNotify(ClientApplicationEvent event) {
return true;
}
protected abstract void doNotify(ClientApplicationEvent event) throws Exception; protected abstract void doNotify(ClientApplicationEvent event) throws Exception;
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
*/ */
package de.codecentric.boot.admin.notify; package de.codecentric.boot.admin.notify;
import org.springframework.util.Assert;
import de.codecentric.boot.admin.event.ClientApplicationEvent; import de.codecentric.boot.admin.event.ClientApplicationEvent;
/** /**
...@@ -22,16 +24,17 @@ import de.codecentric.boot.admin.event.ClientApplicationEvent; ...@@ -22,16 +24,17 @@ import de.codecentric.boot.admin.event.ClientApplicationEvent;
* *
* @author Sebastian Meiser * @author Sebastian Meiser
*/ */
public class CompositeNotifier implements Notifier { public class CompositeNotifier extends AbstractEventNotifier {
private final Iterable<Notifier> notifiers; private final Iterable<Notifier> delegates;
public CompositeNotifier(Iterable<Notifier> notifiers) { public CompositeNotifier(Iterable<Notifier> delegates) {
this.notifiers = notifiers; Assert.notNull(delegates, "'delegates' must not be null!");
this.delegates = delegates;
} }
@Override @Override
public void notify(ClientApplicationEvent event) { public void doNotify(ClientApplicationEvent event) {
for (Notifier notifier : notifiers) { for (Notifier notifier : delegates) {
notifier.notify(event); notifier.notify(event);
} }
} }
......
...@@ -20,6 +20,8 @@ import java.util.Arrays; ...@@ -20,6 +20,8 @@ import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.springframework.util.Assert;
import de.codecentric.boot.admin.event.ClientApplicationDeregisteredEvent; import de.codecentric.boot.admin.event.ClientApplicationDeregisteredEvent;
import de.codecentric.boot.admin.event.ClientApplicationEvent; import de.codecentric.boot.admin.event.ClientApplicationEvent;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent; import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
...@@ -29,18 +31,19 @@ import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent; ...@@ -29,18 +31,19 @@ import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
* *
* @author Johannes Edmeier * @author Johannes Edmeier
*/ */
public class RemindingNotifier implements Notifier { public class RemindingNotifier extends AbstractEventNotifier {
private final ConcurrentHashMap<String, Reminder> reminders = new ConcurrentHashMap<>(); private final ConcurrentHashMap<String, Reminder> reminders = new ConcurrentHashMap<>();
private long reminderPeriod = TimeUnit.MINUTES.toMillis(10L); private long reminderPeriod = TimeUnit.MINUTES.toMillis(10L);
private String[] reminderStatuses = { "DOWN", "OFFLINE" }; private String[] reminderStatuses = { "DOWN", "OFFLINE" };
private final Notifier delegate; private final Notifier delegate;
public RemindingNotifier(Notifier delegate) { public RemindingNotifier(Notifier delegate) {
Assert.notNull(delegate, "'delegate' must not be null!");
this.delegate = delegate; this.delegate = delegate;
} }
@Override @Override
public void notify(ClientApplicationEvent event) { public void doNotify(ClientApplicationEvent event) {
delegate.notify(event); delegate.notify(event);
if (shouldEndReminder(event)) { if (shouldEndReminder(event)) {
reminders.remove(event.getApplication().getId()); reminders.remove(event.getApplication().getId());
......
...@@ -25,8 +25,10 @@ import java.util.concurrent.atomic.AtomicLong; ...@@ -25,8 +25,10 @@ import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import de.codecentric.boot.admin.event.ClientApplicationEvent; import de.codecentric.boot.admin.event.ClientApplicationEvent;
import de.codecentric.boot.admin.notify.AbstractEventNotifier;
import de.codecentric.boot.admin.notify.Notifier; import de.codecentric.boot.admin.notify.Notifier;
/** /**
...@@ -34,7 +36,7 @@ import de.codecentric.boot.admin.notify.Notifier; ...@@ -34,7 +36,7 @@ import de.codecentric.boot.admin.notify.Notifier;
* *
* @author Johannes Edmeier * @author Johannes Edmeier
*/ */
public class FilteringNotifier implements Notifier { public class FilteringNotifier extends AbstractEventNotifier {
private static final Logger LOGGER = LoggerFactory.getLogger(FilteringNotifier.class); private static final Logger LOGGER = LoggerFactory.getLogger(FilteringNotifier.class);
private final ConcurrentMap<String, NotificationFilter> filters = new ConcurrentHashMap<>(); private final ConcurrentMap<String, NotificationFilter> filters = new ConcurrentHashMap<>();
private final Notifier delegate; private final Notifier delegate;
...@@ -43,11 +45,17 @@ public class FilteringNotifier implements Notifier { ...@@ -43,11 +45,17 @@ public class FilteringNotifier implements Notifier {
private AtomicLong counter = new AtomicLong(); private AtomicLong counter = new AtomicLong();
public FilteringNotifier(Notifier delegate) { public FilteringNotifier(Notifier delegate) {
Assert.notNull(delegate, "'delegate' must not be null!");
this.delegate = delegate; this.delegate = delegate;
} }
@Override @Override
public void notify(ClientApplicationEvent event) { protected boolean shouldNotify(ClientApplicationEvent event) {
return !filter(event);
}
@Override
public void doNotify(ClientApplicationEvent event) {
if (!filter(event)) { if (!filter(event)) {
delegate.notify(event); delegate.notify(event);
} }
......
...@@ -4,6 +4,7 @@ import static org.hamcrest.CoreMatchers.is; ...@@ -4,6 +4,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import org.junit.Test; import org.junit.Test;
...@@ -19,6 +20,10 @@ public class CompositeNotifierTest { ...@@ -19,6 +20,10 @@ public class CompositeNotifierTest {
.withStatusInfo(StatusInfo.ofDown()).build(), .withStatusInfo(StatusInfo.ofDown()).build(),
StatusInfo.ofUp(), StatusInfo.ofDown()); StatusInfo.ofUp(), StatusInfo.ofDown());
@Test(expected = IllegalArgumentException.class)
public void test_ctor_assert() {
new CompositeNotifier(null);
}
@Test @Test
public void test_all_notifiers_get_notified() throws Exception { public void test_all_notifiers_get_notified() throws Exception {
...@@ -28,7 +33,7 @@ public class CompositeNotifierTest { ...@@ -28,7 +33,7 @@ public class CompositeNotifierTest {
compositeNotifier.notify(APP_DOWN); compositeNotifier.notify(APP_DOWN);
assertThat(notifier1.getEvents(), is(Arrays.asList(APP_DOWN))); assertThat(notifier1.getEvents(), is(Collections.singletonList(APP_DOWN)));
assertThat(notifier2.getEvents(), is(Arrays.asList(APP_DOWN))); assertThat(notifier2.getEvents(), is(Collections.singletonList(APP_DOWN)));
} }
} }
...@@ -4,6 +4,7 @@ import static org.hamcrest.CoreMatchers.is; ...@@ -4,6 +4,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import org.junit.Test; import org.junit.Test;
...@@ -29,6 +30,11 @@ public class RemindingNotifierTest { ...@@ -29,6 +30,11 @@ public class RemindingNotifierTest {
.withStatusInfo(StatusInfo.ofUp()).build(), .withStatusInfo(StatusInfo.ofUp()).build(),
StatusInfo.ofDown(), StatusInfo.ofUp()); StatusInfo.ofDown(), StatusInfo.ofUp());
@Test(expected = IllegalArgumentException.class)
public void test_ctor_assert() {
new CompositeNotifier(null);
}
@Test @Test
public void test_remind() throws Exception { public void test_remind() throws Exception {
TestNotifier notifier = new TestNotifier(); TestNotifier notifier = new TestNotifier();
...@@ -67,7 +73,7 @@ public class RemindingNotifierTest { ...@@ -67,7 +73,7 @@ public class RemindingNotifierTest {
reminder.notify(APP_DOWN); reminder.notify(APP_DOWN);
reminder.sendReminders(); reminder.sendReminders();
assertThat(notifier.getEvents(), is(Arrays.asList(APP_DOWN))); assertThat(notifier.getEvents(), is(Collections.singletonList(APP_DOWN)));
} }
} }
package de.codecentric.boot.admin.notify.filter; package de.codecentric.boot.admin.notify.filter;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import org.junit.Test; import org.junit.Test;
...@@ -17,6 +17,11 @@ public class FilteringNotifierTest { ...@@ -17,6 +17,11 @@ public class FilteringNotifierTest {
private static final ClientApplicationRegisteredEvent EVENT = new ClientApplicationRegisteredEvent( private static final ClientApplicationRegisteredEvent EVENT = new ClientApplicationRegisteredEvent(
Application.create("foo").withHealthUrl("http://health").build()); Application.create("foo").withHealthUrl("http://health").build());
@Test(expected = IllegalArgumentException.class)
public void test_ctor_assert() {
new FilteringNotifier(null);
}
@Test @Test
public void test_expired_removal() { public void test_expired_removal() {
FilteringNotifier notifier = new FilteringNotifier(new TestNotifier()); FilteringNotifier notifier = new FilteringNotifier(new TestNotifier());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment