package com.elab.example.springbootdemo.rocketmq;

import com.alibaba.fastjson.JSONObject;
import com.elab.core.utils.RandomUtils;
import com.elab.example.springbootdemo.SpringbootDemoApplication;
import com.elab.mq.rocket.template.DefaultMQProducerExt;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.UnsupportedEncodingException;

/**
 * 生产者发送消息
 *
 * @author ： liukx
 * @time ： 2019/9/16 - 15:26
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {SpringbootDemoApplication.class})
public class producerTest {

    @Autowired
    private DefaultMQProducerExt rocketMQTemplate;


    @Test
    public void sendJsonMsg() throws UnsupportedEncodingException, InterruptedException, RemotingException, MQClientException, MQBrokerException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", "true");
        jsonObject.put("msg", "my name is lkx");
        Message message = new Message();
        message.setBody(jsonObject.toJSONString().getBytes());
        message.setTags("");
        SendResult sendResult = rocketMQTemplate.send("elab-marketing-user", "tag", RandomUtils.randomString(5),
                jsonObject.toJSONString().getBytes());
        SendStatus sendStatus = sendResult.getSendStatus();
        System.out.println("--->" + sendResult.toString());
    }


}
