package com.gyee.edge.gateway.bridge.rocketmq.producter;


import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//异步请求
@Slf4j
@Component
public class AsyncProducter {

    @Value("${rocketmq.namesrvaddr}")
    String nameSrvAddr;

    @Autowired
    private Gyfp2Protocol gyfp2Protocol;
    public  void dataSendProducter() throws Exception {
        //实例化消息的生产者
        DefaultMQProducer producer = new DefaultMQProducer("group_test");
        //设置nameServer的地址
        producer.setNamesrvAddr(nameSrvAddr);
        //启动producer实例
        producer.start();
        //支持protobuf协议          byte[] bytes = ReadGolden.protoData();
        //支持GYFP2协议
        byte[] bytes = gyfp2Protocol.toBytes();
            //创建消息
            Message message = new Message("keyMessage"/*Topic*/,
                    "TagA"/* Tag*/, "OrderID888", bytes);
            producer.send(message, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    log.info("消息发送成功",sendResult);
                }

                @Override
                public void onException(Throwable throwable) {
                    log.info("消息发送失败", throwable);
                    throwable.printStackTrace();
                }
            });
            /*Thread.sleep(1000);
        producer.shutdown();*/
    }
}
