|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.streams; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import org.apache.kafka.common.serialization.Serdes; |
| 27 | +import org.apache.kafka.streams.StreamsBuilder; |
| 28 | +import org.apache.kafka.streams.StreamsConfig; |
| 29 | +import org.apache.kafka.streams.kstream.KStream; |
| 30 | +import org.apache.kafka.streams.processor.WallclockTimestampExtractor; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +import org.springframework.beans.factory.annotation.Autowired; |
| 34 | +import org.springframework.beans.factory.annotation.Value; |
| 35 | +import org.springframework.context.annotation.Bean; |
| 36 | +import org.springframework.context.annotation.Configuration; |
| 37 | +import org.springframework.kafka.annotation.EnableKafkaStreams; |
| 38 | +import org.springframework.kafka.annotation.KafkaStreamsDefaultConfiguration; |
| 39 | +import org.springframework.kafka.config.KafkaStreamsConfiguration; |
| 40 | +import org.springframework.kafka.config.StreamsBuilderFactoryBean; |
| 41 | +import org.springframework.kafka.config.StreamsBuilderFactoryBeanConfigurer; |
| 42 | +import org.springframework.kafka.config.StreamsBuilderFactoryBeanCustomizer; |
| 43 | +import org.springframework.kafka.test.EmbeddedKafkaBroker; |
| 44 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 45 | +import org.springframework.test.annotation.DirtiesContext; |
| 46 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 47 | + |
| 48 | +/** |
| 49 | + * @author Gary Russell |
| 50 | + * @since 2.7 |
| 51 | + * |
| 52 | + */ |
| 53 | +@SpringJUnitConfig |
| 54 | +@DirtiesContext |
| 55 | +@EmbeddedKafka(partitions = 1, |
| 56 | + topics = Configurer1Tests.STREAMING_TOPIC1, |
| 57 | + brokerProperties = { |
| 58 | + "auto.create.topics.enable=${topics.autoCreate:false}", |
| 59 | + "delete.topic.enable=${topic.delete:true}" }, |
| 60 | + brokerPropertiesLocation = "classpath:/${broker.filename:broker}.properties") |
| 61 | +public class Configurer1Tests { |
| 62 | + |
| 63 | + public static final String STREAMING_TOPIC1 = "Configurer1Tests1"; |
| 64 | + |
| 65 | + @Test |
| 66 | + void appliedInOrder(@Autowired List<Integer> callOrder) { |
| 67 | + assertThat(callOrder).containsExactly(1, 2, 3); |
| 68 | + } |
| 69 | + |
| 70 | + @Configuration |
| 71 | + @EnableKafkaStreams |
| 72 | + public static class Config { |
| 73 | + |
| 74 | + @Value("${" + EmbeddedKafkaBroker.SPRING_EMBEDDED_KAFKA_BROKERS + "}") |
| 75 | + private String brokerAddresses; |
| 76 | + |
| 77 | + @Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME) |
| 78 | + public KafkaStreamsConfiguration kStreamsConfigs() { |
| 79 | + Map<String, Object> props = new HashMap<>(); |
| 80 | + props.put(StreamsConfig.APPLICATION_ID_CONFIG, "configurer1"); |
| 81 | + props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, this.brokerAddresses); |
| 82 | + props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.Integer().getClass().getName()); |
| 83 | + props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName()); |
| 84 | + props.put(StreamsConfig.DEFAULT_TIMESTAMP_EXTRACTOR_CLASS_CONFIG, |
| 85 | + WallclockTimestampExtractor.class.getName()); |
| 86 | + props.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, "100"); |
| 87 | + return new KafkaStreamsConfiguration(props); |
| 88 | + } |
| 89 | + |
| 90 | + @Bean |
| 91 | + public KStream<Integer, String> kStream(StreamsBuilder kStreamBuilder) { |
| 92 | + KStream<Integer, String> stream = kStreamBuilder.stream(STREAMING_TOPIC1); |
| 93 | + stream.foreach((K, v) -> { }); |
| 94 | + return stream; |
| 95 | + } |
| 96 | + |
| 97 | + @Bean |
| 98 | + List<Integer> callOrder() { |
| 99 | + return new ArrayList<>(); |
| 100 | + } |
| 101 | + |
| 102 | + @Bean |
| 103 | + StreamsBuilderFactoryBeanConfigurer three(List<Integer> callOrder) { |
| 104 | + return new StreamsBuilderFactoryBeanConfigurer() { |
| 105 | + |
| 106 | + @Override |
| 107 | + public void configure(StreamsBuilderFactoryBean factoryBean) { |
| 108 | + callOrder.add(3); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public int getOrder() { |
| 113 | + return Integer.MAX_VALUE; |
| 114 | + } |
| 115 | + |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + @Bean |
| 120 | + StreamsBuilderFactoryBeanConfigurer one(List<Integer> callOrder) { |
| 121 | + return new StreamsBuilderFactoryBeanConfigurer() { |
| 122 | + |
| 123 | + @Override |
| 124 | + public void configure(StreamsBuilderFactoryBean factoryBean) { |
| 125 | + callOrder.add(1); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public int getOrder() { |
| 130 | + return Integer.MIN_VALUE; |
| 131 | + } |
| 132 | + |
| 133 | + }; |
| 134 | + } |
| 135 | + |
| 136 | + @Bean |
| 137 | + StreamsBuilderFactoryBeanConfigurer two(List<Integer> callOrder) { |
| 138 | + return new StreamsBuilderFactoryBeanConfigurer() { |
| 139 | + |
| 140 | + @Override |
| 141 | + public void configure(StreamsBuilderFactoryBean factoryBean) { |
| 142 | + callOrder.add(2); |
| 143 | + } |
| 144 | + |
| 145 | + }; |
| 146 | + } |
| 147 | + |
| 148 | + @SuppressWarnings("deprecation") |
| 149 | + @Bean |
| 150 | + public StreamsBuilderFactoryBeanCustomizer wontBeFoundNotUnique(List<Integer> callOrder) { |
| 151 | + return fb -> fb.setStateListener((newState, oldState) -> { |
| 152 | + callOrder.add(4); |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments