package com.zm.order.tool.server.config;

import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <p>
 *
 * </p>
 *
 * @author nianchang
 * @date 2023/08/14 4:35 下午
 */
@Component
public class KafkaProducerLauncher {

    @Autowired
    private KafkaProducer<String, Object> producer;

    public void sendAsync(String topic, String shardingKey, String message) throws Exception {
        try {
            producer.send(new ProducerRecord<>(topic, shardingKey, message));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void sendAsync(String topic, String shardingKey, String message, Callback callback) throws Exception {
        try {
            producer.send(new ProducerRecord<>(topic, shardingKey, message), callback);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
