Search in sources :

Example 1 with ResponseFuture

use of org.apache.rocketmq.remoting.netty.ResponseFuture in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImplTest method testSendMessageAsync_Success.

@Test
public void testSendMessageAsync_Success() throws RemotingException, InterruptedException, MQBrokerException {
    doNothing().when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    SendResult sendResult = mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, new SendMessageContext(), defaultMQProducerImpl);
    assertThat(sendResult).isNull();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            InvokeCallback callback = mock.getArgument(3);
            RemotingCommand request = mock.getArgument(1);
            ResponseFuture responseFuture = new ResponseFuture(request.getOpaque(), 3 * 1000, null, null);
            responseFuture.setResponseCommand(createSuccessResponse(request));
            callback.operationComplete(responseFuture);
            return null;
        }
    }).when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    SendMessageContext sendMessageContext = new SendMessageContext();
    sendMessageContext.setProducer(new DefaultMQProducerImpl(new DefaultMQProducer()));
    mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
            assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
            assertThat(sendResult.getOffsetMsgId()).isEqualTo("123");
            assertThat(sendResult.getQueueOffset()).isEqualTo(123L);
            assertThat(sendResult.getMessageQueue().getQueueId()).isEqualTo(1);
        }

        @Override
        public void onException(Throwable e) {
        }
    }, null, null, 0, sendMessageContext, defaultMQProducerImpl);
}
Also used : InvokeCallback(org.apache.rocketmq.remoting.InvokeCallback) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) ResponseFuture(org.apache.rocketmq.remoting.netty.ResponseFuture) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SendResult(org.apache.rocketmq.client.producer.SendResult) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) DefaultMQProducerImpl(org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl) SendCallback(org.apache.rocketmq.client.producer.SendCallback) Test(org.junit.Test)

Example 2 with ResponseFuture

use of org.apache.rocketmq.remoting.netty.ResponseFuture in project rocketmq by apache.

the class MQClientAPIImplTest method testSendMessageAsync_Success.

@Test
public void testSendMessageAsync_Success() throws RemotingException, InterruptedException, MQBrokerException {
    doNothing().when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    SendResult sendResult = mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, new SendMessageContext(), defaultMQProducerImpl);
    assertThat(sendResult).isNull();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            InvokeCallback callback = mock.getArgument(3);
            RemotingCommand request = mock.getArgument(1);
            ResponseFuture responseFuture = new ResponseFuture(request.getOpaque(), 3 * 1000, null, null);
            responseFuture.setResponseCommand(createSuccessResponse(request));
            callback.operationComplete(responseFuture);
            return null;
        }
    }).when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    SendMessageContext sendMessageContext = new SendMessageContext();
    sendMessageContext.setProducer(new DefaultMQProducerImpl(new DefaultMQProducer()));
    mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
            assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
            assertThat(sendResult.getOffsetMsgId()).isEqualTo("123");
            assertThat(sendResult.getQueueOffset()).isEqualTo(123L);
            assertThat(sendResult.getMessageQueue().getQueueId()).isEqualTo(1);
        }

        @Override
        public void onException(Throwable e) {
        }
    }, null, null, 0, sendMessageContext, defaultMQProducerImpl);
}
Also used : InvokeCallback(org.apache.rocketmq.remoting.InvokeCallback) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) ResponseFuture(org.apache.rocketmq.remoting.netty.ResponseFuture) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SendResult(org.apache.rocketmq.client.producer.SendResult) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) DefaultMQProducerImpl(org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl) SendCallback(org.apache.rocketmq.client.producer.SendCallback) Test(org.junit.Test)

Example 3 with ResponseFuture

use of org.apache.rocketmq.remoting.netty.ResponseFuture in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class RequestHeader method testInvokeAsync.

