package com.muyu.processing.controller;

import cn.hutool.json.JSONObject;
import com.muyu.common.core.constant.KafkaConstants;
import com.muyu.common.core.utils.uuid.UUID;
import com.muyu.common.kafka.config.KafkaProducerConfig;
import lombok.extern.log4j.Log4j2;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 消息队列测试-生产者
 * @Author：杨鹏
 * @Package：com.muyu.processing.controller
 * @Project：car-cloud-server
 * @name：TestKafka
 * @Date：2024/9/28 21:17
 */
@Log4j2
@RestController
@RequestMapping("/kafka")
public class TestKafka {


    @Resource
    private KafkaProducer<String, String> kafkaProducer;

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送Kafka消息
     * @return String
     */
    @GetMapping("/send")
    public String sendMsg(){
        JSONObject entries = new JSONObject();
        entries.set("vin","vin123468");
        entries.set("name","宝马");
        String entriesString = entries.toString();
        ProducerRecord<String, String> producerRecord = new ProducerRecord<>("zeshi", entriesString);
        kafkaProducer.send(producerRecord);
        return "OK";
    }

    /**
     * 发送MQ消息
     * @return String
     */
    @GetMapping("/sendMQ")
    public String sendMQ(){
        rabbitTemplate.convertAndSend("long_time_no_see","晨哀,好久不见",message -> {
            message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
            return message;
        });
        return "OK";
    }

    /**
     * 发送MQ队列消息
     * @return String
     */
    @GetMapping("/sendDui")
    public String sedDui() {
        rabbitTemplate.convertAndSend("myExchange","Im.fine","");
        return "OK";
    }
}
