org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader Java Examples

The following examples show how to use org.apache.rocketmq.common.protocol.header.SendMessageResponseHeader. 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: MQClientAPIImplTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());

    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);

    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
 
Example #2
Source File: MQClientAPIImplTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMessageSync_WithException() throws InterruptedException, RemotingException, MQBrokerException {
    doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            RemotingCommand request = mock.getArgument(1);
            RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setOpaque(request.getOpaque());
            response.setRemark("Broker is broken.");
            return response;
        }
    }).when(remotingClient).invokeSync(anyString(), any(RemotingCommand.class), anyLong());

    SendMessageRequestHeader requestHeader = createSendMessageRequestHeader();

    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, requestHeader,
            3 * 1000, CommunicationMode.SYNC, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(MQBrokerException.class);
    } catch (MQBrokerException e) {
        assertThat(e).hasMessageContaining("Broker is broken.");
    }
}
 
Example #3
Source File: MQClientAPIImplTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Test
public void testResumeCheckHalfMessage_WithException() throws RemotingException, InterruptedException, MQBrokerException, MQClientException {
    doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            RemotingCommand request = mock.getArgument(1);
            RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setOpaque(request.getOpaque());
            response.setRemark("Put message back to RMQ_SYS_TRANS_HALF_TOPIC failed.");
            return response;
        }
    }).when(remotingClient).invokeSync(anyString(), any(RemotingCommand.class), anyLong());

    boolean result = mqClientAPI.resumeCheckHalfMessage(brokerAddr, "test", 3000);
    assertThat(result).isEqualTo(false);
}
 
Example #4
Source File: AbstractSendMessageProcessor.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
public void executeSendMessageHookAfter(final RemotingCommand response, final SendMessageContext context) {
    if (hasSendMessageHook()) {
        for (SendMessageHook hook : this.sendMessageHookList) {
            try {
                if (response != null) {
                    final SendMessageResponseHeader responseHeader =
                        (SendMessageResponseHeader) response.readCustomHeader();
                    context.setMsgId(responseHeader.getMsgId());
                    context.setQueueId(responseHeader.getQueueId());
                    context.setQueueOffset(responseHeader.getQueueOffset());
                    context.setCode(response.getCode());
                    context.setErrorMsg(response.getRemark());
                }
                hook.sendMessageAfter(context);
            } catch (Throwable e) {
                // Ignore
            }
        }
    }
}
 
Example #5
Source File: MQClientAPIImplTest.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());

    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);

    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
 
Example #6
Source File: MQClientAPIImplTest.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMessageSync_WithException() throws InterruptedException, RemotingException, MQBrokerException {
    doAnswer(new Answer() {
        @Override public Object answer(InvocationOnMock mock) throws Throwable {
            RemotingCommand request = mock.getArgument(1);
            RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setOpaque(request.getOpaque());
            response.setRemark("Broker is broken.");
            return response;
        }
    }).when(remotingClient).invokeSync(anyString(), any(RemotingCommand.class), anyLong());

    SendMessageRequestHeader requestHeader = createSendMessageRequestHeader();

    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, requestHeader,
            3 * 1000, CommunicationMode.SYNC, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(MQBrokerException.class);
    } catch (MQBrokerException e) {
        assertThat(e).hasMessageContaining("Broker is broken.");
    }
}
 
Example #7
Source File: MQClientAPIImplTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());

    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);

    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
 
Example #8
Source File: AbstractSendMessageProcessor.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
public void executeSendMessageHookAfter(final RemotingCommand response, final SendMessageContext context) {
    if (hasSendMessageHook()) {
        for (SendMessageHook hook : this.sendMessageHookList) {
            try {
                if (response != null) {
                    final SendMessageResponseHeader responseHeader =
                        (SendMessageResponseHeader) response.readCustomHeader();
                    context.setMsgId(responseHeader.getMsgId());
                    context.setQueueId(responseHeader.getQueueId());
                    context.setQueueOffset(responseHeader.getQueueOffset());
                    context.setCode(response.getCode());
                    context.setErrorMsg(response.getRemark());
                }
                hook.sendMessageAfter(context);
            } catch (Throwable e) {
                // Ignore
            }
        }
    }
}
 
