package com.muyu.system.rabbit;

import com.alibaba.fastjson2.JSONObject;
import com.muyu.system.domain.SysConfig;
import jakarta.annotation.PostConstruct;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Log4j2
@RestController
@RequestMapping("/rabbit/test")
public class RabbitTest {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Bean
    public Queue initQueue(){
        return new Queue("rabbit.test.init");
    }

    @RabbitListener(queues = "rabbit.test.init")
    public void msg(SysConfig sysConfig){
        log.info("消息队列：[{}], 消息内容：[{}]", "rabbit.test.init", JSONObject.toJSONString(sysConfig));
    }

    @PostConstruct
    public void init(){
        new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            SysConfig sysConfig = SysConfig.builder()
                    .configId(1L)
                    .configKey("ceshi-key")
                    .configName("测试名称")
                    .configType("测试类型")
                    .configValue("测试值")
                    .build();
            rabbitTemplate.convertAndSend("rabbit.test.init",sysConfig);
        }).start();
    }

}
