public void start(final boolean startFactory) throws MQClientException {
    switch (this.serviceState) {
      case CREATE_JUST:
        this.serviceState = ServiceState.START_FAILED;

        this.checkConfig();

        this.mQClientFactory =
            MQClientManager.getInstance().getAndCreateMQClientFactory(this.defaultMQProducer);

        boolean registerOK =
            mQClientFactory.registerProducer(this.defaultMQProducer.getProducerGroup(), this);
        if (!registerOK) {
          this.serviceState = ServiceState.CREATE_JUST;
          throw new MQClientException(
              "The producer group["
                  + this.defaultMQProducer.getProducerGroup()
                  + "] has been created before, specify another name please."
                  + FAQUrl.suggestTodo(FAQUrl.GROUP_NAME_DUPLICATE_URL),
              null);
        }

        // 默认Topic注册
        this.topicPublishInfoTable.put(
            this.defaultMQProducer.getCreateTopicKey(), new TopicPublishInfo());

        if (startFactory) {
          mQClientFactory.start();
        }

        log.info("the producer [{}] start OK", this.defaultMQProducer.getProducerGroup());
        this.serviceState = ServiceState.RUNNING;
        break;
      case RUNNING:
      case START_FAILED:
      case SHUTDOWN_ALREADY:
        throw new MQClientException(
            "The producer service state not OK, maybe started once, " //
                + this.serviceState //
                + FAQUrl.suggestTodo(FAQUrl.CLIENT_SERVICE_NOT_OK),
            null);
      default:
        break;
    }

    this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
  }
示例#2
0
  public void shutdown() {
    // Consumer
    if (!this.consumerTable.isEmpty()) return;

    // AdminExt
    if (!this.adminExtTable.isEmpty()) return;

    // Producer
    if (this.producerTable.size() > 1) return;

    synchronized (this) {
      switch (this.serviceState) {
        case CREATE_JUST:
          break;
        case RUNNING:
          this.defaultMQProducer.getDefaultMQProducerImpl().shutdown(false);

          this.serviceState = ServiceState.SHUTDOWN_ALREADY;
          this.pullMessageService.shutdown(true);
          this.scheduledExecutorService.shutdown();
          this.mQClientAPIImpl.shutdown();
          this.rebalanceService.shutdown();

          if (this.datagramSocket != null) {
            this.datagramSocket.close();
            this.datagramSocket = null;
          }
          MQClientManager.getInstance().removeClientFactory(this.clientId);
          log.info("the client factory [{}] shutdown OK", this.clientId);
          break;
        case SHUTDOWN_ALREADY:
          break;
        default:
          break;
      }
    }
  }