com.alibaba.rocketmq.client.impl.factory.MQClientInstance Java Examples

The following examples show how to use com.alibaba.rocketmq.client.impl.factory.MQClientInstance. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MQAdminImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 6 votes vote down vote up
public Set<MessageQueue> fetchSubscribeMessageQueues(String topic) throws MQClientException {
    try {
        TopicRouteData topicRouteData = this.mQClientFactory.getMQClientAPIImpl()
            .getTopicRouteInfoFromNameServer(topic, 1000 * 3);
        if (topicRouteData != null) {
            Set<MessageQueue> mqList =
                    MQClientInstance.topicRouteData2TopicSubscribeInfo(topic, topicRouteData);
            if (!mqList.isEmpty()) {
                return mqList;
            }
            else {
                throw new MQClientException(
                    "Can not find Message Queue for this topic, " + topic + " Namesrv return empty",
                    null);
            }
        }
    }
    catch (Exception e) {
        throw new MQClientException(
            "Can not find Message Queue for this topic, " + topic
                    + FAQUrl.suggestTodo(FAQUrl.MQLIST_NOT_EXIST), //
            e);
    }

    throw new MQClientException("Unknow why, Can not find Message Queue for this topic, " + topic, null);
}
 
Example #2
Source File: MQAdminImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 6 votes vote down vote up
public List<MessageQueue> fetchPublishMessageQueues(String topic) throws MQClientException {
    try {
        TopicRouteData topicRouteData = this.mQClientFactory.getMQClientAPIImpl()
            .getTopicRouteInfoFromNameServer(topic, 1000 * 3);
        if (topicRouteData != null) {
            TopicPublishInfo topicPublishInfo =
                    MQClientInstance.topicRouteData2TopicPublishInfo(topic, topicRouteData);
            if (topicPublishInfo != null && topicPublishInfo.ok()) {
                return topicPublishInfo.getMessageQueueList();
            }
        }
    }
    catch (Exception e) {
        throw new MQClientException("Can not find Message Queue for this topic, " + topic, e);
    }

    throw new MQClientException("Unknow why, Can not find Message Queue for this topic, " + topic, null);
}
 
Example #3
Source File: MQAdminImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public Set<MessageQueue> fetchSubscribeMessageQueues(String topic) throws MQClientException {
    try {
        TopicRouteData topicRouteData = this.mQClientFactory.getMQClientAPIImpl().getTopicRouteInfoFromNameServer(topic, timeoutMillis);
        if (topicRouteData != null) {
            Set<MessageQueue> mqList = MQClientInstance.topicRouteData2TopicSubscribeInfo(topic, topicRouteData);
            if (!mqList.isEmpty()) {
                return mqList;
            } else {
                throw new MQClientException("Can not find Message Queue for this topic, " + topic + " Namesrv return empty", null);
            }
        }
    } catch (Exception e) {
        throw new MQClientException(
                "Can not find Message Queue for this topic, " + topic + FAQUrl.suggestTodo(FAQUrl.MQLIST_NOT_EXIST), //
                e);
    }

    throw new MQClientException("Unknow why, Can not find Message Queue for this topic, " + topic, null);
}
 
Example #4
Source File: MQAdminImpl.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 6 votes vote down vote up
public Set<MessageQueue> fetchSubscribeMessageQueues(String topic) throws MQClientException {
    try {
        TopicRouteData topicRouteData = this.mQClientFactory.getMQClientAPIImpl().getTopicRouteInfoFromNameServer(topic, timeoutMillis);
        if (topicRouteData != null) {
            Set<MessageQueue> mqList = MQClientInstance.topicRouteData2TopicSubscribeInfo(topic, topicRouteData);
            if (!mqList.isEmpty()) {
                return mqList;
            }
            else {
                throw new MQClientException("Can not find Message Queue for this topic, " + topic + " Namesrv return empty", null);
            }
        }
    }
    catch (Exception e) {
        throw new MQClientException(
            "Can not find Message Queue for this topic, " + topic + FAQUrl.suggestTodo(FAQUrl.MQLIST_NOT_EXIST), //
            e);
    }

    throw new MQClientException("Unknow why, Can not find Message Queue for this topic, " + topic, null);
}
 
Example #5
Source File: MQAdminImpl.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 6 votes vote down vote up
public List<MessageQueue> fetchPublishMessageQueues(String topic) throws MQClientException {
    try {
        TopicRouteData topicRouteData = this.mQClientFactory.getMQClientAPIImpl().getTopicRouteInfoFromNameServer(topic, timeoutMillis);
        if (topicRouteData != null) {
            TopicPublishInfo topicPublishInfo = MQClientInstance.topicRouteData2TopicPublishInfo(topic, topicRouteData);
            if (topicPublishInfo != null && topicPublishInfo.ok()) {
                return topicPublishInfo.getMessageQueueList();
            }
        }
    }
    catch (Exception e) {
        throw new MQClientException("Can not find Message Queue for this topic, " + topic, e);
    }

    throw new MQClientException("Unknow why, Can not find Message Queue for this topic, " + topic, null);
}
 
Example #6
Source File: LocalFileOffsetStore.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public LocalFileOffsetStore(MQClientInstance mQClientFactory, String groupName) {
    this.mQClientFactory = mQClientFactory;
    this.groupName = groupName;
    this.storePath = LocalOffsetStoreDir + File.separator + //
            this.mQClientFactory.getClientId() + File.separator + //
            this.groupName + File.separator + //
            "offsets.json";
}
 
Example #7
Source File: RebalanceImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
public RebalanceImpl(String consumerGroup, MessageModel messageModel,
        AllocateMessageQueueStrategy allocateMessageQueueStrategy, MQClientInstance mQClientFactory) {
    this.consumerGroup = consumerGroup;
    this.messageModel = messageModel;
    this.allocateMessageQueueStrategy = allocateMessageQueueStrategy;
    this.mQClientFactory = mQClientFactory;
}
 
Example #8
Source File: LocalFileOffsetStore.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
public LocalFileOffsetStore(MQClientInstance mQClientFactory, String groupName) {
    this.mQClientFactory = mQClientFactory;
    this.groupName = groupName;
    this.storePath = LocalOffsetStoreDir + File.separator + //
            this.mQClientFactory.getClientId() + File.separator + //
            this.groupName + File.separator + //
            "offsets.json";
}
 
Example #9
Source File: AllocateMQSubCommand.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) {
    DefaultMQAdminExt adminExt = new DefaultMQAdminExt(rpcHook);
    adminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    try {
        adminExt.start();

        String topic = commandLine.getOptionValue('t').trim();
        String ips = commandLine.getOptionValue('i').trim();
        final String[] split = ips.split(",");
        final List<String> ipList = new LinkedList<String>();
        for (String ip : split) {
            ipList.add(ip);
        }

        final TopicRouteData topicRouteData = adminExt.examineTopicRouteInfo(topic);
        final Set<MessageQueue> mqs = MQClientInstance.topicRouteData2TopicSubscribeInfo(topic, topicRouteData);

        final AllocateMessageQueueAveragely averagely = new AllocateMessageQueueAveragely();


        RebalanceResult rr = new RebalanceResult();

        for (String i : ipList) {
            final List<MessageQueue> mqResult = averagely.allocate("aa", i, new ArrayList<MessageQueue>(mqs), ipList);
            rr.getResult().put(i, mqResult);
        }

        final String json = RemotingSerializable.toJson(rr, false);
        System.out.println(json);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        adminExt.shutdown();
    }
}
 
Example #10
Source File: MQAdminImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public List<MessageQueue> fetchPublishMessageQueues(String topic) throws MQClientException {
    try {
        TopicRouteData topicRouteData = this.mQClientFactory.getMQClientAPIImpl().getTopicRouteInfoFromNameServer(topic, timeoutMillis);
        if (topicRouteData != null) {
            TopicPublishInfo topicPublishInfo = MQClientInstance.topicRouteData2TopicPublishInfo(topic, topicRouteData);
            if (topicPublishInfo != null && topicPublishInfo.ok()) {
                return topicPublishInfo.getMessageQueueList();
            }
        }
    } catch (Exception e) {
        throw new MQClientException("Can not find Message Queue for this topic, " + topic, e);
    }

    throw new MQClientException("Unknow why, Can not find Message Queue for this topic, " + topic, null);
}
 
Example #11
Source File: RebalanceImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public RebalanceImpl(String consumerGroup, MessageModel messageModel, AllocateMessageQueueStrategy allocateMessageQueueStrategy,
                     MQClientInstance mQClientFactory) {
    this.consumerGroup = consumerGroup;
    this.messageModel = messageModel;
    this.allocateMessageQueueStrategy = allocateMessageQueueStrategy;
    this.mQClientFactory = mQClientFactory;
}
 
Example #12
Source File: LocalFileOffsetStore.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public LocalFileOffsetStore(MQClientInstance mQClientFactory, String groupName) {
    this.mQClientFactory = mQClientFactory;
    this.groupName = groupName;
    this.storePath = LocalOffsetStoreDir + File.separator + //
            this.mQClientFactory.getClientId() + File.separator + //
            this.groupName + File.separator + //
            "offsets.json";
}
 
