Example #1
0
  private HeartbeatData prepareHeartbeatData() {
    HeartbeatData heartbeatData = new HeartbeatData();

    // clientID
    heartbeatData.setClientID(this.clientId);

    // Consumer
    for (String group : this.consumerTable.keySet()) {
      MQConsumerInner impl = this.consumerTable.get(group);
      if (impl != null) {
        ConsumerData consumerData = new ConsumerData();
        consumerData.setGroupName(impl.groupName());
        consumerData.setConsumeType(impl.consumeType());
        consumerData.setMessageModel(impl.messageModel());
        consumerData.setConsumeFromWhere(impl.consumeFromWhere());
        consumerData.getSubscriptionDataSet().addAll(impl.subscriptions());

        heartbeatData.getConsumerDataSet().add(consumerData);
      }
    }

    // Producer
    for (String group : this.producerTable.keySet()) {
      MQProducerInner impl = this.producerTable.get(group);
      if (impl != null) {
        ProducerData producerData = new ProducerData();
        producerData.setGroupName(group);

        heartbeatData.getProducerDataSet().add(producerData);
      }
    }

    return heartbeatData;
  }
  public RemotingCommand heartBeat(ChannelHandlerContext ctx, RemotingCommand request) {
    RemotingCommand response = RemotingCommand.createResponseCommand(null);

    HeartbeatData heartbeatData = HeartbeatData.decode(request.getBody(), HeartbeatData.class);

    ClientChannelInfo clientChannelInfo =
        new ClientChannelInfo( //
            ctx.channel(), //
            heartbeatData.getClientID(), //
            request.getLanguage(), //
            request.getVersion() //
            );

    // 注册Consumer
    for (ConsumerData data : heartbeatData.getConsumerDataSet()) {
      SubscriptionGroupConfig subscriptionGroupConfig =
          this.brokerController
              .getSubscriptionGroupManager()
              .findSubscriptionGroupConfig(data.getGroupName());
      if (null != subscriptionGroupConfig) {
        String newTopic = MixAll.getRetryTopic(data.getGroupName());
        this.brokerController
            .getTopicConfigManager()
            .createTopicInSendMessageBackMethod( //
                newTopic, //
                subscriptionGroupConfig.getRetryQueueNums(), //
                PermName.PERM_WRITE | PermName.PERM_READ);
      }

      boolean changed =
          this.brokerController
              .getConsumerManager()
              .registerConsumer( //
                  data.getGroupName(), //
                  clientChannelInfo, //
                  data.getConsumeType(), //
                  data.getMessageModel(), //
                  data.getConsumeFromWhere(), //
                  data.getSubscriptionDataSet() //
                  );

      if (changed) {
        log.info(
            "registerConsumer info changed {} {}", //
            data.toString(), //
            RemotingHelper.parseChannelRemoteAddr(ctx.channel()) //
            );
      }
    }

    // 注册Producer
    for (ProducerData data : heartbeatData.getProducerDataSet()) {
      this.brokerController
          .getProducerManager()
          .registerProducer(data.getGroupName(), clientChannelInfo);
    }

    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
  }