Пример #1
0
 protected void unsubscribeAll(BrokerState brokerState) throws ClientException {
   if (!clientConfig.detailState)
     throw new ClientException(
         "unsubscribeAll() 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());
   SubscriptionMessage[] subMsgArray =
       brokerState.getSubMessages().toArray(new SubscriptionMessage[0]);
   for (SubscriptionMessage subMsg : subMsgArray) {
     Unsubscription unSub = new Unsubscription(subMsg.getMessageID());
     UnsubscriptionMessage unSubMsg =
         new UnsubscriptionMessage(
             unSub, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest);
     try {
       msgSender.send(unSubMsg, HostType.CLIENT);
       if (clientConfig.detailState) brokerState.removeSubMsg(subMsg);
     } catch (CommunicationException e) {
       throw new ClientException(e.getMessage());
     }
   }
 }
Пример #2
0
 /**
  * Returns all the subscription messages the client has sent in the past. This method is
  * operational only when the store_detail_state option is switched ON.
  *
  * @return
  * @throws ClientException If the client.store_detail_state option is OFF
  */
 public Map<String, SubscriptionMessage> getSubscriptions() throws ClientException {
   if (!clientConfig.detailState)
     throw new ClientException(
         "getSubscriptions() not supported with client.store_detail_state=OFF");
   HashMap<String, SubscriptionMessage> idMsgMap = new HashMap<String, SubscriptionMessage>();
   for (BrokerState brokerState : brokerStates.values()) {
     Set<SubscriptionMessage> subs = brokerState.getSubMessages();
     for (SubscriptionMessage subMsg : subs) idMsgMap.put(subMsg.getMessageID(), subMsg);
   }
   return idMsgMap;
 }
Пример #3
0
  /**
   * 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());
    }
  }