Example #9
Source File: MQClientAPIImplTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());

    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);

    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
 
Example #10
Source File: MQClientAPIImplTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMessageSync_WithException() throws InterruptedException, RemotingException, MQBrokerException {
    doAnswer(new Answer() {
        @Override public Object answer(InvocationOnMock mock) throws Throwable {
            RemotingCommand request = mock.getArgument(1);
            RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setOpaque(request.getOpaque());
            response.setRemark("Broker is broken.");
            return response;
        }
    }).when(remotingClient).invokeSync(anyString(), any(RemotingCommand.class), anyLong());

    SendMessageRequestHeader requestHeader = createSendMessageRequestHeader();

    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, requestHeader,
            3 * 1000, CommunicationMode.SYNC, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(MQBrokerException.class);
    } catch (MQBrokerException e) {
        assertThat(e).hasMessageContaining("Broker is broken.");
    }
}
 
Example #11
Source File: AbstractSendMessageProcessor.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public void executeSendMessageHookAfter(final RemotingCommand response, final SendMessageContext context) {
    if (hasSendMessageHook()) {
        for (SendMessageHook hook : this.sendMessageHookList) {
            try {
                if (response != null) {
                    final SendMessageResponseHeader responseHeader =
                        (SendMessageResponseHeader) response.readCustomHeader();
                    context.setMsgId(responseHeader.getMsgId());
                    context.setQueueId(responseHeader.getQueueId());
                    context.setQueueOffset(responseHeader.getQueueOffset());
                    context.setCode(response.getCode());
                    context.setErrorMsg(response.getRemark());
                }
                hook.sendMessageAfter(context);
            } catch (Throwable e) {
                // Ignore
            }
        }
    }
}
 
Example #12
Source File: AbstractSendMessageProcessor.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public void executeSendMessageHookAfter(final RemotingCommand response, final SendMessageContext context) {
    if (hasSendMessageHook()) {
        for (SendMessageHook hook : this.sendMessageHookList) {
            try {
                if (response != null) {
                    final SendMessageResponseHeader responseHeader =
                        (SendMessageResponseHeader) response.readCustomHeader();
                    context.setMsgId(responseHeader.getMsgId());
                    context.setQueueId(responseHeader.getQueueId());
                    context.setQueueOffset(responseHeader.getQueueOffset());
                    context.setCode(response.getCode());
                    context.setErrorMsg(response.getRemark());
                }
                hook.sendMessageAfter(context);
            } catch (Throwable e) {
                // Ignore
            }
        }
    }
}
 
Example #13
Source File: MQClientAPIImplTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());

    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);

    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
 
Example #14
Source File: MQClientAPIImplTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMessageSync_WithException() throws InterruptedException, RemotingException, MQBrokerException {
    doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            RemotingCommand request = mock.getArgument(1);
            RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setOpaque(request.getOpaque());
            response.setRemark("Broker is broken.");
            return response;
        }
    }).when(remotingClient).invokeSync(anyString(), any(RemotingCommand.class), anyLong());

    SendMessageRequestHeader requestHeader = createSendMessageRequestHeader();

    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, requestHeader,
            3 * 1000, CommunicationMode.SYNC, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(MQBrokerException.class);
    } catch (MQBrokerException e) {
        assertThat(e).hasMessageContaining("Broker is broken.");
    }
}
 
Example #15
Source File: AbstractSendMessageProcessor.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public void executeSendMessageHookAfter(final RemotingCommand response, final SendMessageContext context) {
    if (hasSendMessageHook()) {
        for (SendMessageHook hook : this.sendMessageHookList) {
            try {
                if (response != null) {
                    final SendMessageResponseHeader responseHeader =
                        (SendMessageResponseHeader)response.readCustomHeader();
                    context.setMsgId(responseHeader.getMsgId());
                    context.setQueueId(responseHeader.getQueueId());
                    context.setQueueOffset(responseHeader.getQueueOffset());
                    context.setCode(response.getCode());
                    context.setErrorMsg(response.getRemark());
                }
                hook.sendMessageAfter(context);
            } catch (Throwable e) {
                // Ignore
            }
        }
    }
}
 
Example #16
Source File: MQClientAPIImplTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMessageSync_WithException() throws InterruptedException, RemotingException, MQBrokerException {
    doAnswer(new Answer() {
        @Override public Object answer(InvocationOnMock mock) throws Throwable {
            RemotingCommand request = mock.getArgument(1);
            RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setOpaque(request.getOpaque());
            response.setRemark("Broker is broken.");
            return response;
        }
    }).when(remotingClient).invokeSync(anyString(), any(RemotingCommand.class), anyLong());

    SendMessageRequestHeader requestHeader = createSendMessageRequestHeader();

    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, requestHeader,
            3 * 1000, CommunicationMode.SYNC, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(MQBrokerException.class);
    } catch (MQBrokerException e) {
        assertThat(e).hasMessageContaining("Broker is broken.");
    }
}
 
Example #17
Source File: MQClientAPIImplTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());

    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);

    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
 
Example #18
Source File: AbstractSendMessageProcessor.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * 执行发送Message的After勾子
 * @param response response
 * @param context 上下文
 */
public void executeSendMessageHookAfter(final RemotingCommand response, final SendMessageContext context) {
    if (hasSendMessageHook()) {
        for (SendMessageHook hook : this.sendMessageHookList) {
            try {
                if (response != null) {
                    final SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
                    context.setMsgId(responseHeader.getMsgId());
                    context.setQueueId(responseHeader.getQueueId());
                    context.setQueueOffset(responseHeader.getQueueOffset());
                    context.setCode(response.getCode());
                    context.setErrorMsg(response.getRemark());
                }
                hook.sendMessageAfter(context);
            } catch (Throwable e) {
                // Ignore
            }
        }
    }
}
 
Example #19
Source File: MQClientAPIImplTest.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());

    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);

    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
 
Example #20
Source File: MQClientAPIImplTest.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMessageSync_WithException() throws InterruptedException, RemotingException, MQBrokerException {
    doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            RemotingCommand request = mock.getArgument(1);
            RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setOpaque(request.getOpaque());
            response.setRemark("Broker is broken.");
            return response;
        }
    }).when(remotingClient).invokeSync(anyString(), any(RemotingCommand.class), anyLong());

    SendMessageRequestHeader requestHeader = createSendMessageRequestHeader();

    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, requestHeader,
            3 * 1000, CommunicationMode.SYNC, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(MQBrokerException.class);
    } catch (MQBrokerException e) {
        assertThat(e).hasMessageContaining("Broker is broken.");
    }
}
 
Example #21
Source File: DeFiBusClientAPIImplTest.java    From DeFiBus with Apache License 2.0 6 votes vote down vote up
private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());

    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);

    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
 
Example #22
Source File: AbstractSendMessageProcessor.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public void executeSendMessageHookAfter(final RemotingCommand response, final SendMessageContext context) {
    if (hasSendMessageHook()) {
        for (SendMessageHook hook : this.sendMessageHookList) {
            try {
                if (response != null) {
                    final SendMessageResponseHeader responseHeader =
                        (SendMessageResponseHeader) response.readCustomHeader();
                    context.setMsgId(responseHeader.getMsgId());
                    context.setQueueId(responseHeader.getQueueId());
                    context.setQueueOffset(responseHeader.getQueueOffset());
                    context.setCode(response.getCode());
                    context.setErrorMsg(response.getRemark());
                }
                hook.sendMessageAfter(context);
            } catch (Throwable e) {
                // Ignore
            }
        }
    }
}
 
Example #23
Source File: MQClientAPIImplTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMessageSync_WithException() throws InterruptedException, RemotingException, MQBrokerException {
    doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            RemotingCommand request = mock.getArgument(1);
            RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setOpaque(request.getOpaque());
            response.setRemark("Broker is broken.");
            return response;
        }
    }).when(remotingClient).invokeSync(anyString(), any(RemotingCommand.class), anyLong());

    SendMessageRequestHeader requestHeader = createSendMessageRequestHeader();

    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, requestHeader,
            3 * 1000, CommunicationMode.SYNC, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(MQBrokerException.class);
    } catch (MQBrokerException e) {
        assertThat(e).hasMessageContaining("Broker is broken.");
    }
}
 
Example #24
Source File: AbstractSendMessageProcessor.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
public void executeSendMessageHookAfter(final RemotingCommand response, final SendMessageContext context) {
    if (hasSendMessageHook()) {
        for (SendMessageHook hook : this.sendMessageHookList) {
            try {
                if (response != null) {
                    final SendMessageResponseHeader responseHeader =
                        (SendMessageResponseHeader) response.readCustomHeader();
                    context.setMsgId(responseHeader.getMsgId());
                    context.setQueueId(responseHeader.getQueueId());
                    context.setQueueOffset(responseHeader.getQueueOffset());
                    context.setCode(response.getCode());
                    context.setErrorMsg(response.getRemark());
                }
                hook.sendMessageAfter(context);
            } catch (Throwable e) {
                // Ignore
            }
        }
    }
}
 
Example #25
Source File: MQClientAPIImplTest.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
private RemotingCommand createSuccessResponse(RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    response.setCode(ResponseCode.SUCCESS);
    response.setOpaque(request.getOpaque());

    SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();
    responseHeader.setMsgId("123");
    responseHeader.setQueueId(1);
    responseHeader.setQueueOffset(123L);

    response.addExtField(MessageConst.PROPERTY_MSG_REGION, "RegionHZ");
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, "true");
    response.addExtField("queueId", String.valueOf(responseHeader.getQueueId()));
    response.addExtField("msgId", responseHeader.getMsgId());
    response.addExtField("queueOffset", String.valueOf(responseHeader.getQueueOffset()));
    return response;
}
 
Example #26
Source File: MQClientAPIImplTest.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMessageSync_WithException() throws InterruptedException, RemotingException, MQBrokerException {
    doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            RemotingCommand request = mock.getArgument(1);
            RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
            response.setCode(ResponseCode.SYSTEM_ERROR);
            response.setOpaque(request.getOpaque());
            response.setRemark("Broker is broken.");
            return response;
        }
    }).when(remotingClient).invokeSync(anyString(), any(RemotingCommand.class), anyLong());

    SendMessageRequestHeader requestHeader = createSendMessageRequestHeader();

    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, requestHeader,
            3 * 1000, CommunicationMode.SYNC, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(MQBrokerException.class);
    } catch (MQBrokerException e) {
        assertThat(e).hasMessageContaining("Broker is broken.");
    }
}
 
Example #27
Source File: SendMessageProcessor.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<RemotingCommand> handlePutMessageResultFuture(CompletableFuture<PutMessageResult> putMessageResult,
                                                                        RemotingCommand response,
                                                                        RemotingCommand request,
                                                                        MessageExt msgInner,
                                                                        SendMessageResponseHeader responseHeader,
                                                                        SendMessageContext sendMessageContext,
                                                                        ChannelHandlerContext ctx,
                                                                        int queueIdInt) {
    return putMessageResult.thenApply((r) ->
        handlePutMessageResult(r, response, request, msgInner, responseHeader, sendMessageContext, ctx, queueIdInt)
    );
}
 
Example #28
Source File: ReplyMessageProcessor.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private void handlePushReplyResult(PushReplyResult pushReplyResult, final RemotingCommand response,
    final SendMessageResponseHeader responseHeader, int queueIdInt) {

    if (!pushReplyResult.isPushOk()) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(pushReplyResult.getRemark());
    } else {
        response.setCode(ResponseCode.SUCCESS);
        response.setRemark(null);
        //set to zore to avoid client decoding exception
        responseHeader.setMsgId("0");
        responseHeader.setQueueId(queueIdInt);
        responseHeader.setQueueOffset(0L);
    }
}
 
