package com.jxy.marketall.task.publish;

import com.alibaba.fastjson.JSON;
import com.jxy.common.core.text.Convert;
import com.jxy.common.core.utils.DateUtils;
import com.jxy.marketall.dto.DateStringDto;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;


/**
 * @Description BTC指标推送
 * @Date 2024/1/24 12:59
 * @Author 杜懿
 */
@Configuration
@EnableScheduling
public class ProduceTest {
    @Autowired
    private RedisTemplate redisTemplate;

    /** 最新的一条数据 每1分钟推送一次*/
    //@Scheduled(cron = "0 0/1 * * * ?")
    public void BTCPrice() throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        //1.创建消息的生产者,指定生产者的组名
        DefaultMQProducer producer=new DefaultMQProducer("producer-group");

        //2.指定NameServer地址
        producer.setNamesrvAddr("10.168.2.125:9876");

        //3.启动生产者
        producer.start();

        //
        double random = Math.random();
        DateStringDto dto = new DateStringDto();
        dto.setDate(DateUtils.getNowDate());
        dto.setValue(Convert.toStr(random));
        String msg = JSON.toJSONString(dto);
        System.out.println("构造了数据："+msg);
        //4.创建消息对象  指定主题  标签  消息体
        Message message=new Message("BTC_price_1d","myTag",msg.getBytes());


        //5.发送消息  顺序消息
        SendResult send = producer.send(message, 10000);

        System.out.println(send);
        System.out.println("------------------------------------------");

        //6.关闭生产者
        producer.shutdown();
    }
}
