|
| 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 | + |
| 17 | +package org.springframework.kafka.aot; |
| 18 | + |
| 19 | +import java.util.stream.Stream; |
| 20 | +import java.util.zip.CRC32C; |
| 21 | + |
| 22 | +import org.apache.kafka.clients.admin.NewTopic; |
| 23 | +import org.apache.kafka.clients.consumer.Consumer; |
| 24 | +import org.apache.kafka.clients.consumer.CooperativeStickyAssignor; |
| 25 | +import org.apache.kafka.clients.consumer.RangeAssignor; |
| 26 | +import org.apache.kafka.clients.consumer.RoundRobinAssignor; |
| 27 | +import org.apache.kafka.clients.consumer.StickyAssignor; |
| 28 | +import org.apache.kafka.clients.producer.Producer; |
| 29 | +import org.apache.kafka.clients.producer.RoundRobinPartitioner; |
| 30 | +import org.apache.kafka.clients.producer.UniformStickyPartitioner; |
| 31 | +import org.apache.kafka.clients.producer.internals.DefaultPartitioner; |
| 32 | +import org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic; |
| 33 | +import org.apache.kafka.common.protocol.Message; |
| 34 | +import org.apache.kafka.common.serialization.ByteArrayDeserializer; |
| 35 | +import org.apache.kafka.common.serialization.ByteArraySerializer; |
| 36 | +import org.apache.kafka.common.serialization.ByteBufferDeserializer; |
| 37 | +import org.apache.kafka.common.serialization.ByteBufferSerializer; |
| 38 | +import org.apache.kafka.common.serialization.BytesDeserializer; |
| 39 | +import org.apache.kafka.common.serialization.BytesSerializer; |
| 40 | +import org.apache.kafka.common.serialization.DoubleDeserializer; |
| 41 | +import org.apache.kafka.common.serialization.DoubleSerializer; |
| 42 | +import org.apache.kafka.common.serialization.FloatDeserializer; |
| 43 | +import org.apache.kafka.common.serialization.FloatSerializer; |
| 44 | +import org.apache.kafka.common.serialization.IntegerDeserializer; |
| 45 | +import org.apache.kafka.common.serialization.IntegerSerializer; |
| 46 | +import org.apache.kafka.common.serialization.ListDeserializer; |
| 47 | +import org.apache.kafka.common.serialization.ListSerializer; |
| 48 | +import org.apache.kafka.common.serialization.LongDeserializer; |
| 49 | +import org.apache.kafka.common.serialization.LongSerializer; |
| 50 | +import org.apache.kafka.common.serialization.Serdes; |
| 51 | +import org.apache.kafka.common.serialization.StringDeserializer; |
| 52 | +import org.apache.kafka.common.serialization.StringSerializer; |
| 53 | +import org.apache.kafka.common.utils.AppInfoParser.AppInfo; |
| 54 | +import org.apache.kafka.common.utils.ImplicitLinkedHashCollection; |
| 55 | + |
| 56 | +import org.springframework.aop.framework.AopProxyUtils; |
| 57 | +import org.springframework.aot.hint.MemberCategory; |
| 58 | +import org.springframework.aot.hint.ReflectionHints; |
| 59 | +import org.springframework.aot.hint.RuntimeHints; |
| 60 | +import org.springframework.aot.hint.RuntimeHintsRegistrar; |
| 61 | +import org.springframework.aot.hint.support.RuntimeHintsUtils; |
| 62 | +import org.springframework.kafka.annotation.KafkaBootstrapConfiguration; |
| 63 | +import org.springframework.kafka.annotation.KafkaListener; |
| 64 | +import org.springframework.kafka.annotation.KafkaListenerAnnotationBeanPostProcessor; |
| 65 | +import org.springframework.kafka.annotation.KafkaListeners; |
| 66 | +import org.springframework.kafka.annotation.PartitionOffset; |
| 67 | +import org.springframework.kafka.annotation.TopicPartition; |
| 68 | +import org.springframework.kafka.config.AbstractKafkaListenerContainerFactory; |
| 69 | +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; |
| 70 | +import org.springframework.kafka.config.KafkaListenerContainerFactory; |
| 71 | +import org.springframework.kafka.config.KafkaListenerEndpointRegistry; |
| 72 | +import org.springframework.kafka.core.ConsumerFactory; |
| 73 | +import org.springframework.kafka.core.DefaultKafkaConsumerFactory; |
| 74 | +import org.springframework.kafka.core.DefaultKafkaProducerFactory; |
| 75 | +import org.springframework.kafka.core.KafkaAdmin; |
| 76 | +import org.springframework.kafka.core.KafkaOperations; |
| 77 | +import org.springframework.kafka.core.KafkaResourceFactory; |
| 78 | +import org.springframework.kafka.core.KafkaTemplate; |
| 79 | +import org.springframework.kafka.core.ProducerFactory; |
| 80 | +import org.springframework.kafka.listener.ConsumerProperties; |
| 81 | +import org.springframework.kafka.listener.ContainerProperties; |
| 82 | +import org.springframework.kafka.support.LoggingProducerListener; |
| 83 | +import org.springframework.kafka.support.ProducerListener; |
| 84 | +import org.springframework.kafka.support.serializer.DelegatingByTopicDeserializer; |
| 85 | +import org.springframework.kafka.support.serializer.DelegatingByTypeSerializer; |
| 86 | +import org.springframework.kafka.support.serializer.DelegatingDeserializer; |
| 87 | +import org.springframework.kafka.support.serializer.DelegatingSerializer; |
| 88 | +import org.springframework.kafka.support.serializer.ErrorHandlingDeserializer; |
| 89 | +import org.springframework.kafka.support.serializer.JsonDeserializer; |
| 90 | +import org.springframework.kafka.support.serializer.JsonSerializer; |
| 91 | +import org.springframework.kafka.support.serializer.ParseStringDeserializer; |
| 92 | +import org.springframework.kafka.support.serializer.StringOrBytesSerializer; |
| 93 | +import org.springframework.kafka.support.serializer.ToStringSerializer; |
| 94 | +import org.springframework.lang.Nullable; |
| 95 | + |
| 96 | +/** |
| 97 | + * {@link RuntimeHintsRegistrar} for Spring for Apache Kafka. |
| 98 | + * |
| 99 | + * @author Gary Russell |
| 100 | + * @since 3.0 |
| 101 | + * |
| 102 | + */ |
| 103 | +public class KafkaRuntimeHintsRegistrar implements RuntimeHintsRegistrar { |
| 104 | + |
| 105 | + @Override |
| 106 | + public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) { |
| 107 | + RuntimeHintsUtils.registerAnnotation(hints, KafkaListener.class); |
| 108 | + RuntimeHintsUtils.registerAnnotation(hints, KafkaListeners.class); |
| 109 | + RuntimeHintsUtils.registerAnnotation(hints, PartitionOffset.class); |
| 110 | + RuntimeHintsUtils.registerAnnotation(hints, TopicPartition.class); |
| 111 | + ReflectionHints reflectionHints = hints.reflection(); |
| 112 | + Stream.of( |
| 113 | + ConsumerProperties.class, |
| 114 | + ContainerProperties.class, |
| 115 | + ProducerListener.class) |
| 116 | + .forEach(type -> reflectionHints.registerType(type, |
| 117 | + builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_METHODS))); |
| 118 | + |
| 119 | + Stream.of( |
| 120 | + Message.class, |
| 121 | + ImplicitLinkedHashCollection.Element.class, |
| 122 | + NewTopic.class, |
| 123 | + AbstractKafkaListenerContainerFactory.class, |
| 124 | + ConcurrentKafkaListenerContainerFactory.class, |
| 125 | + KafkaListenerContainerFactory.class, |
| 126 | + KafkaListenerEndpointRegistry.class, |
| 127 | + DefaultKafkaConsumerFactory.class, |
| 128 | + DefaultKafkaProducerFactory.class, |
| 129 | + KafkaAdmin.class, |
| 130 | + KafkaOperations.class, |
| 131 | + KafkaResourceFactory.class, |
| 132 | + KafkaTemplate.class, |
| 133 | + ProducerFactory.class, |
| 134 | + KafkaOperations.class, |
| 135 | + ConsumerFactory.class, |
| 136 | + LoggingProducerListener.class, |
| 137 | + ImplicitLinkedHashCollection.Element.class, |
| 138 | + KafkaListenerAnnotationBeanPostProcessor.class) |
| 139 | + .forEach(type -> reflectionHints.registerType(type, |
| 140 | + builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, |
| 141 | + MemberCategory.INVOKE_DECLARED_METHODS, |
| 142 | + MemberCategory.INTROSPECT_PUBLIC_METHODS))); |
| 143 | + |
| 144 | + Stream.of( |
| 145 | + KafkaBootstrapConfiguration.class, |
| 146 | + CreatableTopic.class, |
| 147 | + KafkaListenerEndpointRegistry.class) |
| 148 | + .forEach(type -> reflectionHints.registerType(type, |
| 149 | + builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS))); |
| 150 | + |
| 151 | + Stream.of( |
| 152 | + AppInfo.class, |
| 153 | + // standard assignors |
| 154 | + CooperativeStickyAssignor.class, |
| 155 | + RangeAssignor.class, |
| 156 | + RoundRobinAssignor.class, |
| 157 | + StickyAssignor.class, |
| 158 | + // standard partitioners |
| 159 | + DefaultPartitioner.class, |
| 160 | + RoundRobinPartitioner.class, |
| 161 | + UniformStickyPartitioner.class, |
| 162 | + // standard serialization |
| 163 | + ByteArrayDeserializer.class, |
| 164 | + ByteArraySerializer.class, |
| 165 | + ByteBufferDeserializer.class, |
| 166 | + ByteBufferSerializer.class, |
| 167 | + BytesDeserializer.class, |
| 168 | + BytesSerializer.class, |
| 169 | + DoubleSerializer.class, |
| 170 | + DoubleDeserializer.class, |
| 171 | + FloatSerializer.class, |
| 172 | + FloatDeserializer.class, |
| 173 | + IntegerSerializer.class, |
| 174 | + IntegerDeserializer.class, |
| 175 | + ListDeserializer.class, |
| 176 | + ListSerializer.class, |
| 177 | + LongSerializer.class, |
| 178 | + LongDeserializer.class, |
| 179 | + StringDeserializer.class, |
| 180 | + StringSerializer.class, |
| 181 | + // Spring serialization |
| 182 | + DelegatingByTopicDeserializer.class, |
| 183 | + DelegatingByTypeSerializer.class, |
| 184 | + DelegatingDeserializer.class, |
| 185 | + ErrorHandlingDeserializer.class, |
| 186 | + DelegatingSerializer.class, |
| 187 | + JsonDeserializer.class, |
| 188 | + JsonSerializer.class, |
| 189 | + ParseStringDeserializer.class, |
| 190 | + StringOrBytesSerializer.class, |
| 191 | + ToStringSerializer.class, |
| 192 | + Serdes.class, |
| 193 | + Serdes.ByteArraySerde.class, |
| 194 | + Serdes.BytesSerde.class, |
| 195 | + Serdes.ByteBufferSerde.class, |
| 196 | + Serdes.DoubleSerde.class, |
| 197 | + Serdes.FloatSerde.class, |
| 198 | + Serdes.IntegerSerde.class, |
| 199 | + Serdes.LongSerde.class, |
| 200 | + Serdes.ShortSerde.class, |
| 201 | + Serdes.StringSerde.class, |
| 202 | + Serdes.UUIDSerde.class, |
| 203 | + Serdes.VoidSerde.class, |
| 204 | + CRC32C.class) |
| 205 | + .forEach(type -> reflectionHints.registerType(type, builder -> |
| 206 | + builder.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS))); |
| 207 | + |
| 208 | + hints.proxies().registerJdkProxy(AopProxyUtils.completeJdkProxyInterfaces(Consumer.class)); |
| 209 | + hints.proxies().registerJdkProxy(AopProxyUtils.completeJdkProxyInterfaces(Producer.class)); |
| 210 | + |
| 211 | + Stream.of( |
| 212 | + "org.apache.kafka.streams.processor.internals.StreamsPartitionAssignor", |
| 213 | + "org.apache.kafka.streams.errors.DefaultProductionExceptionHandler", |
| 214 | + "org.apache.kafka.streams.processor.FailOnInvalidTimestamp", |
| 215 | + "org.apache.kafka.streams.processor.internals.assignment.HighAvailabilityTaskAssignor", |
| 216 | + "org.apache.kafka.streams.processor.internals.assignment.StickyTaskAssignor", |
| 217 | + "org.apache.kafka.streams.processor.internals.assignment.FallbackPriorTaskAssignor", |
| 218 | + "org.apache.kafka.streams.errors.LogAndFailExceptionHandler") |
| 219 | + .forEach(type -> reflectionHints.registerTypeIfPresent(classLoader, type, builder -> |
| 220 | + builder.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS))); |
| 221 | + } |
| 222 | + |
| 223 | +} |
0 commit comments