博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
聊聊AbstractOMSProducer
阅读量:5791 次
发布时间:2019-06-18

本文共 8854 字,大约阅读时间需要 29 分钟。

本文主要研究一下AbstractOMSProducer

AbstractOMSProducer

io/openmessaging/rocketmq/producer/AbstractOMSProducer.java

abstract class AbstractOMSProducer implements ServiceLifecycle, MessageFactory {    final static Logger log = ClientLogger.getLog();    final KeyValue properties;    final DefaultMQProducer rocketmqProducer;    private boolean started = false;    final ClientConfig clientConfig;    AbstractOMSProducer(final KeyValue properties) {        this.properties = properties;        this.rocketmqProducer = new DefaultMQProducer();        this.clientConfig = BeanUtils.populate(properties, ClientConfig.class);        String accessPoints = clientConfig.getOmsAccessPoints();        if (accessPoints == null || accessPoints.isEmpty()) {            throw new OMSRuntimeException("-1", "OMS AccessPoints is null or empty.");        }        this.rocketmqProducer.setNamesrvAddr(accessPoints.replace(',', ';'));        this.rocketmqProducer.setProducerGroup(clientConfig.getRmqProducerGroup());        String producerId = buildInstanceName();        this.rocketmqProducer.setSendMsgTimeout(clientConfig.getOmsOperationTimeout());        this.rocketmqProducer.setInstanceName(producerId);        this.rocketmqProducer.setMaxMessageSize(1024 * 1024 * 4);        properties.put(PropertyKeys.PRODUCER_ID, producerId);    }    @Override    public synchronized void startup() {        if (!started) {            try {                this.rocketmqProducer.start();            } catch (MQClientException e) {                throw new OMSRuntimeException("-1", e);            }        }        this.started = true;    }    @Override    public synchronized void shutdown() {        if (this.started) {            this.rocketmqProducer.shutdown();        }        this.started = false;    }    OMSRuntimeException checkProducerException(String topic, String msgId, Throwable e) {        if (e instanceof MQClientException) {            if (e.getCause() != null) {                if (e.getCause() instanceof RemotingTimeoutException) {                    return new OMSTimeOutException("-1", String.format("Send message to broker timeout, %dms, Topic=%s, msgId=%s",                        this.rocketmqProducer.getSendMsgTimeout(), topic, msgId), e);                } else if (e.getCause() instanceof MQBrokerException || e.getCause() instanceof RemotingConnectException) {                    MQBrokerException brokerException = (MQBrokerException) e.getCause();                    return new OMSRuntimeException("-1", String.format("Received a broker exception, Topic=%s, msgId=%s, %s",                        topic, msgId, brokerException.getErrorMessage()), e);                }            }            // Exception thrown by local.            else {                MQClientException clientException = (MQClientException) e;                if (-1 == clientException.getResponseCode()) {                    return new OMSRuntimeException("-1", String.format("Topic does not exist, Topic=%s, msgId=%s",                        topic, msgId), e);                } else if (ResponseCode.MESSAGE_ILLEGAL == clientException.getResponseCode()) {                    return new OMSMessageFormatException("-1", String.format("A illegal message for RocketMQ, Topic=%s, msgId=%s",                        topic, msgId), e);                }            }        }        return new OMSRuntimeException("-1", "Send message to RocketMQ broker failed.", e);    }    protected void checkMessageType(Message message) {        if (!(message instanceof BytesMessage)) {            throw new OMSNotSupportedException("-1", "Only BytesMessage is supported.");        }    }    @Override    public BytesMessage createBytesMessageToTopic(final String topic, final byte[] body) {        BytesMessage bytesMessage = new BytesMessageImpl();        bytesMessage.setBody(body);        bytesMessage.headers().put(MessageHeader.TOPIC, topic);        return bytesMessage;    }    @Override    public BytesMessage createBytesMessageToQueue(final String queue, final byte[] body) {        BytesMessage bytesMessage = new BytesMessageImpl();        bytesMessage.setBody(body);        bytesMessage.headers().put(MessageHeader.QUEUE, queue);        return bytesMessage;    }}
  • AbstractOMSProducer实现了ServiceLifecycle以及MessageFactory
  • ServiceLifecycle的startup里头调用DefaultMQProducer的start方法,shutdown里头调用DefaultMQProducer的shutdown方法
  • MessageFactory的createBytesMessage的方法主要是返回了BytesMessageImpl

MessageFactory

io/openmessaging/openmessaging-api/0.1.0-alpha/openmessaging-api-0.1.0-alpha-sources.jar!/io/openmessaging/MessageFactory.java

/** * A factory interface for creating {@code Message} objects. * * @author vintagewang@apache.org * @author yukon@apache.org */public interface MessageFactory {    /**     * Creates a {@code BytesMessage} object. A {@code BytesMessage} object is used to send a message containing a     * stream of uninterpreted bytes.     * 

* The returned {@code BytesMessage} object only can be sent to the specified topic. * * @param topic the target topic to send * @param body the body data for a message * @return the created {@code BytesMessage} object * @throws OMSRuntimeException if the OMS provider fails to create this message due to some internal error. */ BytesMessage createBytesMessageToTopic(String topic, byte[] body); /** * Creates a {@code BytesMessage} object. A {@code BytesMessage} object is used to send a message containing a * stream of uninterpreted bytes. *

* The returned {@code BytesMessage} object only can be sent to the specified queue. * * @param queue the target queue to send * @param body the body data for a message * @return the created {@code BytesMessage} object * @throws OMSRuntimeException if the OMS provider fails to create this message due to some internal error. */ BytesMessage createBytesMessageToQueue(String queue, byte[] body);}

