Example #1
0
  /**
   * Connects to a broker with the given URI. The given URI should conform to an accepted
   * communication protocol format.
   *
   * @param brokerURI URI of the broker to connect to.
   * @return A BrokerState data structure to keep track of the state of the connection and related
   *     operation
   * @throws ClientException In case the given URI is malformated, a connection already exists to
   *     the specified broker, or a communication error is occurred.
   * @see BrokerState, NodeAddress
   */
  public BrokerState connect(String brokerURI) throws ClientException {
    try {
      NodeAddress brokerAddr = ConnectionHelper.getAddress(brokerURI);
      if (brokerStates.containsKey(brokerAddr)) {
        throw new ClientException("Server connection already exists");
      } else {
        if (brokerStates.size() == 0) setDefaultBrokerAddress(brokerAddr);
        MessageSender msgSender = commSystem.getMessageSender(brokerURI);
        BrokerState bState = addBrokerState(brokerAddr, msgSender);

        msgSender.connect(
            MessageDestination.formatClientDestination(clientID, brokerAddr.getNodeURI()),
            msgListener);
        return bState;
      }
    } catch (CommunicationException e) {
      exceptionLogger.error("Could not connect to broker: " + e);
      throw new ClientException("Could not connect to broker: " + e.getMessage(), e);
    }
  }