Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configurable KafkaStreams cleanup execution
542 few minor fixes Docs, Javadocs
- Loading branch information
1 parent
2526509
commit 63b2bcb
Showing
4 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
spring-kafka/src/main/java/org/springframework/kafka/core/CleanupConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.kafka.core; | ||
|
||
import org.apache.kafka.streams.KafkaStreams; | ||
|
||
/** | ||
* Specifies time of {@link KafkaStreams#cleanUp()} execution. | ||
* | ||
* @author Pawel Szymczyk | ||
*/ | ||
public class CleanupConfig { | ||
|
||
private final boolean onStart; | ||
private final boolean onStop; | ||
|
||
public CleanupConfig() { | ||
this(false, true); | ||
} | ||
|
||
public CleanupConfig(boolean onStart, boolean onStop) { | ||
this.onStart = onStart; | ||
this.onStop = onStop; | ||
} | ||
|
||
public boolean cleanupOnStart() { | ||
return this.onStart; | ||
} | ||
|
||
public boolean cleanupOnStop() { | ||
return this.onStop; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
spring-kafka/src/test/java/org/springframework/kafka/core/StreamsBuilderFactoryBeanTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.kafka.core; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.kafka.streams.StreamsConfig; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.kafka.annotation.EnableKafka; | ||
import org.springframework.kafka.annotation.EnableKafkaStreams; | ||
import org.springframework.kafka.annotation.KafkaStreamsDefaultConfiguration; | ||
import org.springframework.kafka.test.context.EmbeddedKafka; | ||
import org.springframework.kafka.test.rule.KafkaEmbedded; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
/** | ||
* @author Pawel Szymczyk | ||
*/ | ||
@RunWith(SpringRunner.class) | ||
@DirtiesContext | ||
@EmbeddedKafka | ||
public class StreamsBuilderFactoryBeanTest { | ||
|
||
private static final String APPLICATION_ID = "testCleanupStreams"; | ||
|
||
private static Path stateStoreDir; | ||
|
||
@BeforeClass | ||
public static void setup() throws IOException { | ||
stateStoreDir = Files.createTempDirectory("test-state-dir"); | ||
} | ||
|
||
@Autowired | ||
private StreamsBuilderFactoryBean streamsBuilderFactoryBean; | ||
|
||
@Test | ||
public void testCleanupStreams() throws IOException { | ||
Path stateStore = Files.createDirectory(Paths.get(stateStoreDir.toString(), APPLICATION_ID, "0_0")); | ||
assertThat(stateStore).exists(); | ||
streamsBuilderFactoryBean.stop(); | ||
assertThat(stateStore).doesNotExist(); | ||
|
||
stateStore = Files.createDirectory(Paths.get(stateStoreDir.toString(), APPLICATION_ID, "0_0")); | ||
assertThat(stateStore).exists(); | ||
streamsBuilderFactoryBean.start(); | ||
assertThat(stateStore).doesNotExist(); | ||
} | ||
|
||
@Configuration | ||
@EnableKafka | ||
@EnableKafkaStreams | ||
public static class KafkaStreamsConfiguration { | ||
|
||
@Value("${" + KafkaEmbedded.SPRING_EMBEDDED_KAFKA_BROKERS + "}") | ||
private String brokerAddresses; | ||
|
||
@Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_BUILDER_BEAN_NAME) | ||
public StreamsBuilderFactoryBean defaultKafkaStreamsBuilder() throws IOException { | ||
return new StreamsBuilderFactoryBean(kStreamsConfigs(), new CleanupConfig(true, true)); | ||
} | ||
|
||
@Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME) | ||
public StreamsConfig kStreamsConfigs() throws IOException { | ||
Map<String, Object> props = new HashMap<>(); | ||
props.put(StreamsConfig.APPLICATION_ID_CONFIG, APPLICATION_ID); | ||
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, this.brokerAddresses); | ||
props.put(StreamsConfig.STATE_DIR_CONFIG, stateStoreDir.toString()); | ||
return new StreamsConfig(props); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters