Commit 73d1d217 by Sebastian Meiser

added CompositeNotifier to allow for multiple notifiers. Only used when multiple…

added CompositeNotifier to allow for multiple notifiers. Only used when multiple notifiers are configured and no primary notifier is already configured.
parent 9c142aa2
......@@ -15,18 +15,25 @@
*/
package de.codecentric.boot.admin.config;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.condition.NoneNestedConditions;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.mail.MailSender;
import de.codecentric.boot.admin.notify.CompositeNotifier;
import de.codecentric.boot.admin.notify.HipchatNotifier;
import de.codecentric.boot.admin.notify.MailNotifier;
import de.codecentric.boot.admin.notify.Notifier;
......@@ -50,6 +57,32 @@ public class NotifierConfiguration {
}
@Configuration
@AutoConfigureBefore({ NotifierListenerConfiguration.class })
@AutoConfigureAfter({ MailNotifierConfiguration.class, PagerdutyNotifierConfiguration.class, HipchatNotifierConfiguration.class })
public static class CompositeNotifierConfiguration {
@Bean
@Primary
@Conditional(OnNoPrimaryNotifierCondition.class)
@ConditionalOnBean(Notifier.class)
public CompositeNotifier compositeNotifier(List<Notifier> notifiers) {
return new CompositeNotifier(notifiers);
}
static class OnNoPrimaryNotifierCondition extends NoneNestedConditions {
OnNoPrimaryNotifierCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnSingleCandidate(Notifier.class)
static class HasSingleNotifierInstance {
}
}
}
@Configuration
@ConditionalOnBean(MailSender.class)
@AutoConfigureAfter({ MailSenderAutoConfiguration.class })
@AutoConfigureBefore({ NotifierListenerConfiguration.class })
......
package de.codecentric.boot.admin.notify;
import de.codecentric.boot.admin.event.ClientApplicationEvent;
/**
* A notifier delegating notifications to all specified notifiers.
*
* @author sebastian.meiser
*/
public class CompositeNotifier implements Notifier {
private final Iterable<Notifier> notifiers;
public CompositeNotifier(Iterable<Notifier> notifiers) {
this.notifiers = notifiers;
}
@Override
public void notify(ClientApplicationEvent event) {
for (Notifier notifier : notifiers) {
notifier.notify(event);
}
}
}
......@@ -35,6 +35,7 @@ import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
......@@ -42,10 +43,13 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon
import com.hazelcast.config.Config;
import de.codecentric.boot.admin.discovery.ApplicationDiscoveryListener;
import de.codecentric.boot.admin.event.ClientApplicationEvent;
import de.codecentric.boot.admin.journal.store.HazelcastJournaledEventStore;
import de.codecentric.boot.admin.journal.store.JournaledEventStore;
import de.codecentric.boot.admin.journal.store.SimpleJournaledEventStore;
import de.codecentric.boot.admin.notify.CompositeNotifier;
import de.codecentric.boot.admin.notify.MailNotifier;
import de.codecentric.boot.admin.notify.Notifier;
import de.codecentric.boot.admin.notify.PagerdutyNotifier;
import de.codecentric.boot.admin.registry.store.ApplicationStore;
import de.codecentric.boot.admin.registry.store.HazelcastApplicationStore;
......@@ -112,6 +116,35 @@ public class AdminServerWebConfigurationTest {
}
@Test
public void simpleConfig_multipleNotifiers() {
load(TestAdditionalNotifierConfig.class, "spring.boot.admin.notify.pagerduty.service-key:foo");
assertThat(context.getBean(Notifier.class),
is(instanceOf(CompositeNotifier.class)));
}
@Test
public void simpleConfig_multipleNotifiersWithPrimary() {
load(TestAdditionalPrimaryNotifierConfig.class, "spring.boot.admin.notify.pagerduty.service-key:foo");
assertThat(context.getBean(Notifier.class),
is(instanceOf(TestNotifier.class)));
}
static class TestAdditionalNotifierConfig {
@Bean
public Notifier testNotifier() {
return new TestNotifier();
}
}
static class TestAdditionalPrimaryNotifierConfig {
@Bean
@Primary
public Notifier testNotifier() {
return new TestNotifier();
}
}
@Test
public void hazelcastConfig() {
load(TestHazelcastConfig.class);
assertThat(context.getBean(ApplicationStore.class),
......@@ -159,4 +192,10 @@ public class AdminServerWebConfigurationTest {
applicationContext.refresh();
this.context = applicationContext;
}
private static class TestNotifier implements Notifier {
@Override
public void notify(ClientApplicationEvent event) {}
}
}
package de.codecentric.boot.admin.notify;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import de.codecentric.boot.admin.event.ClientApplicationEvent;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
public class CompositeNotifierTest {
private static final ClientApplicationEvent APP_DOWN = new ClientApplicationStatusChangedEvent(
Application.create("App").withId("id-1").withHealthUrl("http://health")
.withStatusInfo(StatusInfo.ofDown()).build(),
StatusInfo.ofUp(), StatusInfo.ofDown());
@Test
public void test_all_notifiers_get_notified() throws Exception {
TestNotifier notifier1 = new TestNotifier();
TestNotifier notifier2 = new TestNotifier();
CompositeNotifier compositeNotifier = new CompositeNotifier(Arrays.<Notifier>asList(notifier1,notifier2));
compositeNotifier.notify(APP_DOWN);
assertThat(notifier1.getEvents(), is(Arrays.asList(APP_DOWN)));
assertThat(notifier2.getEvents(), is(Arrays.asList(APP_DOWN)));
}
private static class TestNotifier implements Notifier {
private List<ClientApplicationEvent> events = new ArrayList<ClientApplicationEvent>();
@Override
public void notify(ClientApplicationEvent event) {
this.events.add(event);
}
public List<ClientApplicationEvent> getEvents() {
return events;
}
}
}
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