package com.muyu.processing.controller;

import cn.hutool.json.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
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 {

    private static final String TIPSY = "tipsy";
    private static final String VIN = "63YCZDY6336C8H4CA";

    /**
     * 上线监听队列名称
     */
    private static final String ON_LINE_MONITORING = "MQ_ON_LINE_MONITORING";
    /**
     * 下线监听队列名称
     */
    private static final String OFFLINE_MONITORING = "MQ_OFFLINE_MONITORING";
    @Resource
    private KafkaProducer<String, String> kafkaProducer;

    @Resource
    private RabbitTemplate rabbitTemplate;

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

    /**
     * 上线监听测试
     */
    @GetMapping("/sendMQ")
    public String sendMQ(){
        // 发送消息
        rabbitTemplate.convertAndSend(ON_LINE_MONITORING,VIN);
        return "OK";
    }

    /**
     * 下线监听测试
     */
    @GetMapping("/sendDui")
    public String sedDui() {
        // 发送消息
        rabbitTemplate.convertAndSend(OFFLINE_MONITORING,VIN);
        return "OK";
    }
}
