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