protected void unsubscribeCSAll(BrokerState brokerState) throws ClientException { if (!clientConfig.detailState) throw new ClientException( "unsubscribeCSAll() not supported with client.store_detail_state=OFF"); MessageDestination clientDest = MessageDestination.formatClientDestination( clientID, brokerState.getBrokerAddress().getNodeURI()); MessageSender msgSender = brokerState.getMsgSender(); if (msgSender == null) throw new ClientException( "Connection not found for broker " + brokerState.getBrokerAddress()); CompositeSubscriptionMessage[] csMsgArray = brokerState.getCSMessages().toArray(new CompositeSubscriptionMessage[0]); for (CompositeSubscriptionMessage csMsg : csMsgArray) { Uncompositesubscription unCS = new Uncompositesubscription(csMsg.getMessageID()); UncompositesubscriptionMessage unCSMsg = new UncompositesubscriptionMessage( unCS, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest); try { msgSender.send(unCSMsg, HostType.CLIENT); brokerState.removeCSMsg(csMsg); } catch (CommunicationException e) { throw new ClientException(e.getMessage()); } } }
protected void unAdvertiseAll(BrokerState brokerState) throws ClientException { if (!clientConfig.detailState) throw new ClientException("unAdertiseAll() not supported with client.store_detail_state=OFF"); MessageDestination clientDest = MessageDestination.formatClientDestination( clientID, brokerState.getBrokerAddress().getNodeURI()); MessageSender msgSender = brokerState.getMsgSender(); if (msgSender == null) throw new ClientException( "Connection not found for broker " + brokerState.getBrokerAddress()); AdvertisementMessage[] advMsgArray = brokerState.getAdvMessages().toArray(new AdvertisementMessage[0]); for (AdvertisementMessage advMsg : advMsgArray) { Unadvertisement unAdv = new Unadvertisement(advMsg.getMessageID()); UnadvertisementMessage unAdvMsg = new UnadvertisementMessage( unAdv, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest); try { msgSender.send(unAdvMsg, HostType.CLIENT); brokerState.removeAdvMsg(advMsg); } catch (CommunicationException e) { throw new ClientException(e.getMessage()); } } }
/** * 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); } }
/** * Gracefully shuts down the client. Disconnects from all the brokers first. * * @throws ClientException */ public void shutdown() throws ClientException { for (BrokerState brokerState : brokerStates.values()) { disconnect(brokerState); } try { commSystem.shutDown(); } catch (CommunicationException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Send a subscription to a broker with the given URI. In case the brokerURI is null, the * subscription will be sent to the default broker. * * @param sub The subscription to be sent. * @param brokerURI The broker to which the subscription is to be sent. * @return The SubscriptionMessage containing the given subscription. * @throws ClientException Upon the following situations: * <ul> * <li>The client is not connected to the specified broker. * <li>Given brokerURI is badly formated. * <li>There is an error in sending the subscription message. * </ul> */ public SubscriptionMessage subscribe(Subscription sub, String brokerURI) throws ClientException { if (!isConnected()) throw new ClientException("Not connected to any broker"); try { if (brokerURI == null || brokerURI.equals("")) brokerURI = defaultBrokerAddress.getNodeURI(); BrokerState brokerState = getBrokerState(brokerURI); if (brokerState == null) { throw new ClientException("Not connected to broker " + brokerURI); } MessageDestination clientDest = MessageDestination.formatClientDestination( clientID, brokerState.getBrokerAddress().getNodeURI()); SubscriptionMessage subMsg = new SubscriptionMessage( sub, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest); // TODO: fix this hack for historic queries Map<String, Predicate> predMap = subMsg.getSubscription().getPredicateMap(); if (predMap.get("_start_time") != null) { SimpleDateFormat timeFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); try { Date startTime = timeFormat.parse((String) (predMap.get("_start_time")).getValue()); predMap.remove("_start_time"); subMsg.setStartTime(startTime); } catch (java.text.ParseException e) { exceptionLogger.error("Fail to convert Date format : " + e); } } if (predMap.get("_end_time") != null) { SimpleDateFormat timeFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); try { Date endTime = timeFormat.parse((String) (predMap.get("_end_time")).getValue()); predMap.remove("_end_time"); subMsg.setEndTime(endTime); } catch (java.text.ParseException e) { exceptionLogger.error("Fail to convert Date format : " + e); } } String msgID = brokerState.getMsgSender().send(subMsg, HostType.CLIENT); subMsg.setMessageID(msgID); if (clientConfig.detailState) brokerState.addSubMsg(subMsg); return subMsg; } catch (CommunicationException e) { throw new ClientException(e.getMessage()); } }
protected UnadvertisementMessage unAdvertise(String advID, BrokerState brokerState) throws ClientException { try { Unadvertisement unAdv = new Unadvertisement(advID); MessageDestination clientDest = MessageDestination.formatClientDestination( clientID, brokerState.getBrokerAddress().getNodeURI()); UnadvertisementMessage unAdvMsg = new UnadvertisementMessage( unAdv, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest); String msgID = brokerState.getMsgSender().send(unAdvMsg, HostType.CLIENT); unAdvMsg.setMessageID(msgID); if (clientConfig.detailState) brokerState.removeAdvMsg(advID); return unAdvMsg; } catch (CommunicationException e) { throw new ClientException(e.getMessage()); } }
protected UncompositesubscriptionMessage unSubscribeCS(String csID, BrokerState brokerState) throws ClientException { try { Uncompositesubscription unCS = new Uncompositesubscription(csID); MessageDestination clientDest = MessageDestination.formatClientDestination( clientID, brokerState.getBrokerAddress().getNodeURI()); UncompositesubscriptionMessage unCSMsg = new UncompositesubscriptionMessage( unCS, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest); String msgID = brokerState.getMsgSender().send(unCSMsg, HostType.CLIENT); unCSMsg.setMessageID(msgID); if (clientConfig.detailState) brokerState.removeCSMsg(csID); return unCSMsg; } catch (CommunicationException e) { throw new ClientException(e.getMessage()); } }
/** * 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); } }
/** * Initializes the client object: (a) adds the default command handler (b) initializes the logging * system (c) creates a message listener (d) assign client ID if not already assigned (e) if the * configuration object specifies list of brokers, connects to those brokers and hook the message * listener to those broker connections * * <p>This method is always called internally from the constructors * * @param newConfig * @throws ClientException */ protected void initialize(ClientConfig newConfig) throws ClientException { clientConfig = newConfig; cmdHandlers = new ArrayList<CommandHandler>(); addCommandHandler(new BaseCommandHandler(this)); // initialize logging initLog(clientConfig.logLocation); // start a message listener in a thread who listens to messages from server msgListener = new MessageQueueManager(this); if (clientID == null) clientID = clientConfig.clientID; try { commSystem = new CommSystem(); MessageSender.setConnectRetryLimit(clientConfig.connectionRetries); MessageSender.setConnectRetryPauseTime(clientConfig.retryPauseTime); if (clientConfig.connectBrokerList != null) { for (String brokerURI : clientConfig.connectBrokerList) { connect(brokerURI); } } } catch (CommunicationException e) { throw new ClientException(e.getMessage()); } }
public PublicationMessage publish(Publication pub, String brokerURI) throws ClientException { if (!isConnected()) throw new ClientException("Not connected to any broker"); try { if (brokerURI == null || brokerURI.trim().length() == 0) brokerURI = defaultBrokerAddress.getNodeURI(); BrokerState brokerState = getBrokerState(brokerURI); if (brokerState == null) { throw new ClientException("Not connected to broker " + brokerURI); } MessageDestination clientDest = MessageDestination.formatClientDestination( clientID, brokerState.getBrokerAddress().getNodeURI()); PublicationMessage pubMsg = new PublicationMessage( pub, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest); pubCount++; String msgID = brokerState.getMsgSender().send(pubMsg, HostType.CLIENT); pubMsg.setMessageID(msgID); return pubMsg; } catch (CommunicationException e) { throw new ClientException(e.getMessage()); } }
/** * Sends an advertisement to a given broker. * * @param adv The advertisement to be sent. It is an {@link Advertisement} object. * @param brokerURI The URI of the broker to where the advertisement is sent. * @return The {@link AdvertisementMessage} produced by the given advertisement. The message ID of * the message is returned by the broker. * @throws ClientException If the given URI is malformatted, the client is not connected to the * broker, or there is a communication error while sending the advertisement. */ public AdvertisementMessage advertise(Advertisement adv, String brokerURI) throws ClientException { if (!isConnected()) throw new ClientException("Not connected to any broker"); try { if (brokerURI == null || brokerURI.equals("")) brokerURI = defaultBrokerAddress.getNodeURI(); BrokerState brokerState = getBrokerState(brokerURI); if (brokerState == null) { throw new ClientException("Not connected to broker " + brokerURI); } MessageDestination clientDest = MessageDestination.formatClientDestination( clientID, brokerState.getBrokerAddress().getNodeURI()); AdvertisementMessage advMsg = new AdvertisementMessage( adv, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest); String msgID = brokerState.getMsgSender().send(advMsg, HostType.CLIENT); advMsg.setMessageID(msgID); if (clientConfig.detailState) brokerState.addAdvMsg(advMsg); return advMsg; } catch (CommunicationException e) { throw new ClientException(e.getMessage()); } }
/** * Disconnects from a broker with a specified BrokerState. The client first * unsubscribe/unadvertise all the subscriptions/advertisements before disconnecting from brokers. * * @param brokerState the BrokerState of the broker to disconnect from * @return The given broker state. * @throws ClientException Some exeception is unsubscribing/unadvertising or some other * communication error. */ protected BrokerState disconnect(BrokerState brokerState) throws ClientException { try { // Withdraw all the messages if (clientConfig.detailState) { unsubscribeAll(brokerState); unsubscribeCSAll(brokerState); unAdvertiseAll(brokerState); } // disconnect MessageSender msgSender = brokerState.getMsgSender(); msgSender.disconnect( MessageDestination.formatClientDestination( clientID, brokerState.getBrokerAddress().getNodeURI())); // remove the broker state brokerStates.remove(brokerState.getBrokerAddress()); } catch (CommunicationException e) { throw new ClientException( String.format( "Problem disconnecting from Broker %s: %s", brokerState.getBrokerAddress(), e.getMessage())); } return brokerState; }
public CompositeSubscriptionMessage subscribeCS(CompositeSubscription cs, String brokerURI) throws ClientException { if (!isConnected()) throw new ClientException("Not connected to any broker"); try { if (brokerURI == null || brokerURI.trim().equals("")) brokerURI = defaultBrokerAddress.getNodeURI(); BrokerState brokerState = getBrokerState(brokerURI); if (brokerState == null) { throw new ClientException("Not connected to broker " + brokerURI); } MessageDestination clientDest = MessageDestination.formatClientDestination( clientID, brokerState.getBrokerAddress().getNodeURI()); CompositeSubscriptionMessage newCSMsg = new CompositeSubscriptionMessage( cs, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest); String msgID = brokerState.getMsgSender().send(newCSMsg, HostType.CLIENT); newCSMsg.setMessageID(msgID); if (clientConfig.detailState) brokerState.addCSSubMsg(newCSMsg); return newCSMsg; } catch (CommunicationException e) { throw new ClientException(e.getMessage()); } }