Package com.alibaba.ons.api.impl.rocketmq

Source Code of com.alibaba.ons.api.impl.rocketmq.ProducerImpl

package com.alibaba.ons.api.impl.rocketmq;

import java.util.Properties;

import com.alibaba.ons.api.Message;
import com.alibaba.ons.api.Producer;
import com.alibaba.ons.api.PropertyKeyConst;
import com.alibaba.ons.api.SendResult;
import com.alibaba.ons.api.exception.ONSClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;


public class ProducerImpl extends ONSClientAbstract implements Producer {
    private final DefaultMQProducer defaultMQProducer;


    public ProducerImpl(final Properties properties) {
        super(properties);
        this.defaultMQProducer = new DefaultMQProducer();

        String producerGroup =
                properties.getProperty(PropertyKeyConst.ProducerId, "__ONS_PRODUCER_DEFAULT_GROUP");
        this.defaultMQProducer.setProducerGroup(producerGroup);

        String sendMsgTimeoutMillis = properties.getProperty(PropertyKeyConst.SendMsgTimeoutMillis, "3000");
        this.defaultMQProducer.setSendMsgTimeout(Integer.parseInt(sendMsgTimeoutMillis));

        this.defaultMQProducer.setInstanceName(this.buildIntanceName());
    }


    @Override
    public Properties getProperties() {
        return this.properties;
    }


    @Override
    public void start() {
        try {
            this.defaultMQProducer.start();
            if (!this.sessionCredentials.equals(this.defaultMQProducer.getDefaultMQProducerImpl()
                .getmQClientFactory().getMQClientAPIImpl().getSessionCredentials())) {
                this.defaultMQProducer.getDefaultMQProducerImpl().getmQClientFactory().getMQClientAPIImpl()
                    .setSessionCredentials(this.sessionCredentials);
            }
        }
        catch (Exception e) {
            throw new ONSClientException("defaultMQProducer start exception", e);
        }
    }


    @Override
    public void shutdown() {
        this.defaultMQProducer.shutdown();
    }


    @Override
    public SendResult send(Message message) {
        com.alibaba.rocketmq.common.message.Message msgRMQ = ONSUtil.msgConvert(message);

        try {
            com.alibaba.rocketmq.client.producer.SendResult sendResultRMQ =
                    this.defaultMQProducer.send(msgRMQ);

            SendResult sendResult = new SendResult();
            sendResult.setMessageId(sendResultRMQ.getMsgId());
            return sendResult;
        }
        catch (Exception e) {
            throw new ONSClientException("defaultMQProducer send exception", e);
        }
    }


    @Override
    public void sendOneway(Message message) {
        com.alibaba.rocketmq.common.message.Message msgRMQ = ONSUtil.msgConvert(message);
        try {
            this.defaultMQProducer.sendOneway(msgRMQ);
        }
        catch (Exception e) {
            throw new ONSClientException("defaultMQProducer sendOneway exception", e);
        }
    }
}
TOP

Related Classes of com.alibaba.ons.api.impl.rocketmq.ProducerImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.