Example #13
Source File: MQAdminInstance.java    From rocket-console with Apache License 2.0 5 votes vote down vote up
/**
 * 使用反射获取到DefaultMQAdminExt中的RemotingCommand
 *
 * @return
 */
public static RemotingClient threadLocalRemotingClient() {
    DefaultMQAdminExtImpl defaultMQAdminExtImpl = Reflect.on(MQAdminInstance.threadLocalMQAdminExt()).get("defaultMQAdminExtImpl");
    MQClientInstance mqClientInstance = Reflect.on(defaultMQAdminExtImpl).get("mqClientInstance");
    MQClientAPIImpl mQClientAPIImpl = Reflect.on(mqClientInstance).get("mQClientAPIImpl");
    return Reflect.on(mQClientAPIImpl).get("remotingClient");
}
 
Example #14
Source File: AllocateMQSubCommand.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) {
    DefaultMQAdminExt adminExt = new DefaultMQAdminExt(rpcHook);
    adminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    try {
        adminExt.start();

        String topic = commandLine.getOptionValue('t').trim();
        String ips = commandLine.getOptionValue('i').trim();
        final String[] split = ips.split(",");
        final List<String> ipList = new LinkedList<String>();
        for (String ip : split) {
            ipList.add(ip);
        }

        final TopicRouteData topicRouteData = adminExt.examineTopicRouteInfo(topic);
        final Set<MessageQueue> mqs = MQClientInstance.topicRouteData2TopicSubscribeInfo(topic, topicRouteData);

        final AllocateMessageQueueAveragely averagely = new AllocateMessageQueueAveragely();


        RebalanceResult rr = new RebalanceResult();

        for (String i : ipList) {
            final List<MessageQueue> mqResult = averagely.allocate("aa", i, new ArrayList<MessageQueue>(mqs), ipList);
            rr.getResult().put(i, mqResult);
        }

        final String json = RemotingSerializable.toJson(rr, false);
        System.out.println(json);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        adminExt.shutdown();
    }
}
 
Example #15
Source File: RebalanceImpl.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public RebalanceImpl(String consumerGroup, MessageModel messageModel,
                     AllocateMessageQueueStrategy allocateMessageQueueStrategy, MQClientInstance mQClientFactory) {
    this.consumerGroup = consumerGroup;
    this.messageModel = messageModel;
    this.allocateMessageQueueStrategy = allocateMessageQueueStrategy;
    this.mQClientFactory = mQClientFactory;
}
 
Example #16
Source File: RebalanceService.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public RebalanceService(MQClientInstance mqClientFactory) {
    this.mqClientFactory = mqClientFactory;
}
 
Example #17
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public SendResult sendMessage(//
                              final String addr, // 1
                              final String brokerName, // 2
                              final Message msg, // 3
                              final SendMessageRequestHeader requestHeader, // 4
                              final long timeoutMillis, // 5
                              final CommunicationMode communicationMode, // 6
                              final SendCallback sendCallback, // 7
                              final TopicPublishInfo topicPublishInfo, // 8
                              final MQClientInstance instance, // 9
                              final int retryTimesWhenSendFailed, // 10
                              final SendMessageContext context, // 11
                              final DefaultMQProducerImpl producer // 12
                              ) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = null;
    if (sendSmartMsg) {
        SendMessageRequestHeaderV2 requestHeaderV2 = SendMessageRequestHeaderV2.createSendMessageRequestHeaderV2(requestHeader);
        request = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE_V2, requestHeaderV2);
    } else {
        request = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, requestHeader);
    }

    request.setBody(msg.getBody());

    switch (communicationMode) {
        case ONEWAY:
            this.remotingClient.invokeOneway(addr, request, timeoutMillis);
            return null;
        case ASYNC:
            final AtomicInteger times = new AtomicInteger();
            this.sendMessageAsync(addr, brokerName, msg, timeoutMillis, request, sendCallback, topicPublishInfo, instance,
                    retryTimesWhenSendFailed, times, context, producer);
            return null;
        case SYNC:
            return this.sendMessageSync(addr, brokerName, msg, timeoutMillis, request);
        default:
            assert false;
            break;
    }

    return null;
}
 
Example #18
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
private void sendMessageAsync(//
                              final String addr, //
                              final String brokerName, //
                              final Message msg, //
                              final long timeoutMillis, //
                              final RemotingCommand request, //
                              final SendCallback sendCallback, //
                              final TopicPublishInfo topicPublishInfo, //
                              final MQClientInstance instance, //
                              final int retryTimesWhenSendFailed, //
                              final AtomicInteger times, //
                              final SendMessageContext context, //
                              final DefaultMQProducerImpl producer //
) throws InterruptedException, RemotingException {
    this.remotingClient.invokeAsync(addr, request, timeoutMillis, new InvokeCallback() {
        @Override
        public void operationComplete(ResponseFuture responseFuture) {
            RemotingCommand response = responseFuture.getResponseCommand();
            if (null == sendCallback && response != null) {

                try {
                    SendResult sendResult = MQClientAPIImpl.this.processSendResponse(brokerName, msg, response);
                    if (context != null && sendResult != null) {
                        context.setSendResult(sendResult);
                        context.getProducer().executeSendMessageHookAfter(context);
                    }
                } catch (Throwable e) {
                    //
                }

                producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), false);
                return;
            }

            if (response != null) {
                try {
                    SendResult sendResult = MQClientAPIImpl.this.processSendResponse(brokerName, msg, response);
                    assert sendResult != null;
                    if (context != null) {
                        context.setSendResult(sendResult);
                        context.getProducer().executeSendMessageHookAfter(context);
                    }

                    try {
                        sendCallback.onSuccess(sendResult);
                    } catch (Throwable e) {
                    }

                    producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), false);
                } catch (Exception e) {
                    producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), true);
                    onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance,
                            retryTimesWhenSendFailed, times, e, context, false, producer);
                }
            } else {
                producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), true);
                if (!responseFuture.isSendRequestOK()) {
                    MQClientException ex = new MQClientException("send request failed", responseFuture.getCause());
                    onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance,
                            retryTimesWhenSendFailed, times, ex, context, true, producer);
                } else if (responseFuture.isTimeout()) {
                    MQClientException ex = new MQClientException("wait response timeout " + responseFuture.getTimeoutMillis() + "ms",
                            responseFuture.getCause());
                    onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance,
                            retryTimesWhenSendFailed, times, ex, context, true, producer);
                } else {
                    MQClientException ex = new MQClientException("unknow reseaon", responseFuture.getCause());
                    onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance,
                            retryTimesWhenSendFailed, times, ex, context, true, producer);
                }
            }
        }
    });
}
 
Example #19
Source File: DefaultMQPushConsumerImpl.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
public MQClientInstance getmQClientFactory() {
    return mQClientFactory;
}
 
Example #20
Source File: RemoteBrokerOffsetStore.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public RemoteBrokerOffsetStore(MQClientInstance mQClientFactory, String groupName) {
    this.mQClientFactory = mQClientFactory;
    this.groupName = groupName;
}
 
Example #21
Source File: RebalancePullImpl.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
public RebalancePullImpl(String consumerGroup, MessageModel messageModel,
        AllocateMessageQueueStrategy allocateMessageQueueStrategy, MQClientInstance mQClientFactory,
        DefaultMQPullConsumerImpl defaultMQPullConsumerImpl) {
    super(consumerGroup, messageModel, allocateMessageQueueStrategy, mQClientFactory);
    this.defaultMQPullConsumerImpl = defaultMQPullConsumerImpl;
}
 
Example #22
Source File: DefaultMQProducerImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public MQClientInstance getmQClientFactory() {
    return mQClientFactory;
}
 
Example #23
Source File: PullMessageService.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public PullMessageService(MQClientInstance mQClientFactory) {
    this.mQClientFactory = mQClientFactory;
}
 
Example #24
Source File: RebalancePullImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public RebalancePullImpl(String consumerGroup, MessageModel messageModel,
        AllocateMessageQueueStrategy allocateMessageQueueStrategy, MQClientInstance mQClientFactory,
        DefaultMQPullConsumerImpl defaultMQPullConsumerImpl) {
    super(consumerGroup, messageModel, allocateMessageQueueStrategy, mQClientFactory);
    this.defaultMQPullConsumerImpl = defaultMQPullConsumerImpl;
}
 
Example #25
Source File: DefaultMQPushConsumerImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public MQClientInstance getmQClientFactory() {
    return mQClientFactory;
}
 
Example #26
Source File: DefaultMQPushConsumerImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public void setmQClientFactory(MQClientInstance mQClientFactory) {
    this.mQClientFactory = mQClientFactory;
}
 
Example #27
Source File: RebalanceService.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
public RebalanceService(MQClientInstance mqClientFactory) {
    this.mqClientFactory = mqClientFactory;
}
 
Example #28
Source File: PullMessageService.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
public PullMessageService(MQClientInstance mQClientFactory) {
    this.mQClientFactory = mQClientFactory;
}
 
Example #29
Source File: RebalanceImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public MQClientInstance getmQClientFactory() {
    return mQClientFactory;
}
 
Example #30
Source File: RebalanceImpl.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public void setmQClientFactory(MQClientInstance mQClientFactory) {
    this.mQClientFactory = mQClientFactory;
}