/** * 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()); } }