@Test
public void testInvokeAsync() throws InterruptedException, RemotingConnectException, RemotingTimeoutException, RemotingTooMuchRequestException, RemotingSendRequestException {
    final CountDownLatch latch = new CountDownLatch(1);
    RemotingCommand request = RemotingCommand.createRequestCommand(0, null);
    request.setRemark("messi");
    remotingClient.invokeAsync("localhost:8888", request, 1000 * 3, new InvokeCallback() {

        @Override
        public void operationComplete(ResponseFuture responseFuture) {
            latch.countDown();
            assertTrue(responseFuture != null);
            assertThat(responseFuture.getResponseCommand().getLanguage()).isEqualTo(LanguageCode.JAVA);
            assertThat(responseFuture.getResponseCommand().getExtFields()).hasSize(2);
        }
    });
    latch.await();
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ResponseFuture(org.apache.rocketmq.remoting.netty.ResponseFuture) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with ResponseFuture

use of org.apache.rocketmq.remoting.netty.ResponseFuture in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQAdminImpl method queryMessage.

/**
 * 通过断点查看rocketmq-console项目查询MessageId我们可以发现,只有offsetMsgId才是真正的这种规则的key,而msgId其实仅仅知识生成的UNIQ_KEY
 * 这也是为什么用MessageDecoder.decodeMessageId端口号超限问题的原因
 */
protected QueryResult queryMessage(String topic, String key, int maxNum, long begin, long end, boolean isUniqKey) throws MQClientException, InterruptedException {
    TopicRouteData topicRouteData = this.mQClientFactory.getAnExistTopicRouteData(topic);
    if (null == topicRouteData) {
        this.mQClientFactory.updateTopicRouteInfoFromNameServer(topic);
        topicRouteData = this.mQClientFactory.getAnExistTopicRouteData(topic);
    }
    if (topicRouteData != null) {
        List<String> brokerAddrs = new LinkedList<String>();
        for (BrokerData brokerData : topicRouteData.getBrokerDatas()) {
            String addr = brokerData.selectBrokerAddr();
            if (addr != null) {
                brokerAddrs.add(addr);
            }
        }
        if (!brokerAddrs.isEmpty()) {
            final CountDownLatch countDownLatch = new CountDownLatch(brokerAddrs.size());
            final List<QueryResult> queryResultList = new LinkedList<QueryResult>();
            final ReadWriteLock lock = new ReentrantReadWriteLock(false);
            for (String addr : brokerAddrs) {
                try {
                    // 构建查询请求头信息
                    QueryMessageRequestHeader requestHeader = new QueryMessageRequestHeader();
                    requestHeader.setTopic(topic);
                    requestHeader.setKey(key);
                    requestHeader.setMaxNum(maxNum);
                    requestHeader.setBeginTimestamp(begin);
                    requestHeader.setEndTimestamp(end);
                    this.mQClientFactory.getMQClientAPIImpl().queryMessage(addr, requestHeader, timeoutMillis * 3, new InvokeCallback() {

                        @Override
                        public void operationComplete(ResponseFuture responseFuture) {
                            try {
                                RemotingCommand response = responseFuture.getResponseCommand();
                                if (response != null) {
                                    switch(response.getCode()) {
                                        case ResponseCode.SUCCESS:
                                            {
                                                QueryMessageResponseHeader responseHeader = null;
                                                try {
                                                    responseHeader = (QueryMessageResponseHeader) response.decodeCommandCustomHeader(QueryMessageResponseHeader.class);
                                                } catch (RemotingCommandException e) {
                                                    log.error("decodeCommandCustomHeader exception", e);
                                                    return;
                                                }
                                                List<MessageExt> wrappers = MessageDecoder.decodes(ByteBuffer.wrap(response.getBody()), true);
                                                QueryResult qr = new QueryResult(responseHeader.getIndexLastUpdateTimestamp(), wrappers);
                                                try {
                                                    lock.writeLock().lock();
                                                    queryResultList.add(qr);
                                                } finally {
                                                    lock.writeLock().unlock();
                                                }
                                                break;
                                            }
                                        default:
                                            log.warn("getResponseCommand failed, {} {}", response.getCode(), response.getRemark());
                                            break;
                                    }
                                } else {
                                    log.warn("getResponseCommand return null");
                                }
                            } finally {
                                countDownLatch.countDown();
                            }
                        }
                    }, isUniqKey);
                } catch (Exception e) {
                    log.warn("queryMessage exception", e);
                }
            }
            boolean ok = countDownLatch.await(timeoutMillis * 4, TimeUnit.MILLISECONDS);
            if (!ok) {
                log.warn("queryMessage, maybe some broker failed");
            }
            long indexLastUpdateTimestamp = 0;
            List<MessageExt> messageList = new LinkedList<MessageExt>();
            for (QueryResult qr : queryResultList) {
                if (qr.getIndexLastUpdateTimestamp() > indexLastUpdateTimestamp) {
                    indexLastUpdateTimestamp = qr.getIndexLastUpdateTimestamp();
                }
                for (MessageExt msgExt : qr.getMessageList()) {
                    if (isUniqKey) {
                        if (msgExt.getMsgId().equals(key)) {
                            if (messageList.size() > 0) {
                                if (messageList.get(0).getStoreTimestamp() > msgExt.getStoreTimestamp()) {
                                    messageList.clear();
                                    messageList.add(msgExt);
                                }
                            } else {
                                messageList.add(msgExt);
                            }
                        } else {
                            log.warn("queryMessage by uniqKey, find message key not matched, maybe hash duplicate {}", msgExt.toString());
                        }
                    } else {
                        String keys = msgExt.getKeys();
                        if (keys != null) {
                            boolean matched = false;
                            String[] keyArray = keys.split(MessageConst.KEY_SEPARATOR);
                            if (keyArray != null) {
                                for (String k : keyArray) {
                                    if (key.equals(k)) {
                                        matched = true;
                                        break;
                                    }
                                }
                            }
                            if (matched) {
                                messageList.add(msgExt);
                            } else {
                                log.warn("queryMessage, find message key not matched, maybe hash duplicate {}", msgExt.toString());
                            }
                        }
                    }
                }
            }
            if (!messageList.isEmpty()) {
                return new QueryResult(indexLastUpdateTimestamp, messageList);
            } else {
                throw new MQClientException(ResponseCode.NO_MESSAGE, "query message by key finished, but no message.");
            }
        }
    }
    throw new MQClientException(ResponseCode.TOPIC_NOT_EXIST, "The topic[" + topic + "] not matched route info");
}
Also used : InvokeCallback(org.apache.rocketmq.remoting.InvokeCallback) QueryMessageRequestHeader(org.apache.rocketmq.common.protocol.header.QueryMessageRequestHeader) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) ResponseFuture(org.apache.rocketmq.remoting.netty.ResponseFuture) CountDownLatch(java.util.concurrent.CountDownLatch) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) LinkedList(java.util.LinkedList) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) MessageExt(org.apache.rocketmq.common.message.MessageExt) QueryResult(org.apache.rocketmq.client.QueryResult) QueryMessageResponseHeader(org.apache.rocketmq.common.protocol.header.QueryMessageResponseHeader) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 5 with ResponseFuture