Example #29
Source File: SendMessageProcessor.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private RemotingCommand preSend(ChannelHandlerContext ctx, RemotingCommand request,
                                SendMessageRequestHeader requestHeader) {
    final RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);

    response.setOpaque(request.getOpaque());

    response.addExtField(MessageConst.PROPERTY_MSG_REGION, this.brokerController.getBrokerConfig().getRegionId());
    response.addExtField(MessageConst.PROPERTY_TRACE_SWITCH, String.valueOf(this.brokerController.getBrokerConfig().isTraceOn()));

    log.debug("Receive SendMessage request command {}", request);

    final long startTimestamp = this.brokerController.getBrokerConfig().getStartAcceptSendRequestTimeStamp();

    if (this.brokerController.getMessageStore().now() < startTimestamp) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("broker unable to service, until %s", UtilAll.timeMillisToHumanString2(startTimestamp)));
        return response;
    }

    response.setCode(-1);
    super.msgCheck(ctx, requestHeader, response);
    if (response.getCode() != -1) {
        return response;
    }

    return response;
}
 
Example #30
Source File: SendMessageProcessor.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
private CompletableFuture<RemotingCommand> asyncSendBatchMessage(ChannelHandlerContext ctx, RemotingCommand request,
                                                                 SendMessageContext mqtraceContext,
                                                                 SendMessageRequestHeader requestHeader) {
    final RemotingCommand response = preSend(ctx, request, requestHeader);
    final SendMessageResponseHeader responseHeader = (SendMessageResponseHeader)response.readCustomHeader();

    if (response.getCode() != -1) {
        return CompletableFuture.completedFuture(response);
    }

    int queueIdInt = requestHeader.getQueueId();
    TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(requestHeader.getTopic());

    if (queueIdInt < 0) {
        queueIdInt = randomQueueId(topicConfig.getWriteQueueNums());
    }

    if (requestHeader.getTopic().length() > Byte.MAX_VALUE) {
        response.setCode(ResponseCode.MESSAGE_ILLEGAL);
        response.setRemark("message topic length too long " + requestHeader.getTopic().length());
        return CompletableFuture.completedFuture(response);
    }

    MessageExtBatch messageExtBatch = new MessageExtBatch();
    messageExtBatch.setTopic(requestHeader.getTopic());
    messageExtBatch.setQueueId(queueIdInt);

    int sysFlag = requestHeader.getSysFlag();
    if (TopicFilterType.MULTI_TAG == topicConfig.getTopicFilterType()) {
        sysFlag |= MessageSysFlag.MULTI_TAGS_FLAG;
    }
    messageExtBatch.setSysFlag(sysFlag);

    messageExtBatch.setFlag(requestHeader.getFlag());
    MessageAccessor.setProperties(messageExtBatch, MessageDecoder.string2messageProperties(requestHeader.getProperties()));
    messageExtBatch.setBody(request.getBody());
    messageExtBatch.setBornTimestamp(requestHeader.getBornTimestamp());
    messageExtBatch.setBornHost(ctx.channel().remoteAddress());
    messageExtBatch.setStoreHost(this.getStoreHost());
    messageExtBatch.setReconsumeTimes(requestHeader.getReconsumeTimes() == null ? 0 : requestHeader.getReconsumeTimes());
    String clusterName = this.brokerController.getBrokerConfig().getBrokerClusterName();
    MessageAccessor.putProperty(messageExtBatch, MessageConst.PROPERTY_CLUSTER, clusterName);

    CompletableFuture<PutMessageResult> putMessageResult = this.brokerController.getMessageStore().asyncPutMessages(messageExtBatch);
    return handlePutMessageResultFuture(putMessageResult, response, request, messageExtBatch, responseHeader, mqtraceContext, ctx, queueIdInt);
}