Skip to content

Commit dd3122a

Browse files
committed
Introduce "switch off all submission" toggle
- auto configuration - test for non-existence of certain beans - documentation
1 parent db124f4 commit dd3122a

3 files changed

Lines changed: 117 additions & 26 deletions

File tree

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,23 @@ If you do not use the STUPS Tokens library, you can implement token retrieval yo
163163
type [`AccessTokenProvider`](nakadi-producer-spring-boot-starter/src/main/java/org/zalando/nakadiproducer/AccessTokenProvider.java).
164164
The starter will detect it and call it once for each request to retrieve the token.
165165

166+
### Disable submission completely
167+
168+
You can disable the whole Nakadi integration completely with this property:
169+
170+
```yaml
171+
nakadi-producer:
172+
submission-enabled: false
173+
```
174+
175+
In this case you don't need to configure anything related to the Nakadi communication ↑ (and this library won't
176+
set up any beans related to it).
177+
178+
A use case for this might be that you have several components of your application connected to the same database,
179+
and want the submission of the events centralized in one of these components. Then for all other components you'd
180+
set `nakadi-producer.submission-enabled: false` (true is the default), but still can use the EventLogWriter to
181+
create events.
182+
166183
### Creating events
167184

168185
The typical use case for this library is to publish events like creating or updating of some objects.
@@ -431,7 +448,8 @@ This is a list of all the documented spring properties (in alphabetical order),
431448
| [`nakadi-producer.lock-duration-buffer`](#customizing-event-locks) | Number of seconds before the expiry of a lock an event is not used. |
432449
| [`nakadi-producer.lock-size`](#customizing-event-locks) | Number of events to lock (and then load into memory) at once. |
433450
| [`nakadi-producer.nakadi-base-uri`](#letting-this-library-set-things-up) | The Nakadi base URI used for submitting events. |
434-
| [`nakadi-producer.scheduled-transmission-enabled: false`](#test-support) | Disable event transmission scheduler. |
451+
| [`nakadi-producer.scheduled-transmission-enabled: false`](#test-support) | Disable event transmission scheduler (but still set up Nakadi connection beans, so it can be used manually). |
452+
| [`nakadi-producer.submission-enabled: false`](#disable-submission-completely) | Disable event submission completely (including all beans for this). |
435453
| [`tracer.traces.X-Flow-ID: flow-id`](#x-flow-id-optional) | Enable flow-ID support |
436454

437455
## Contributing

nakadi-producer-spring-boot-starter/src/main/java/org/zalando/nakadiproducer/NakadiProducerAutoConfiguration.java

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
1515
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
1616
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
17+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
1718
import org.springframework.boot.autoconfigure.flyway.FlywayProperties;
1819
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
1920
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -47,24 +48,27 @@
4748

4849
@Configuration
4950
@AutoConfigureAfter(name="org.zalando.tracer.spring.TracerAutoConfiguration")
50-
@EnableScheduling
5151
@EnableConfigurationProperties({ DataSourceProperties.class, FlywayProperties.class })
5252
@Slf4j
5353
public class NakadiProducerAutoConfiguration {
5454

55+
@ConditionalOnProperty(name="nakadi-producer.submission-enabled", havingValue = "true", matchIfMissing = true)
5556
@ConditionalOnMissingBean({NakadiPublishingClient.class, NakadiClient.class})
5657
@Configuration
5758
@Import(FahrscheinWithTokensNakadiClientConfiguration.StupsTokenConfiguration.class)
5859
static class FahrscheinWithTokensNakadiClientConfiguration {
5960

6061
@Bean
61-
public NakadiPublishingClient nakadiProducerPublishingClient(AccessTokenProvider accessTokenProvider,
62-
@Value("${nakadi-producer.nakadi-base-uri}") URI nakadiBaseUri, RequestFactory requestFactory) {
62+
public NakadiPublishingClient nakadiProducerPublishingClient(
63+
AccessTokenProvider accessTokenProvider,
64+
@Value("${nakadi-producer.nakadi-base-uri}") URI nakadiBaseUri,
65+
RequestFactory requestFactory) {
6366
return new FahrscheinNakadiPublishingClient(NakadiClient.builder(nakadiBaseUri, requestFactory)
6467
.withAccessTokenProvider(accessTokenProvider::getAccessToken).build());
6568
}
6669

6770
@ConditionalOnClass(name = "org.zalando.stups.tokens.Tokens")
71+
@ConditionalOnProperty(name="nakadi-producer.submission-enabled", havingValue = "true", matchIfMissing = true)
6872
@ConditionalOnMissingBean({NakadiPublishingClient.class, NakadiClient.class})
6973
@Configuration
7074
static class StupsTokenConfiguration {
@@ -75,16 +79,16 @@ public StupsTokenComponent accessTokenProvider(
7579
@Value("${nakadi-producer.access-token-scopes:uid}") String[] accessTokenScopes) {
7680
return new StupsTokenComponent(accessTokenUri, Arrays.asList(accessTokenScopes));
7781
}
78-
7982
}
83+
8084
@Bean
8185
@ConditionalOnMissingBean
82-
RequestFactory requestFactory(@Value("${nakadi-producer.encoding:GZIP}") ContentEncoding encoding){
86+
RequestFactory requestFactory(@Value("${nakadi-producer.encoding:GZIP}") ContentEncoding encoding) {
8387
return new SimpleRequestFactory(encoding);
8488
}
85-
8689
}
8790

91+
@ConditionalOnProperty(name="nakadi-producer.submission-enabled", havingValue = "true", matchIfMissing = true)
8892
@ConditionalOnMissingBean(NakadiPublishingClient.class)
8993
@ConditionalOnBean(NakadiClient.class)
9094
@Configuration
@@ -150,27 +154,34 @@ public EventLogRepository eventLogRepository(NamedParameterJdbcTemplate namedPar
150154
return new EventLogRepositoryImpl(namedParameterJdbcTemplate, lockSize);
151155
}
152156

153-
@Bean
154-
public EventTransmitter eventTransmitter(EventTransmissionService eventTransmissionService) {
155-
return new EventTransmitter(eventTransmissionService);
156-
}
157+
@ConditionalOnProperty(name="nakadi-producer.submission-enabled", havingValue = "true", matchIfMissing = true)
158+
@EnableScheduling
159+
@Configuration
160+
static class TransmissionConfiguration {
157161

158-
@Bean
159-
public EventTransmissionScheduler eventTransmissionScheduler(EventTransmitter eventTransmitter,
160-
@Value("${nakadi-producer.scheduled-transmission-enabled:true}") boolean scheduledTransmissionEnabled) {
161-
return new EventTransmissionScheduler(eventTransmitter, scheduledTransmissionEnabled);
162-
}
162+
@Bean
163+
public EventTransmitter eventTransmitter(EventTransmissionService eventTransmissionService) {
164+
return new EventTransmitter(eventTransmissionService);
165+
}
166+
167+
@Bean
168+
public EventTransmissionScheduler eventTransmissionScheduler(
169+
EventTransmitter eventTransmitter,
170+
@Value("${nakadi-producer.scheduled-transmission-enabled:true}") boolean scheduledTransmissionEnabled) {
171+
return new EventTransmissionScheduler(eventTransmitter, scheduledTransmissionEnabled);
172+
}
163173

164-
@Bean
165-
public EventTransmissionService eventTransmissionService(
166-
EventLogRepository eventLogRepository,
167-
NakadiPublishingClient nakadiPublishingClient,
168-
ObjectMapper objectMapper,
169-
@Value("${nakadi-producer.lock-duration:600}") int lockDuration,
170-
@Value("${nakadi-producer.lock-duration-buffer:60}") int lockDurationBuffer) {
171-
return new EventTransmissionService(
172-
eventLogRepository, nakadiPublishingClient, objectMapper, lockDuration, lockDurationBuffer);
173-
}
174+
@Bean
175+
public EventTransmissionService eventTransmissionService(
176+
EventLogRepository eventLogRepository,
177+
NakadiPublishingClient nakadiPublishingClient,
178+
ObjectMapper objectMapper,
179+
@Value("${nakadi-producer.lock-duration:600}") int lockDuration,
180+
@Value("${nakadi-producer.lock-duration-buffer:60}") int lockDurationBuffer) {
181+
return new EventTransmissionService(
182+
eventLogRepository, nakadiPublishingClient, objectMapper, lockDuration, lockDurationBuffer);
183+
}
184+
}
174185

175186
@Bean
176187
public FlywayMigrator flywayMigrator() {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.zalando.nakadiproducer;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.context.ApplicationContext;
7+
import org.springframework.test.context.ActiveProfiles;
8+
import org.zalando.fahrschein.NakadiClient;
9+
import org.zalando.fahrschein.http.api.RequestFactory;
10+
import org.zalando.nakadiproducer.config.EmbeddedDataSourceConfig;
11+
import org.zalando.nakadiproducer.eventlog.EventLogWriter;
12+
import org.zalando.nakadiproducer.eventlog.impl.EventLogRepository;
13+
import org.zalando.nakadiproducer.transmission.NakadiPublishingClient;
14+
import org.zalando.nakadiproducer.transmission.impl.EventTransmissionService;
15+
import org.zalando.nakadiproducer.transmission.impl.EventTransmitter;
16+
17+
import static org.hamcrest.CoreMatchers.notNullValue;
18+
import static org.hamcrest.CoreMatchers.nullValue;
19+
import static org.hamcrest.MatcherAssert.assertThat;
20+
21+
// no "test" profile, as this would include the mock client.
22+
@SpringBootTest(
23+
webEnvironment = SpringBootTest.WebEnvironment.MOCK,
24+
properties = {"nakadi-producer.submission-enabled:false", "debug:true"},
25+
classes = { TestApplication.class, EmbeddedDataSourceConfig.class }
26+
)
27+
public class SubmissionDisabledIT {
28+
29+
@Autowired
30+
ApplicationContext context;
31+
32+
@Test
33+
public void noNakadiBeans() {
34+
assertThat(context.getBeanProvider(NakadiClient.class).getIfAvailable(), nullValue());
35+
assertThat(context.getBeanProvider(NakadiPublishingClient.class).getIfAvailable(), nullValue());
36+
assertThat(context.getBeanProvider(StupsTokenComponent.class).getIfAvailable(), nullValue());
37+
assertThat(context.getBeanProvider(RequestFactory.class).getIfAvailable(), nullValue());
38+
}
39+
40+
@Test
41+
public void noTransmissionBeans() {
42+
assertThat(context.getBeanProvider(EventTransmitter.class).getIfAvailable(), nullValue());
43+
assertThat(context.getBeanProvider(EventTransmissionService.class).getIfAvailable(), nullValue());
44+
assertThat(context.getBeanProvider(EventTransmissionScheduler.class).getIfAvailable(), nullValue());
45+
}
46+
47+
@Test
48+
public void yesEventLogWriter() {
49+
assertThat(context.getBeanProvider(EventLogWriter.class).getIfAvailable(), notNullValue());
50+
}
51+
52+
@Test
53+
public void yesRepository() {
54+
assertThat(context.getBeanProvider(EventLogRepository.class).getIfAvailable(), notNullValue());
55+
}
56+
57+
@Test
58+
public void yesFlywayMigrator() {
59+
assertThat(context.getBeanProvider(FlywayMigrator.class).getIfAvailable(), notNullValue());
60+
}
61+
62+
}

0 commit comments

Comments
 (0)