コード例 #1
0
  public BrokerDetails getNextBrokerDetails() {
    BrokerDetails broker = null;

    synchronized (_brokerListLock) {
      if (_currentBrokerIndex == (_connectionDetails.getBrokerCount() - 1)) {
        _currentBrokerIndex = 0;
      } else {
        _currentBrokerIndex++;
      }

      broker = _connectionDetails.getBrokerDetails(_currentBrokerIndex);

      // When the broker list is updated it will include the current broker as well
      // There is no point trying it again, so trying the next one.
      if (_currentBrokerDetail != null
          && broker.getHost().equals(_currentBrokerDetail.getHost())
          && broker.getPort() == _currentBrokerDetail.getPort()) {
        if (_connectionDetails.getBrokerCount() > 1) {
          return getNextBrokerDetails();
        } else {
          _failedAttemps++;
          return null;
        }
      }
    }

    String delayStr = broker.getProperty(BrokerDetails.OPTIONS_CONNECT_DELAY);
    if (delayStr != null) {
      Long delay = Long.parseLong(delayStr);
      _logger.info("Delay between connect retries:" + delay);
      try {
        Thread.sleep(delay);
      } catch (InterruptedException ie) {
        return null;
      }
    } else {
      _logger.info(
          "No delay between connect retries, use tcp://host:port?connectdelay='value' to enable.");
    }

    _failedAttemps++;
    _currentBrokerDetail = broker;

    return broker;
  }
コード例 #2
0
  public void onMessage(Message m) {
    _logger.info("Failover exchange notified cluster membership change");

    String currentBrokerIP = "";
    try {
      currentBrokerIP = InetAddress.getByName(_currentBrokerDetail.getHost()).getHostAddress();
    } catch (Exception e) {
      _logger.warn("Unable to resolve current broker host name", e);
    }

    List<BrokerDetails> brokerList = new ArrayList<BrokerDetails>();
    try {
      List<String> list = (List<String>) m.getObjectProperty("amq.failover");
      for (String brokerEntry : list) {
        String[] urls = brokerEntry.substring(5).split(",");
        // Iterate until you find the correct transport
        // Need to reconsider the logic when the C++ broker supports
        // SSL URLs.
        for (String url : urls) {
          String[] tokens = url.split(":");
          if (tokens[0].equalsIgnoreCase(_originalBrokerDetail.getTransport())) {
            BrokerDetails broker = new AMQBrokerDetails();
            broker.setTransport(tokens[0]);
            broker.setHost(tokens[1]);
            broker.setPort(Integer.parseInt(tokens[2]));
            broker.setProperties(_originalBrokerDetail.getProperties());
            broker.setSSLConfiguration(_originalBrokerDetail.getSSLConfiguration());
            brokerList.add(broker);

            if (currentBrokerIP.equals(broker.getHost())
                && _currentBrokerDetail.getPort() == broker.getPort()) {
              _currentBrokerIndex = brokerList.indexOf(broker);
            }

            break;
          }
        }
      }
    } catch (JMSException e) {
      _logger.error("Error parsing the message sent by failover exchange", e);
    }

    synchronized (_brokerListLock) {
      _connectionDetails.setBrokerDetails(brokerList);
    }

    _logger.info("============================================================");
    _logger.info("Updated cluster membership details " + _connectionDetails);
    _logger.info("============================================================");
  }