/**
  * launch the scheduler loop every 250 milliseconds, to reconnect a failed connection. Will verify
  * if there is an existing scheduler
  *
  * @param now now will launch the loop immediatly, 250ms after if false
  */
 protected void launchFailLoopIfNotlaunched(boolean now) {
   if (isLooping.compareAndSet(false, true) && urlParser.getOptions().failoverLoopRetries != 0) {
     scheduledFailover =
         Executors.newSingleThreadScheduledExecutor()
             .scheduleWithFixedDelay(
                 new FailLoop(this), now ? 0 : 250, 250, TimeUnit.MILLISECONDS);
   }
 }
 /** Permit to remove Host to blacklist after loadBalanceBlacklistTimeout seconds. */
 public void resetOldsBlackListHosts() {
   long currentTime = System.currentTimeMillis();
   Set<HostAddress> currentBlackListkeys = new HashSet<HostAddress>(blacklist.keySet());
   for (HostAddress blackListHost : currentBlackListkeys) {
     if (blacklist.get(blackListHost)
         < currentTime - urlParser.getOptions().loadBalanceBlacklistTimeout * 1000) {
       //                if (log.isTraceEnabled()) log.trace("host " + blackListHost+" remove of
       // blacklist");
       blacklist.remove(blackListHost);
     }
   }
 }
 public boolean isAutoReconnect() {
   return urlParser.getOptions().autoReconnect;
 }
 public int getRetriesAllDown() {
   return urlParser.getOptions().retriesAllDown;
 }
 /**
  * Initialisation.
  *
  * @param urlParser url options.
  */
 public MastersFailoverListener(final UrlParser urlParser) {
   super(urlParser);
   this.mode = urlParser.getHaMode();
   setMasterHostFail();
 }
Пример #6
0
  /*
  Parse ConnectorJ compatible urls
  jdbc:mysql://host:port/database
  Example: jdbc:mysql://localhost:3306/test?user=root&password=passwd
   */
  private static void parseInternal(UrlParser urlParser, String url, Properties properties)
      throws SQLException {
    try {
      int separator = url.indexOf("//");
      if (separator == -1) {
        throw new IllegalArgumentException(
            "url parsing error : '//' is not present in the url " + url);
      }
      String[] baseTokens = url.substring(0, separator).split(":");

      // parse HA mode
      urlParser.haMode = HaMode.NONE;
      if (baseTokens.length > 2) {
        try {
          urlParser.haMode = HaMode.valueOf(baseTokens[2].toUpperCase());
        } catch (IllegalArgumentException i) {
          throw new IllegalArgumentException(
              "url parameter error '"
                  + baseTokens[2]
                  + "' is a unknown parameter in the url "
                  + url);
        }
      }

      url = url.substring(separator + 2);
      String[] tokens = url.split("/");
      String hostAddressesString = tokens[0];
      String additionalParameters =
          (tokens.length > 1) ? url.substring(tokens[0].length() + 1) : null;

      urlParser.addresses = HostAddress.parse(hostAddressesString, urlParser.haMode);

      if (additionalParameters == null) {
        urlParser.database = null;
        urlParser.options = DefaultOptions.parse(urlParser.haMode, "", properties);
      } else {
        int ind = additionalParameters.indexOf('?');
        if (ind > -1) {
          urlParser.database = additionalParameters.substring(0, ind);
          urlParser.options =
              DefaultOptions.parse(
                  urlParser.haMode, additionalParameters.substring(ind + 1), properties);
        } else {
          urlParser.database = additionalParameters;
          urlParser.options = DefaultOptions.parse(urlParser.haMode, "", properties);
        }
      }

      if (urlParser.haMode == HaMode.AURORA) {
        for (HostAddress hostAddress : urlParser.addresses) {
          hostAddress.type = null;
        }
      } else {
        for (HostAddress hostAddress : urlParser.addresses) {
          if (hostAddress.type == null) {
            hostAddress.type = ParameterConstant.TYPE_MASTER;
          }
        }
      }
    } catch (IllegalArgumentException i) {
      throw new SQLException(i.getMessage());
    }
  }