diff --git a/spring-kafka/src/main/java/org/springframework/kafka/core/CleanupConfig.java b/spring-kafka/src/main/java/org/springframework/kafka/core/CleanupConfig.java new file mode 100644 index 00000000..930908d9 --- /dev/null +++ b/spring-kafka/src/main/java/org/springframework/kafka/core/CleanupConfig.java @@ -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; + } +} diff --git a/spring-kafka/src/main/java/org/springframework/kafka/core/StreamsBuilderFactoryBean.java b/spring-kafka/src/main/java/org/springframework/kafka/core/StreamsBuilderFactoryBean.java index 10e7548c..f30b3506 100644 --- a/spring-kafka/src/main/java/org/springframework/kafka/core/StreamsBuilderFactoryBean.java +++ b/spring-kafka/src/main/java/org/springframework/kafka/core/StreamsBuilderFactoryBean.java @@ -44,6 +44,7 @@ public class StreamsBuilderFactoryBean extends AbstractFactoryBean streamsConfig) { + this(streamsConfig, new CleanupConfig()); + } + + /** + * Construct an instance with the supplied streams configuration and + * clean up configuration. + * @param streamsConfig the streams configuration. + * @param cleanupConfig the cleanup configuration. + * @since 2.1.2. + */ + public StreamsBuilderFactoryBean(Map streamsConfig, CleanupConfig cleanupConfig) { Assert.notNull(streamsConfig, "'streamsConfig' must not be null"); this.streamsConfig = new StreamsConfig(streamsConfig); + this.cleanupConfig = cleanupConfig; } public void setClientSupplier(KafkaClientSupplier clientSupplier) { @@ -133,6 +166,9 @@ public class StreamsBuilderFactoryBean extends AbstractFactoryBean 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); + } + } + +} diff --git a/src/reference/asciidoc/streams.adoc b/src/reference/asciidoc/streams.adoc index 09cf80e0..b30d3c6c 100644 --- a/src/reference/asciidoc/streams.adoc +++ b/src/reference/asciidoc/streams.adoc @@ -119,6 +119,9 @@ Only you need is to declare `StreamsConfig` bean with the `defaultKafkaStreamsCo A `StreamsBuilder` bean with the `defaultKafkaStreamsBuilder` name will be declare in the application context automatically. Any additional `StreamsBuilderFactoryBean` beans can be declared and used as well. +By default, when the factory bean is stopped, the `KafkaStreams.cleanUp()` method is called. +Starting with _version 2.1.2_, the factory bean has additional constructors, taking a `CleanupConfig` object that has properties to allow you to control whether the `cleanUp()` method is called during `start()`, `stop()`, or neither. + ==== Kafka Streams Example Putting it all together: