Spring Batch中的kafka生产者,多线程编写者将重复消息发送到主题

时间:2020-08-18 23:55:22

标签: java multithreading spring-batch spring-kafka kafka-producer-api

我正在使用Writer的Spring Batch(如果条件失败,也有重试逻辑,如果再次失败,请尝试再次执行),并且如果条件在Writer中得到满足,我正在尝试向主题发送Kafka消息。我正在使用在配置中实例化了Bean的Kafka模板。

    @Bean
    public Producer<Long, String> producerConfigs() {
        Properties props =  new Properties();
        //bootstrap servers and protocol configs here        
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, LongSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        return new KafkaProducer<>(props);
    }

在编写器部分,我在executorService.submit(...)中使用了线程计数为50,我正在使用以下代码将消息发送到主题

    public void send(String message){
        LOG.info("sending message='{}' to topic='{}'", message, topic);
        kafkaTemplate.send(topic, message).get();
    }

我的问题是我正在获取有关该主题的重复日志和重复的Kafka消息。

0 个答案:

没有答案