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()); } } }
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()); } } }
/** * Returns the map of brokerID -> brokerURI of all the existing broker connections. * * <p>TODO: This method wastes memory because it now maps brokerURI to brokerURI; it got changed * on the way; remove this method and use {@link #getBrokerURIList()} instead * * @return */ public Map<String, String> getBrokerConnections() { Map<String, String> idURIMap = new HashMap<String, String>(); for (BrokerState brokerState : brokerStates.values()) { idURIMap.put( brokerState.getBrokerAddress().getNodeURI(), brokerState.getBrokerAddress().getNodeURI()); } return idURIMap; }
/** * 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()); } }
/** * Disconnects from all the connected brokers. The client first unsubscribe/unadvertise all the * subscriptions/advertisements before disconnecting from brokers. * * @return A String message that describes the success/failure of the operation. */ public String disconnectAll() { String outStr = ""; for (BrokerState brokerState : new HashMap<NodeAddress, BrokerState>(brokerStates).values()) { try { disconnect(brokerState); outStr += "disconnected from " + brokerState.getBrokerAddress() + "\n"; } catch (ClientException e) { outStr += e.getMessage() + "\n"; } } return outStr; }
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()); } }