Skip to content

Commit 8b8bdf2

Browse files
committed
Support for disabling notifications by a Spring Boot property.
1 parent 7305f2f commit 8b8bdf2

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,11 @@ public class MyConfig {
133133
}
134134
```
135135
Then you can inject a Spring bean implementing `io.github.k_tomaszewski.notifications.NotificationService` interface, which is the one
136-
you use for sending notifications.
136+
you use for sending notifications.
137+
138+
### Disabling notifications
139+
In certain cases, for example tests, it is handy to have notifications disabled. To disable notifications
140+
you need to set following Spring Boot property:
141+
```yaml
142+
notifications.enabled: false
143+
```

src/main/java/io/github/k_tomaszewski/notifications/NotificationsConfig.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ public NotificationsProperties notificationsProperties() {
2828

2929
@Bean
3030
public NotificationService notificationService(NotificationsProperties config, AutowireCapableBeanFactory beanFactory) {
31-
return new NotificationServiceBean(config, createSenders(config), beanFactory);
31+
if (config.isEnabled()) {
32+
return new NotificationServiceBean(config, createSenders(config), beanFactory);
33+
} else {
34+
LOG.info("Notifications are disabled by configuration.");
35+
return (level, title, content) -> {};
36+
}
3237
}
3338

3439
private static List<? extends Sender> createSenders(NotificationsProperties config) {

src/main/java/io/github/k_tomaszewski/notifications/NotificationsProperties.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class NotificationsProperties {
1919

2020
private static final String DEFAULT_ENTRY = "default";
2121

22+
private boolean enabled = true;
2223
private String emailTo;
2324
private int queueCapacity = 1000;
2425
private String sendingStrategyClassName = BasicSendingStrategy.class.getName();
@@ -82,6 +83,14 @@ public void setEmailTo(String emailTo) {
8283
this.emailTo = emailTo;
8384
}
8485

86+
public boolean isEnabled() {
87+
return enabled;
88+
}
89+
90+
public void setEnabled(boolean enabled) {
91+
this.enabled = enabled;
92+
}
93+
8594
@SuppressWarnings("unchecked")
8695
private static <T> T createInstance(String className, String errorMessageTemplate) {
8796
try {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.k_tomaszewski.notifications;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.slf4j.event.Level;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.ActiveProfiles;
10+
11+
@SpringBootTest(classes = NotificationsConfig.class, properties = "notifications.enabled=false")
12+
@EnableAutoConfiguration
13+
@ActiveProfiles("test")
14+
public class DisabledNotificationsTet {
15+
16+
@Autowired
17+
private NotificationService notificationService;
18+
19+
@Test
20+
void shouldProvideNotificationServiceWhenNotificationsDisabled() {
21+
Assertions.assertNotNull(notificationService);
22+
notificationService.send(Level.INFO, "title", "content"); // and no error
23+
}
24+
}

0 commit comments

Comments
 (0)