  • 0.1.0-alpha这个版本区分了topic跟queue,不过在最新版已经移除掉topic,统一为createBytesMessage方法,发送到queue

openmessaging-java/openmessaging-api/src/main/java/io/openmessaging/MessageFactory.java

public interface MessageFactory {    /**     * Creates a {@code BytesMessage} object. A {@code BytesMessage} object is used to send a message containing a     * stream of uninterpreted bytes.     * 

* The returned {@code BytesMessage} object only can be sent to the specified queue. * * @param queue the target queue to send * @param body the body data for a message * @return the created {@code BytesMessage} object * @throws OMSRuntimeException if the OMS provider fails to create this message due to some internal error. */ BytesMessage createBytesMessage(String queue, byte[] body);}

BytesMessageImpl

io/openmessaging/rocketmq/domain/BytesMessageImpl.java

public class BytesMessageImpl implements BytesMessage {    private KeyValue headers;    private KeyValue properties;    private byte[] body;    public BytesMessageImpl() {        this.headers = OMS.newKeyValue();        this.properties = OMS.newKeyValue();    }    @Override    public byte[] getBody() {        return body;    }    @Override    public BytesMessage setBody(final byte[] body) {        this.body = body;        return this;    }    @Override    public KeyValue headers() {        return headers;    }    @Override    public KeyValue properties() {        return properties;    }    @Override    public Message putHeaders(final String key, final int value) {        headers.put(key, value);        return this;    }    @Override    public Message putHeaders(final String key, final long value) {        headers.put(key, value);        return this;    }    @Override    public Message putHeaders(final String key, final double value) {        headers.put(key, value);        return this;    }    @Override    public Message putHeaders(final String key, final String value) {        headers.put(key, value);        return this;    }    @Override    public Message putProperties(final String key, final int value) {        properties.put(key, value);        return this;    }    @Override    public Message putProperties(final String key, final long value) {        properties.put(key, value);        return this;    }    @Override    public Message putProperties(final String key, final double value) {        properties.put(key, value);        return this;    }    @Override    public Message putProperties(final String key, final String value) {        properties.put(key, value);        return this;    }    @Override    public String toString() {        return ToStringBuilder.reflectionToString(this);    }}
  • 用byte[]作为body实现BytesMessage接口

小结

rocketmq的4.2.0版本的AbstractOMSProducer实现了ServiceLifecycle以及MessageFactory,其实现的open-messaging api的版本为0.1.0-alpha,该版本的MessageFactory里头创建message的方法区分了topic和queue,而在最新的0.3.2-alpha-SNAPSHOT版本,已经移除了topic的概念,统一发送到queue。

doc

转载地址:http://zqwfx.baihongyu.com/

你可能感兴趣的文章
getResourceAsStream的3种路径配置
查看>>
switch语句小练习
查看>>
组合逻辑电路
查看>>
POP-一个点击带有放大还原的动画效果
查看>>
UE4材质是什么样的机制
查看>>
使用QTP录制自带Flight小实例
查看>>
JProfiler学习笔记
查看>>
Loadrunner脚本编程(4)-数据类型操作和字符串操作
查看>>
STL 算法
查看>>
分享:Backbone.js 样例站点与入门指南
查看>>
图的基本算法
查看>>
《架构之美》摘录三
查看>>
HTML基础(一)
查看>>
boost.circular_buffer简介
查看>>
Database Appliance并非Mini版的Exadata-还原真实的Oracle Unbreakable Database Appliance
查看>>
网页图片缩放(js)
查看>>
如何用Fiddler对Android应用进行抓包
查看>>
iOS为所需要的视图添加模糊效果--UIVisualEffectView
查看>>
Kibana登录认证设置
查看>>
volley 应用 GET POST请求 图片异步加载
查看>>