|
| 1 | +/* |
| 2 | + * Copyright 2016-2016 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 | + * http://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.boot.kafka; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.apache.kafka.clients.CommonClientConfigs; |
| 23 | +import org.apache.kafka.common.metrics.KafkaMetric; |
| 24 | +import org.apache.kafka.common.metrics.MetricsReporter; |
| 25 | + |
| 26 | +import org.springframework.boot.autoconfigure.kafka.KafkaProperties; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Configuration; |
| 29 | +import org.springframework.kafka.core.ConsumerFactory; |
| 30 | +import org.springframework.kafka.core.DefaultKafkaConsumerFactory; |
| 31 | +import org.springframework.kafka.core.DefaultKafkaProducerFactory; |
| 32 | +import org.springframework.kafka.core.ProducerFactory; |
| 33 | + |
| 34 | +/** |
| 35 | + * Example custom kafka configuration beans used when the user wants to |
| 36 | + * apply different common properties to the producer and consumer. |
| 37 | + * |
| 38 | + * @author Gary Russell |
| 39 | + * @since 1.5 |
| 40 | + * |
| 41 | + */ |
| 42 | +public class KafkaSpecialProducerConsumerConfigExample { |
| 43 | + |
| 44 | + // tag::configuration[] |
| 45 | + @Configuration |
| 46 | + public static class CustomKafkaBeans { |
| 47 | + |
| 48 | + /** |
| 49 | + * Customized ProducerFactory bean. |
| 50 | + * @param properties the kafka properties. |
| 51 | + * @return the bean. |
| 52 | + */ |
| 53 | + @Bean |
| 54 | + public ProducerFactory<?, ?> kafkaProducerFactory(KafkaProperties properties) { |
| 55 | + Map<String, Object> producerProperties = properties.buildProducerProperties(); |
| 56 | + producerProperties.put(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG, |
| 57 | + MyProducerMetricsReporter.class); |
| 58 | + return new DefaultKafkaProducerFactory<Object, Object>(producerProperties); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Customized ConsumerFactory bean. |
| 63 | + * @param properties the kafka properties. |
| 64 | + * @return the bean. |
| 65 | + */ |
| 66 | + @Bean |
| 67 | + public ConsumerFactory<?, ?> kafkaConsumerFactory(KafkaProperties properties) { |
| 68 | + Map<String, Object> consumererProperties = properties.buildConsumerProperties(); |
| 69 | + consumererProperties.put(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG, |
| 70 | + MyConsumerMetricsReporter.class); |
| 71 | + return new DefaultKafkaConsumerFactory<Object, Object>(consumererProperties); |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + // end::configuration[] |
| 76 | + |
| 77 | + public static class MyConsumerMetricsReporter implements MetricsReporter { |
| 78 | + |
| 79 | + @Override |
| 80 | + public void configure(Map<String, ?> configs) { |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void init(List<KafkaMetric> metrics) { |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void metricChange(KafkaMetric metric) { |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void metricRemoval(KafkaMetric metric) { |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void close() { |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + public static class MyProducerMetricsReporter implements MetricsReporter { |
| 102 | + |
| 103 | + @Override |
| 104 | + public void configure(Map<String, ?> configs) { |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void init(List<KafkaMetric> metrics) { |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void metricChange(KafkaMetric metric) { |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void metricRemoval(KafkaMetric metric) { |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void close() { |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments