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

The following examples show how to use org.apache.rocketmq.remoting.protocol.RemotingCommand#createRequestCommand() . 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_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 2
Source File: Broker2Client.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * 通知消费者消费id有变化
 */
public void notifyConsumerIdsChanged(
    final Channel channel,
    final String consumerGroup) {

    if (null == consumerGroup) {
        log.error("notifyConsumerIdsChanged consumerGroup is null");
        return;
    }

    NotifyConsumerIdsChangedRequestHeader requestHeader = new NotifyConsumerIdsChangedRequestHeader();
    requestHeader.setConsumerGroup(consumerGroup);
    RemotingCommand request =
        RemotingCommand.createRequestCommand(RequestCode.NOTIFY_CONSUMER_IDS_CHANGED, requestHeader);

    try {
        this.brokerController.getRemotingServer().invokeOneway(channel, request, 10);
    } catch (Exception e) {
        log.error("notifyConsumerIdsChanged exception, " + consumerGroup, e.getMessage());
    }
}
 
Example 3
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public GroupList queryTopicConsumeByWho(final String addr, final String topic, final long timeoutMillis)
    throws RemotingConnectException, RemotingSendRequestException, RemotingTimeoutException, InterruptedException,
    MQBrokerException {
    QueryTopicConsumeByWhoRequestHeader requestHeader = new QueryTopicConsumeByWhoRequestHeader();
    requestHeader.setTopic(topic);

    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.QUERY_TOPIC_CONSUME_BY_WHO, requestHeader);

    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr),
        request, timeoutMillis);
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            GroupList groupList = GroupList.decode(response.getBody(), GroupList.class);
            return groupList;
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example 4
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public void unlockBatchMQ(//
                          final String addr, //
                          final UnlockBatchRequestBody requestBody, //
                          final long timeoutMillis, //
                          final boolean oneway//
) throws RemotingException, MQBrokerException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UNLOCK_BATCH_MQ, null);

    request.setBody(requestBody.encode());

    if (oneway) {
        this.remotingClient.invokeOneway(addr, request, timeoutMillis);
    } else {
        RemotingCommand response = this.remotingClient
            .invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
        switch (response.getCode()) {
            case ResponseCode.SUCCESS: {
                return;
            }
            default:
                break;
        }

        throw new MQBrokerException(response.getCode(), response.getRemark());
    }
}
 
Example 5
Source File: DefaultRequestProcessorTest.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessRequest_PutKVConfig() throws RemotingCommandException {
    PutKVConfigRequestHeader header = new PutKVConfigRequestHeader();
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.PUT_KV_CONFIG,
        header);
    request.addExtField("namespace", "namespace");
    request.addExtField("key", "key");
    request.addExtField("value", "value");

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

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

    assertThat(namesrvController.getKvConfigManager().getKVConfig("namespace", "key"))
        .isEqualTo("value");
}
 
Example 6
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 7
Source File: Broker2Client.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
/**
 * 通知指定的生产者或者消费者同组的客户端发生变化
 *
 * @param channel
 * @param consumerGroup
 */
public void notifyConsumerIdsChanged(
    final Channel channel,
    final String consumerGroup) {
    if (null == consumerGroup) {
        log.error("notifyConsumerIdsChanged consumerGroup is null");
        return;
    }

    NotifyConsumerIdsChangedRequestHeader requestHeader = new NotifyConsumerIdsChangedRequestHeader();
    requestHeader.setConsumerGroup(consumerGroup);
    RemotingCommand request =
        RemotingCommand.createRequestCommand(RequestCode.NOTIFY_CONSUMER_IDS_CHANGED, requestHeader);

    try {
        this.brokerController.getRemotingServer().invokeOneway(channel, request, 10);
    } catch (Exception e) {
        log.error("notifyConsumerIdsChanged exception, " + consumerGroup, e.getMessage());
    }
}
 
Example 8
Source File: DefaultRequestProcessorTest.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessRequest_PutKVConfig() throws RemotingCommandException {
    PutKVConfigRequestHeader header = new PutKVConfigRequestHeader();
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.PUT_KV_CONFIG,
        header);
    request.addExtField("namespace", "namespace");
    request.addExtField("key", "key");
    request.addExtField("value", "value");

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

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

    assertThat(namesrvController.getKvConfigManager().getKVConfig("namespace", "key"))
        .isEqualTo("value");
}
 
Example 9
Source File: DefaultRequestProcessorTest.java    From rocketmq-read 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 10
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void updateConsumerOffsetOneway(
    final String addr,
    final UpdateConsumerOffsetRequestHeader requestHeader,
    final long timeoutMillis
) throws RemotingConnectException, RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException,
    InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_CONSUMER_OFFSET, requestHeader);

    this.remotingClient.invokeOneway(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
}
 
Example 11
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void updateConsumerOffsetOneway(//
                                       final String addr, //
                                       final UpdateConsumerOffsetRequestHeader requestHeader, //
                                       final long timeoutMillis//
) throws RemotingConnectException, RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException,
    InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_CONSUMER_OFFSET, requestHeader);

    this.remotingClient.invokeOneway(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
}
 
Example 12
Source File: Broker2Client.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 检查生产者的事务状态(事务回查接口)
 * @param group group
 * @param channel channel
 * @param requestHeader 请求头
 * @param messageExt message扩展
 * @throws Exception ;
 */
public void checkProducerTransactionState(
    final String group,
    final Channel channel,
    final CheckTransactionStateRequestHeader requestHeader,
    final MessageExt messageExt) throws Exception {

    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.CHECK_TRANSACTION_STATE, requestHeader);
    request.setBody(MessageDecoder.encode(messageExt, false));
    try {
        this.brokerController.getRemotingServer().invokeOneway(channel, request, 10);
    } catch (Exception e) {
        log.error("Check transaction failed because invoke producer exception. group={}, msgId={}", group, messageExt.getMsgId(), e.getMessage());
    }
}
 
Example 13
Source File: MQClientAPIImpl.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
public boolean cleanUnusedTopicByAddr(final String addr, long timeoutMillis) throws MQClientException, RemotingConnectException,
    RemotingSendRequestException, RemotingTimeoutException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.CLEAN_UNUSED_TOPIC, null);
    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr),
        request, timeoutMillis);
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            return true;
        }
        default:
            break;
    }

    throw new MQClientException(response.getCode(), response.getRemark());
}
 
Example 14
Source File: MQClientAPIImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public boolean registerClient(final String addr, final HeartbeatData heartbeat, final long timeoutMillis)
    throws RemotingException, InterruptedException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT, null);

    request.setBody(heartbeat.encode());
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    return response.getCode() == ResponseCode.SUCCESS;
}
 
Example 15
Source File: MQClientAPIImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public void registerMessageFilterClass(final String addr, //
    final String consumerGroup, //
    final String topic, //
    final String className, //
    final int classCRC, //
    final byte[] classBody, //
    final long timeoutMillis) throws RemotingConnectException, RemotingSendRequestException, RemotingTimeoutException,
    InterruptedException, MQBrokerException {
    RegisterMessageFilterClassRequestHeader requestHeader = new RegisterMessageFilterClassRequestHeader();
    requestHeader.setConsumerGroup(consumerGroup);
    requestHeader.setClassName(className);
    requestHeader.setTopic(topic);
    requestHeader.setClassCRC(classCRC);

    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.REGISTER_MESSAGE_FILTER_CLASS, requestHeader);
    request.setBody(classBody);
    RemotingCommand response = this.remotingClient.invokeSync(addr, request, timeoutMillis);
    switch (response.getCode()) {
        case ResponseCode.SUCCESS: {
            return;
        }
        default:
            break;
    }

    throw new MQBrokerException(response.getCode(), response.getRemark());
}
 
Example 16
Source File: DefaultRequestProcessorTest.java    From rocketmq-all-4.1.0-incubating 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 17
Source File: DeFiReplyMessageProcessorTest.java    From DeFiBus with Apache License 2.0 4 votes vote down vote up
private RemotingCommand createReplyMsgCommand(int requestCode) {
    SendMessageRequestHeader requestHeader = createSendMsgRequestHeader();
    RemotingCommand request = RemotingCommand.createRequestCommand(requestCode, requestHeader);
    request.makeCustomHeaderToNet();
    return request;
}
 
Example 18
Source File: TlsTest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
private static RemotingCommand createRequest() {
    RequestHeader requestHeader = new RequestHeader();
    requestHeader.setCount(1);
    requestHeader.setMessageTitle("Welcome");
    return RemotingCommand.createRequestCommand(0, requestHeader);
}
 
Example 19
Source File: ConsumeQueueManager.java    From DeFiBus with Apache License 2.0 4 votes vote down vote up
public void resetOffsetOnClient(String consumerGroup, String topic, int queueId, long maxDepth, long maxOffset) {
    long nowDeliverOffset = maxOffset - (long) (maxDepth * RESERVE_PERCENT);
    Map<MessageQueue, Long> offsetTable = new HashMap<MessageQueue, Long>();
    TopicConfig topicConfig = deFiBrokerController.getTopicConfigManager().selectTopicConfig(topic);

    if (queueId < topicConfig.getWriteQueueNums()) {
        MessageQueue mq = new MessageQueue(topic, deFiBrokerController.getBrokerConfig().getBrokerName(), queueId);
        offsetTable.put(mq, nowDeliverOffset);
    }

    ResetOffsetRequestHeader requestHeader = new ResetOffsetRequestHeader();
    requestHeader.setTopic(topic);
    requestHeader.setGroup(consumerGroup);
    requestHeader.setTimestamp(0);
    RemotingCommand request =
        RemotingCommand.createRequestCommand(RequestCode.RESET_CONSUMER_CLIENT_OFFSET, requestHeader);

    ResetOffsetBody body = new ResetOffsetBody();
    body.setOffsetTable(offsetTable);
    request.setBody(body.encode());

    ConsumerGroupInfo consumerGroupInfo =
        this.deFiBrokerController.getConsumerManager().getConsumerGroupInfo(consumerGroup);

    if (consumerGroupInfo != null && !consumerGroupInfo.getAllChannel().isEmpty()) {
        ConcurrentMap<Channel, ClientChannelInfo> channelInfoTable =
            consumerGroupInfo.getChannelInfoTable();
        for (Map.Entry<Channel, ClientChannelInfo> entry : channelInfoTable.entrySet()) {
            int version = entry.getValue().getVersion();
            if (version >= MQVersion.Version.V3_0_7_SNAPSHOT.ordinal()) {
                try {
                    this.deFiBrokerController.getRemotingServer().invokeOneway(entry.getKey(), request, 5000);
                    LOG.info("[reset-offset] reset offset success. topic={}, group={}, clientId={}",
                        topic, consumerGroup, entry.getValue().getClientId());
                } catch (Exception e) {
                    LOG.warn("[reset-offset] reset offset failed. topic={}, group={}",
                        new Object[] {topic, consumerGroup}, e);
                }
            }
        }
    } else {
        String errorInfo =
            String.format("Consumer not online, so can not reset offset, Group: %s Topic: %s Timestamp: %d",
                requestHeader.getGroup(),
                requestHeader.getTopic(),
                requestHeader.getTimestamp());
        LOG.info(errorInfo);
        return;
    }
}
 
Example 20
Source File: TlsTest.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
private static RemotingCommand createRequest() {
    RequestHeader requestHeader = new RequestHeader();
    requestHeader.setCount(1);
    requestHeader.setMessageTitle("Welcome");
    return RemotingCommand.createRequestCommand(0, requestHeader);
}