예제 #1
0
  public void shutdown(final boolean terminate) {
    long start = Clock.currentTimeMillis();
    if (logger.isFinestEnabled()) {
      logger.finest("We are being asked to shutdown when state = " + state);
    }

    if (!STATE.compareAndSet(this, NodeState.ACTIVE, NodeState.SHUTTING_DOWN)) {
      waitIfAlreadyShuttingDown();
      return;
    }

    if (!terminate) {
      final int maxWaitSeconds =
          groupProperties.getSeconds(GroupProperty.GRACEFUL_SHUTDOWN_MAX_WAIT);
      if (!partitionService.prepareToSafeShutdown(maxWaitSeconds, TimeUnit.SECONDS)) {
        logger.warning(
            "Graceful shutdown could not be completed in " + maxWaitSeconds + " seconds!");
      }
      clusterService.sendShutdownMessage();
    } else {
      logger.warning("Terminating forcefully...");
    }

    // set the joined=false first so that
    // threads do not process unnecessary
    // events, such as remove address
    joined.set(false);
    setMasterAddress(null);
    try {
      if (groupProperties.getBoolean(GroupProperty.SHUTDOWNHOOK_ENABLED)) {
        Runtime.getRuntime().removeShutdownHook(shutdownHookThread);
      }
      discoveryService.destroy();
      logger.info("Shutting down connection manager...");
      connectionManager.shutdown();
    } catch (Throwable ignored) {
    }
    versionCheck.shutdown();
    if (managementCenterService != null) {
      managementCenterService.shutdown();
    }

    textCommandService.stop();
    if (multicastService != null) {
      logger.info("Shutting down multicast service...");
      multicastService.stop();
    }
    logger.info("Shutting down connection manager...");
    connectionManager.shutdown();

    logger.info("Shutting down node engine...");
    nodeEngine.shutdown(terminate);

    if (securityContext != null) {
      securityContext.destroy();
    }
    nodeExtension.destroy();
    logger.finest("Destroying serialization service...");
    serializationService.destroy();

    hazelcastThreadGroup.destroy();
    logger.info(
        "Hazelcast Shutdown is completed in " + (Clock.currentTimeMillis() - start) + " ms.");
    state = NodeState.SHUT_DOWN;
  }