首页 > 其他 > 详细

Rocketmq之一个JVM只能有一个producer

时间:2021-05-11 22:07:38      阅读:33      评论:0      收藏:0      [点我收藏+]

如果代码是这么写的

public static void main (String[] args) throws MQClientException {

        DefaultMQProducer defaultMQProducer = new DefaultMQProducer();
        defaultMQProducer.setProducerGroup("operationLogGroup");
        defaultMQProducer.setInstanceName("Constant.operationLogInstance");
        defaultMQProducer.setNamesrvAddr("Constant.rocketQueneAddr");

        DefaultMQProducer defaultMQProducer2 = new DefaultMQProducer();
        defaultMQProducer2.setProducerGroup("operationLogGroup");
        defaultMQProducer2.setInstanceName("Constant.operationLogInstance");
        defaultMQProducer2.setNamesrvAddr("Constant.rocketQueneAddr");
        try {
            defaultMQProducer.start();
            defaultMQProducer2.start();
            Message message = new Message();

            defaultMQProducer.send(message);
        } catch (Exception e) {
            System.out.println("produce operation log message error" + e.getMessage());
        } finally {
            defaultMQProducer.shutdown();
        }
    }

一旦执行会报错

produce operation log message errorThe producer group[operationLogGroup] has been created before, specify another name please.

源代码是在这里

DefaultMQProducerImpl#start()

public void start(final boolean startFactory) throws MQClientException {
        switch (this.serviceState) {
            case CREATE_JUST:
                this.serviceState = ServiceState.START_FAILED;

                this.checkConfig();

                if (!this.defaultMQProducer.getProducerGroup().equals(MixAll.CLIENT_INNER_PRODUCER_GROUP)) {
                    this.defaultMQProducer.changeInstanceNameToPID();
                }

                this.mQClientFactory = MQClientManager.getInstance().getOrCreateMQClientInstance(this.defaultMQProducer, rpcHook);

                boolean registerOK = mQClientFactory.registerProducer(this.defaultMQProducer.getProducerGroup(), this);
public boolean registerProducer(final String group, final DefaultMQProducerImpl producer) {
        if (null == group || null == producer) {
            return false;
        }

        MQProducerInner prev = this.producerTable.putIfAbsent(group, producer);
        if (prev != null) {
            log.warn("the producer group[{}] exist already.", group);
            return false;
        }

        return true;
    }

其中  private final ConcurrentMap<String/* group */, MQProducerInner> producerTable = new ConcurrentHashMap<String, MQProducerInner>(); 

所以一个JVM中同一个producergroup不能有两个producer的

 

Rocketmq之一个JVM只能有一个producer

原文:https://www.cnblogs.com/juniorMa/p/14756985.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!