Java Code Examples for org.apache.rocketmq.remoting.protocol.RemotingCommand#addExtField()

The following examples show how to use org.apache.rocketmq.remoting.protocol.RemotingCommand#addExtField() . 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: DefaultRequestProcessorTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessRequest_GetKVConfigReturnNull() throws RemotingCommandException {
    GetKVConfigRequestHeader header = new GetKVConfigRequestHeader();
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.GET_KV_CONFIG,
        header);
    request.addExtField("namespace", "namespace");
    request.addExtField("key", "key");

    RemotingCommand response = defaultRequestProcessor.processRequest(null, request);

    assertThat(response.getCode()).isEqualTo(ResponseCode.QUERY_NOT_FOUND);
    assertThat(response.getRemark()).isEqualTo("No config item, Namespace: namespace Key: key");

    GetKVConfigResponseHeader responseHeader = (GetKVConfigResponseHeader) response
        .readCustomHeader();

    assertThat(responseHeader.getValue()).isNull();
}
 
Example 2
Source File: DefaultRequestProcessorTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessRequest_DeleteKVConfig() throws RemotingCommandException {
    namesrvController.getKvConfigManager().putKVConfig("namespace", "key", "value");

    DeleteKVConfigRequestHeader header = new DeleteKVConfigRequestHeader();
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.DELETE_KV_CONFIG,
        header);
    request.addExtField("namespace", "namespace");
    request.addExtField("key", "key");

    RemotingCommand response = defaultRequestProcessor.processRequest(null, request);

    assertThat(response.getCode()).isEqualTo(ResponseCode.SUCCESS);
    assertThat(response.getRemark()).isNull();

    assertThat(namesrvController.getKvConfigManager().getKVConfig("namespace", "key"))
        .isNull();
}
 
Example 3
Source File: DefaultRequestProcessorTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessRequest_DeleteKVConfig() throws RemotingCommandException {
    namesrvController.getKvConfigManager().putKVConfig("namespace", "key", "value");

    DeleteKVConfigRequestHeader header = new DeleteKVConfigRequestHeader();
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.DELETE_KV_CONFIG,
        header);
    request.addExtField("namespace", "namespace");
    request.addExtField("key", "key");

    RemotingCommand response = defaultRequestProcessor.processRequest(null, request);

    assertThat(response.getCode()).isEqualTo(ResponseCode.SUCCESS);
    assertThat(response.getRemark()).isNull();

    assertThat(namesrvController.getKvConfigManager().getKVConfig("namespace", "key"))
        .isNull();
}
 
Example 4
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 5
Source File: DefaultRequestProcessorTest.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessRequest_GetKVConfigReturnNull() throws RemotingCommandException {
    GetKVConfigRequestHeader header = new GetKVConfigRequestHeader();
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.GET_KV_CONFIG,
        header);
    request.addExtField("namespace", "namespace");
    request.addExtField("key", "key");

    RemotingCommand response = defaultRequestProcessor.processRequest(null, request);

    assertThat(response.getCode()).isEqualTo(ResponseCode.QUERY_NOT_FOUND);
    assertThat(response.getRemark()).isEqualTo("No config item, Namespace: namespace Key: key");

    GetKVConfigResponseHeader responseHeader = (GetKVConfigResponseHeader) response
        .readCustomHeader();

    assertThat(responseHeader.getValue()).isNull();
}
 
Example 6
Source File: DefaultRequestProcessorTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessRequest_DeleteKVConfig() throws RemotingCommandException {
    namesrvController.getKvConfigManager().putKVConfig("namespace", "key", "value");

    DeleteKVConfigRequestHeader header = new DeleteKVConfigRequestHeader();
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.DELETE_KV_CONFIG,
        header);
    request.addExtField("namespace", "namespace");
    request.addExtField("key", "key");

    RemotingCommand response = defaultRequestProcessor.processRequest(null, request);

    assertThat(response.getCode()).isEqualTo(ResponseCode.SUCCESS);
    assertThat(response.getRemark()).isNull();

    assertThat(namesrvController.getKvConfigManager().getKVConfig("namespace", "key"))
        .isNull();
}
 
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: DefaultRequestProcessorTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessRequest_GetKVConfigReturnNotNull() throws RemotingCommandException {
    namesrvController.getKvConfigManager().putKVConfig("namespace", "key", "value");

    GetKVConfigRequestHeader header = new GetKVConfigRequestHeader();
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.GET_KV_CONFIG,
        header);
    request.addExtField("namespace", "namespace");
    request.addExtField("key", "key");

    RemotingCommand response = defaultRequestProcessor.processRequest(null, request);

    assertThat(response.getCode()).isEqualTo(ResponseCode.SUCCESS);
    assertThat(response.getRemark()).isNull();

    GetKVConfigResponseHeader responseHeader = (GetKVConfigResponseHeader) response
        .readCustomHeader();

    assertThat(responseHeader.getValue()).isEqualTo("value");
}
 
Example 9
Source File: DefaultRequestProcessorTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessRequest_GetKVConfigReturnNotNull() throws RemotingCommandException {
    namesrvController.getKvConfigManager().putKVConfig("namespace", "key", "value");

    GetKVConfigRequestHeader header = new GetKVConfigRequestHeader();
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.GET_KV_CONFIG,
        header);
    request.addExtField("namespace", "namespace");
    request.addExtField("key", "key");

    RemotingCommand response = defaultRequestProcessor.processRequest(null, request);

    assertThat(response.getCode()).isEqualTo(ResponseCode.SUCCESS);
    assertThat(response.getRemark()).isNull();

    GetKVConfigResponseHeader responseHeader = (GetKVConfigResponseHeader) response
        .readCustomHeader();

    assertThat(responseHeader.getValue()).isEqualTo("value");
}
 
Example 10
Source File: PullMessageProcessorTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessRequest_SubNotLatest() throws RemotingCommandException {
    final RemotingCommand request = createPullMsgCommand(RequestCode.PULL_MESSAGE);
    request.addExtField("subVersion", String.valueOf(101));
    RemotingCommand response = pullMessageProcessor.processRequest(handlerContext, request);
    assertThat(response).isNotNull();
    assertThat(response.getCode()).isEqualTo(ResponseCode.SUBSCRIPTION_NOT_LATEST);
    assertThat(response.getRemark()).contains("subscription not latest");
}
 
Example 11
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void queryMessage(
    final String addr,
    final QueryMessageRequestHeader requestHeader,
    final long timeoutMillis,
    final InvokeCallback invokeCallback,
    final Boolean isUnqiueKey
) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.QUERY_MESSAGE, requestHeader);
    request.addExtField(MixAll.UNIQUE_MSG_QUERY_FLAG, isUnqiueKey.toString());
    this.remotingClient.invokeAsync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis,
        invokeCallback);
}
 
Example 12
Source File: DefaultRequestProcessorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private static RemotingCommand genSampleRegisterCmd(boolean reg) {
    RegisterBrokerRequestHeader header = new RegisterBrokerRequestHeader();
    header.setBrokerName("broker");
    RemotingCommand request = RemotingCommand.createRequestCommand(
        reg ? RequestCode.REGISTER_BROKER : RequestCode.UNREGISTER_BROKER, header);
    request.addExtField("brokerName", "broker");
    request.addExtField("brokerAddr", "10.10.1.1");
    request.addExtField("clusterName", "cluster");
    request.addExtField("haServerAddr", "10.10.2.1");
    request.addExtField("brokerId", "2333");
    return request;
}
 
Example 13
Source File: PullMessageProcessorTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessRequest_SubNotLatest() throws RemotingCommandException {
    final RemotingCommand request = createPullMsgCommand(RequestCode.PULL_MESSAGE);
    request.addExtField("subVersion", String.valueOf(101));
    RemotingCommand response = pullMessageProcessor.processRequest(handlerContext, request);
    assertThat(response).isNotNull();
    assertThat(response.getCode()).isEqualTo(ResponseCode.SUBSCRIPTION_NOT_LATEST);
    assertThat(response.getRemark()).contains("subscription not latest");
}
 
Example 14
Source File: DefaultRequestProcessorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private static RemotingCommand genSampleRegisterCmd(boolean reg) {
    RegisterBrokerRequestHeader header = new RegisterBrokerRequestHeader();
    header.setBrokerName("broker");
    RemotingCommand request = RemotingCommand.createRequestCommand(
        reg ? RequestCode.REGISTER_BROKER : RequestCode.UNREGISTER_BROKER, header);
    request.addExtField("brokerName", "broker");
    request.addExtField("brokerAddr", "10.10.1.1");
    request.addExtField("clusterName", "cluster");
    request.addExtField("haServerAddr", "10.10.2.1");
    request.addExtField("brokerId", "2333");
    return request;
}
 
Example 15
Source File: PullMessageProcessorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessRequest_SubNotLatest() throws RemotingCommandException {
    final RemotingCommand request = createPullMsgCommand(RequestCode.PULL_MESSAGE);
    request.addExtField("subVersion", String.valueOf(101));
    RemotingCommand response = pullMessageProcessor.processRequest(handlerContext, request);
    assertThat(response).isNotNull();
    assertThat(response.getCode()).isEqualTo(ResponseCode.SUBSCRIPTION_NOT_LATEST);
    assertThat(response.getRemark()).contains("subscription not latest");
}
 
Example 16
Source File: PullMessageProcessorTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessRequest_SubNotLatest() throws RemotingCommandException {
    final RemotingCommand request = createPullMsgCommand(RequestCode.PULL_MESSAGE);
    request.addExtField("subVersion", String.valueOf(101));
    RemotingCommand response = pullMessageProcessor.processRequest(handlerContext, request);
    assertThat(response).isNotNull();
    assertThat(response.getCode()).isEqualTo(ResponseCode.SUBSCRIPTION_NOT_LATEST);
    assertThat(response.getRemark()).contains("subscription not latest");
}
 
Example 17
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void queryMessage(
    final String addr,
    final QueryMessageRequestHeader requestHeader,
    final long timeoutMillis,
    final InvokeCallback invokeCallback,
    final Boolean isUnqiueKey
) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.QUERY_MESSAGE, requestHeader);
    request.addExtField(MixAll.UNIQUE_MSG_QUERY_FLAG, isUnqiueKey.toString());
    this.remotingClient.invokeAsync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis,
        invokeCallback);
}
 
Example 18
Source File: DefaultRequestProcessorTest.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private static RemotingCommand genSampleRegisterCmd(boolean reg) {
    RegisterBrokerRequestHeader header = new RegisterBrokerRequestHeader();
    header.setBrokerName("broker");
    RemotingCommand request = RemotingCommand.createRequestCommand(
        reg ? RequestCode.REGISTER_BROKER : RequestCode.UNREGISTER_BROKER, header);
    request.addExtField("brokerName", "broker");
    request.addExtField("brokerAddr", "10.10.1.1");
    request.addExtField("clusterName", "cluster");
    request.addExtField("haServerAddr", "10.10.2.1");
    request.addExtField("brokerId", "2333");
    return request;
}
 
Example 19
Source File: SendMessageProcessor.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
private RemotingCommand sendBatchMessage(final ChannelHandlerContext ctx,
    final RemotingCommand request,
    final SendMessageContext sendMessageContext,
    final SendMessageRequestHeader requestHeader) throws RemotingCommandException {

    final RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    final SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();

    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 startTimstamp = this.brokerController.getBrokerConfig().getStartAcceptSendRequestTimeStamp();
    if (this.brokerController.getMessageStore().now() < startTimstamp) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("broker unable to service, until %s", UtilAll.timeMillisToHumanString2(startTimstamp)));
        return response;
    }

    response.setCode(-1);
    String[] topics = null;
    if (requestHeader.isMultiTopic()) {
        topics = requestHeader.getTopic().split(MixAll.BATCH_TOPIC_SPLITTER);
        requestHeader.setTopic(topics[0]);
        msgCheckWithMultiTopic(ctx, requestHeader, response, topics);
    } else {
        super.msgCheck(ctx, requestHeader, response);
    }

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

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

    if (queueIdInt < 0) {
        queueIdInt = Math.abs(this.random.nextInt() % 99999999) % 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 response;
    }

    if (requestHeader.getTopic() != null && requestHeader.getTopic().startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
        response.setCode(ResponseCode.MESSAGE_ILLEGAL);
        response.setRemark("batch request does not support retry group " + requestHeader.getTopic());
        return response;
    }
    MessageExtBatch messageExtBatch = new MessageExtBatch();
    messageExtBatch.setTopic(requestHeader.getTopic());
    messageExtBatch.setQueueId(queueIdInt);
    if (topics != null) {
        messageExtBatch.setMultiTopic(true);
        messageExtBatch.setTopics(topics);
    }

    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());

    PutMessageResult putMessageResult = this.brokerController.getMessageStore().putMessages(messageExtBatch);

    return handlePutMessageResult(putMessageResult, response, request, messageExtBatch, responseHeader, sendMessageContext, ctx, queueIdInt);
}
 
Example 20
Source File: SendMessageProcessor.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
private RemotingCommand sendBatchMessage(final ChannelHandlerContext ctx, //
                                    final RemotingCommand request, //
                                    final SendMessageContext sendMessageContext, //
                                    final SendMessageRequestHeader requestHeader) throws RemotingCommandException {

    final RemotingCommand response = RemotingCommand.createResponseCommand(SendMessageResponseHeader.class);
    final SendMessageResponseHeader responseHeader = (SendMessageResponseHeader) response.readCustomHeader();


    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 startTimstamp = this.brokerController.getBrokerConfig().getStartAcceptSendRequestTimeStamp();
    if (this.brokerController.getMessageStore().now() < startTimstamp) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("broker unable to service, until %s", UtilAll.timeMillisToHumanString2(startTimstamp)));
        return response;
    }

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


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

    if (queueIdInt < 0) {
        queueIdInt = Math.abs(this.random.nextInt() % 99999999) % 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 response;
    }

    if (requestHeader.getTopic() != null && requestHeader.getTopic().startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
        response.setCode(ResponseCode.MESSAGE_ILLEGAL);
        response.setRemark("batch request does not support retry group "  + requestHeader.getTopic());
        return 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());

    PutMessageResult putMessageResult = this.brokerController.getMessageStore().putMessages(messageExtBatch);

    handlePutMessageResult(putMessageResult, response, request, messageExtBatch, responseHeader, sendMessageContext, ctx, queueIdInt);
    return response;
}