use of org.apache.rocketmq.remoting.netty.ResponseFuture in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImpl method sendMessageAsync.

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);
                }
            }
        }
    });
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) InvokeCallback(org.apache.rocketmq.remoting.InvokeCallback) SendResult(org.apache.rocketmq.client.producer.SendResult) ResponseFuture(org.apache.rocketmq.remoting.netty.ResponseFuture) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingTooMuchRequestException(org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Aggregations

ResponseFuture (org.apache.rocketmq.remoting.netty.ResponseFuture)8 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)8 InvokeCallback (org.apache.rocketmq.remoting.InvokeCallback)6 CountDownLatch (java.util.concurrent.CountDownLatch)4 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)4 MQClientException (org.apache.rocketmq.client.exception.MQClientException)4 SendResult (org.apache.rocketmq.client.producer.SendResult)4 RemotingCommandException (org.apache.rocketmq.remoting.exception.RemotingCommandException)4 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)4 Test (org.junit.Test)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 LinkedList (java.util.LinkedList)2 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)2 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)2 QueryResult (org.apache.rocketmq.client.QueryResult)2 SendMessageContext (org.apache.rocketmq.client.hook.SendMessageContext)2 DefaultMQProducerImpl (org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl)2 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)2 SendCallback (org.apache.rocketmq.client.producer.SendCallback)2 MessageExt (org.apache.rocketmq.common.message.MessageExt)2