|
| 1 | +/* |
| 2 | + * Copyright 2022 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 | +package org.springframework.kafka.kdocs.dynamic |
| 17 | + |
| 18 | +import org.apache.kafka.clients.consumer.ConsumerRecord |
| 19 | +import org.springframework.beans.factory.config.ConfigurableBeanFactory |
| 20 | +import org.springframework.boot.ApplicationArguments |
| 21 | +import org.springframework.boot.ApplicationRunner |
| 22 | +import org.springframework.boot.SpringApplication |
| 23 | +import org.springframework.boot.autoconfigure.SpringBootApplication |
| 24 | +import org.springframework.context.ApplicationContext |
| 25 | +import org.springframework.context.annotation.Bean |
| 26 | +import org.springframework.context.annotation.Scope |
| 27 | +import org.springframework.kafka.annotation.KafkaListener |
| 28 | +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory |
| 29 | +import org.springframework.kafka.config.TopicBuilder |
| 30 | +import org.springframework.kafka.core.KafkaAdmin.NewTopics |
| 31 | +import org.springframework.kafka.listener.ConcurrentMessageListenerContainer |
| 32 | +import org.springframework.kafka.listener.MessageListener |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Gary Russell |
| 36 | + * @since 2.8.9 |
| 37 | + */ |
| 38 | +@SpringBootApplication |
| 39 | +class Application { |
| 40 | + @Bean |
| 41 | + fun runner(factory: ConcurrentKafkaListenerContainerFactory<String, String>): ApplicationRunner { |
| 42 | + return ApplicationRunner { args: ApplicationArguments? -> createContainer(factory, "topic1", "group1") } |
| 43 | + } |
| 44 | + |
| 45 | + @Bean |
| 46 | + fun runner(applicationContext: ApplicationContext): ApplicationRunner { |
| 47 | + return ApplicationRunner { args: ApplicationArguments? -> |
| 48 | +// tag::getBeans[] |
| 49 | + |
| 50 | +applicationContext.getBean(MyPojo::class.java, "one", arrayOf("topic2")) |
| 51 | +applicationContext.getBean(MyPojo::class.java, "two", arrayOf("topic3")) |
| 52 | +// end::getBeans[] |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | +// tag::create[] |
| 57 | + |
| 58 | +private fun createContainer( |
| 59 | + factory: ConcurrentKafkaListenerContainerFactory<String, String>, topic: String, group: String |
| 60 | +): ConcurrentMessageListenerContainer<String, String> { |
| 61 | + val container = factory.createContainer(topic) |
| 62 | + container.containerProperties.messageListener = MyListener() |
| 63 | + container.containerProperties.groupId = group |
| 64 | + container.beanName = group |
| 65 | + container.start() |
| 66 | + return container |
| 67 | +} |
| 68 | +// end::create[] |
| 69 | + @Bean |
| 70 | + fun topics(): NewTopics { |
| 71 | + return NewTopics( |
| 72 | + TopicBuilder.name("topic1") |
| 73 | + .partitions(10) |
| 74 | + .replicas(1) |
| 75 | + .build(), |
| 76 | + TopicBuilder.name("topic2") |
| 77 | + .partitions(10) |
| 78 | + .replicas(1) |
| 79 | + .build(), |
| 80 | + TopicBuilder.name("topic3") |
| 81 | + .partitions(10) |
| 82 | + .replicas(1) |
| 83 | + .build() |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | +// tag::pojoBean[] |
| 88 | + |
| 89 | +@Bean |
| 90 | +@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) |
| 91 | +fun pojo(id: String?, topic: String?): MyPojo { |
| 92 | + return MyPojo(id, topic) |
| 93 | +} |
| 94 | +//end::pojoBean[] |
| 95 | + |
| 96 | + companion object { |
| 97 | + @JvmStatic |
| 98 | + fun main(args: Array<String>) { |
| 99 | + SpringApplication.run(Application::class.java, *args).close() |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// tag::listener[] |
| 105 | + |
| 106 | +class MyListener : MessageListener<String?, String?> { |
| 107 | + |
| 108 | + override fun onMessage(data: ConsumerRecord<String?, String?>) { |
| 109 | + // ... |
| 110 | + } |
| 111 | + |
| 112 | +} |
| 113 | +// end::listener[] |
| 114 | + |
| 115 | +// tag::pojo[] |
| 116 | + |
| 117 | +class MyPojo(id: String?, topic: String?) { |
| 118 | + |
| 119 | + @KafkaListener(id = "#{__listener.id}", topics = ["#{__listener.topics}"]) |
| 120 | + fun listen(`in`: String?) { |
| 121 | + println(`in`) |
| 122 | + } |
| 123 | + |
| 124 | +} |
| 125 | +// end::pojo[] |
0 commit comments