package com.elab.mq.rocket.template;

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;

/**
 * 消息发送模版拓展
 *
 * @author ： liukx
 * @time ： 2019/7/18 - 16:14
 */
public class DefaultMQProducerExt {

    private DefaultMQProducer defaultMQProducer;

    public DefaultMQProducerExt(DefaultMQProducer defaultMQProducer) {
        this.defaultMQProducer = defaultMQProducer;
    }

    public DefaultMQProducer getDefaultMQProducer() {
        return defaultMQProducer;
    }

    public SendResult send(String topic, String tag, String key, byte[] body, int delayTimeLevel) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        Message message = new Message();
        message.setBody(body);
        message.setTopic(topic);
        message.setTags(tag);
        message.setKeys(key);
        message.setDelayTimeLevel(delayTimeLevel);
        return defaultMQProducer.send(message);
    }

    public SendResult send(String topic, String tag, String key, byte[] body) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        return send(topic, tag, key, body, 0);
    }

    public SendResult send(String topic, String key, byte[] body) throws InterruptedException, RemotingException,
            MQClientException, MQBrokerException {
        return send(topic, "*", key, body, 0);
    }

}
