Пример #1
0
  /**
   * Connects this channel to a group and gets a state from a specified state provider.
   *
   * <p>This method invokes {@code connect()} and then {@code getState}.
   *
   * <p>If the FLUSH protocol is in the channel's stack definition, only one flush round is executed
   * for both connecting and fetching the state rather than two flushes if we invoke {@code connect}
   * and {@code getState} in succession.
   *
   * <p>If the channel is already connected, an error message will be printed to the error log. If
   * the channel is closed a ChannelClosed exception will be thrown.
   *
   * @param cluster_name The cluster name to connect to. Cannot be null.
   * @param target The state provider. If null, the state will be fetched from the coordinator,
   *     unless this channel is the coordinator.
   * @param timeout The timeout for the state transfer.
   * @exception Exception The protocol stack cannot be started, or the JOIN failed
   * @exception IllegalStateException The channel is closed or disconnected
   * @exception StateTransferException State transfer was not successful
   */
  public synchronized void connect(
      String cluster_name, Address target, long timeout, boolean useFlushIfPresent)
      throws Exception {
    if (!_preConnect(cluster_name)) return;

    if (cluster_name == null) { // only connect if we are not a unicast channel
      state = State.CONNECTED;
      return;
    }

    boolean canFetchState = false;
    try {
      Event connect_event =
          useFlushIfPresent
              ? new Event(Event.CONNECT_WITH_STATE_TRANSFER_USE_FLUSH, cluster_name)
              : new Event(Event.CONNECT_WITH_STATE_TRANSFER, cluster_name);
      _connect(connect_event);
      state = State.CONNECTED;
      notifyChannelConnected(this);
      canFetchState = getView() != null && getView().size() > 1;

      // if I am not the only member in cluster then
      if (canFetchState) getState(target, timeout, false); // fetch state from target
    } finally {
      // stopFlush if we fetched the state or failed to connect...
      if ((flushSupported() && useFlushIfPresent) && (canFetchState || state != State.CONNECTED))
        stopFlush();
    }
  }
Пример #2
0
  /**
   * Connects the channel to a group.
   *
   * @see JChannel#connect(String)
   */
  @ManagedOperation(description = "Connects the channel to a group")
  protected synchronized void connect(String cluster_name, boolean useFlushIfPresent)
      throws Exception {
    if (!_preConnect(cluster_name)) return;

    if (cluster_name != null) { // only connect if we are not a unicast channel
      Event connect_event =
          useFlushIfPresent
              ? new Event(Event.CONNECT_USE_FLUSH, cluster_name)
              : new Event(Event.CONNECT, cluster_name);
      _connect(connect_event);
    }
    state = State.CONNECTED;
    notifyChannelConnected(this);
  }