| /** |
| * |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you 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.apacher.airavata.k8s.task.egress.messaging; |
| |
| import org.apache.kafka.clients.consumer.ConsumerConfig; |
| import org.apache.kafka.common.serialization.StringDeserializer; |
| import org.springframework.beans.factory.annotation.Value; |
| import org.springframework.context.annotation.Bean; |
| import org.springframework.context.annotation.Configuration; |
| import org.springframework.kafka.annotation.EnableKafka; |
| import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; |
| import org.springframework.kafka.config.KafkaListenerContainerFactory; |
| import org.springframework.kafka.core.ConsumerFactory; |
| import org.springframework.kafka.core.DefaultKafkaConsumerFactory; |
| import org.springframework.kafka.listener.AbstractMessageListenerContainer; |
| import org.springframework.kafka.listener.ConcurrentMessageListenerContainer; |
| |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| /** |
| * TODO: Class level comments please |
| * |
| * @author dimuthu |
| * @since 1.0.0-SNAPSHOT |
| */ |
| @Configuration |
| @EnableKafka |
| public class ReceiverConfig { |
| |
| @Value("${kafka.bootstrap-servers}") |
| private String bootstrapServers; |
| |
| @Value("${task.group.name}") |
| private String taskGroupName; |
| |
| @Bean |
| public Map<String, Object> consumerConfigs() { |
| Map<String, Object> props = new HashMap<>(); |
| // list of host:port pairs used for establishing the initial connections to the Kakfa cluster |
| props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); |
| props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); |
| props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); |
| // allows a pool of processes to divide the work of consuming and processing records |
| props.put(ConsumerConfig.GROUP_ID_CONFIG, taskGroupName); |
| props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false); |
| return props; |
| } |
| |
| @Bean |
| public ConsumerFactory<String, String> consumerFactory() { |
| return new DefaultKafkaConsumerFactory<String, String>(consumerConfigs()); |
| } |
| |
| @Bean |
| public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() { |
| ConcurrentKafkaListenerContainerFactory<String, String> factory = |
| new ConcurrentKafkaListenerContainerFactory<>(); |
| factory.setConsumerFactory(consumerFactory()); |
| factory.getContainerProperties().setAckMode(AbstractMessageListenerContainer.AckMode.MANUAL_IMMEDIATE); |
| return factory; |
| } |
| |
| @Bean |
| public KafkaReceiver receiver() { |
| return new KafkaReceiver(); |
| } |
| } |