/** * To check whether an active connection exists to a given broker. * * @param brokerURI The URI of the broker to check for the connection. * @return true is the client is connected to the given broker; false otherwise. * @throws ClientException If the given URI is malformatted. * @see NodeAddress , {@link #connect(String)} */ public boolean connectionIsActive(String brokerURI) throws ClientException { try { NodeAddress brokerAddr = ConnectionHelper.getAddress(brokerURI); return brokerStates.containsKey(brokerAddr); } catch (CommunicationException e) { throw new ClientException(e.getMessage(), e); } }
/** * Provided a broker URI, this method returns the associated broker state. The broker state * contains the details about the broker connection as well as, depending on the configuration, * the adv/sub messages sent to the specified broker. * * @param brokerURI * @return * @throws ClientException If the given broker URI is mal-formatted */ public BrokerState getBrokerState(String brokerURI) throws ClientException { try { NodeAddress brokerAddress = ConnectionHelper.getAddress(brokerURI); return brokerStates.get(brokerAddress); } catch (CommunicationException e) { throw new ClientException("Could not get broker status: " + e.getMessage(), e); } }
/** * 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); } }