Пример #1
0
 public boolean contains(DeliverySystem delivery) {
   synchronized (listeners) {
     for (DistributionRequest listener : listeners)
       if (listener.getDeliverySystem().equals(delivery)) return true;
     return false;
   }
 }
Пример #2
0
  private DataEnumerator makeDataEnum(DistributionRequest listener) {

    PreparedStatement prepareStatement = preparedStatements.get(listener);
    try {
      // prepareStatement.setLong(1, listener.getStartTime());
      prepareStatement.setLong(1, listener.getLastVisitedPk());
    } catch (SQLException e) {
      logger.error(e.getMessage(), e);
      return new DataEnumerator();
    }

    DataEnumerator dataEnum =
        new DataEnumerator(
            Main.getStorage(listener.getVSensorConfig().getName()), prepareStatement, false, true);
    return dataEnum;
  }
Пример #3
0
 public boolean vsUnLoading(VSensorConfig config) {
   synchronized (listeners) {
     logger.debug("Distributer unloading: " + listeners.size());
     ArrayList<DistributionRequest> toRemove = new ArrayList<DistributionRequest>();
     for (DistributionRequest listener : listeners) {
       if (listener.getVSensorConfig() == config) toRemove.add(listener);
     }
     for (DistributionRequest listener : toRemove) {
       try {
         removeListener(listener);
       } catch (Exception e) {
         logger.error(e.getMessage(), e);
       }
     }
   }
   return true;
 }
Пример #4
0
 private void addListenerToCandidates(DistributionRequest listener) {
   /** Locker variable should be modified EXACTLY like candidateListeners variable. */
   logger.debug("Adding the listener: " + listener.toString() + " to the candidates.");
   DataEnumerator dataEnum = makeDataEnum(listener);
   if (dataEnum.hasMoreElements()) {
     candidateListeners.put(listener, dataEnum);
     locker.add(listener);
   }
 }
Пример #5
0
 public void consume(StreamElement se, VSensorConfig config) {
   synchronized (listeners) {
     for (DistributionRequest listener : listeners)
       if (listener.getVSensorConfig() == config) {
         logger.debug(
             "sending stream element "
                 + (se == null ? "second-chance-se" : se.toString())
                 + " produced by "
                 + config.getName()
                 + " to listener =>"
                 + listener.toString());
         if (!candidateListeners.containsKey(listener)) {
           addListenerToCandidates(listener);
         } else {
           candidatesForNextRound.put(listener, Boolean.TRUE);
         }
       }
   }
 }
Пример #6
0
 public void removeListener(DistributionRequest listener) {
   synchronized (listeners) {
     if (listeners.remove(listener)) {
       try {
         candidatesForNextRound.remove(listener);
         removeListenerFromCandidates(listener);
         preparedStatements.get(listener).close();
         listener.close();
         logger.warn(
             "Removing listener completely from Distributer [Listener: "
                 + listener.toString()
                 + "]");
       } catch (SQLException e) {
         logger.error(e.getMessage(), e);
       } finally {
         preparedStatements.remove(listener);
       }
     }
   }
 }
Пример #7
0
 private void removeListenerFromCandidates(DistributionRequest listener) {
   /** Locker variable should be modified EXACTLY like candidateListeners variable. */
   logger.debug("Updating the candidate list [" + listener.toString() + " (removed)].");
   if (candidatesForNextRound.contains(listener)) {
     candidateListeners.put(listener, makeDataEnum(listener));
     candidatesForNextRound.remove(listener);
   } else {
     locker.remove(listener);
     candidateListeners.remove(listener);
   }
 }
Пример #8
0
  /**
   * This method only flushes one single stream element from the provided data enumerator. Returns
   * false if the flushing the stream element fails. This method also cleans the prepared statements
   * by removing the listener completely.
   *
   * @param dataEnum
   * @param listener
   * @return
   */
  private boolean flushStreamElement(DataEnumerator dataEnum, DistributionRequest listener) {
    if (listener.isClosed()) {
      logger.debug(
          "Flushing an stream element failed, isClosed=true [Listener: "
              + listener.toString()
              + "]");
      return false;
    }

    if (!dataEnum.hasMoreElements()) {
      logger.debug("Nothing to flush to [Listener: " + listener.toString() + "]");
      return true;
    }

    StreamElement se = dataEnum.nextElement();
    //		boolean success = true;
    boolean success = listener.deliverStreamElement(se);
    if (!success) {
      logger.debug(
          "FLushing an stream element failed, delivery failure [Listener: "
              + listener.toString()
              + "]");
      return false;
    }
    logger.debug("Flushing an stream element succeed [Listener: " + listener.toString() + "]");
    return true;
  }
Пример #9
0
  public void addListener(DistributionRequest listener) {
    synchronized (listeners) {
      if (!listeners.contains(listener)) {
        logger.warn("Adding a listener to Distributer:" + listener.toString());
        boolean needsAnd =
            SQLValidator.removeSingleQuotes(SQLValidator.removeQuotes(listener.getQuery()))
                    .indexOf(" where ")
                > 0;
        String query = SQLValidator.addPkField(listener.getQuery());
        if (needsAnd) query += " AND ";
        else query += " WHERE ";
        query += " timed > " + listener.getStartTime() + " and pk > ? order by timed asc ";
        PreparedStatement prepareStatement = null;
        try {
          prepareStatement =
              getPersistantConnection(listener.getVSensorConfig())
                  .prepareStatement(
                      query); // prepareStatement =
                              // StorageManager.getInstance().getConnection().prepareStatement(query);
          prepareStatement.setMaxRows(1000); // Limit the number of rows loaded in memory.
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
        preparedStatements.put(listener, prepareStatement);
        listeners.add(listener);
        addListenerToCandidates(listener);

      } else {
        logger.warn(
            "Adding a listener to Distributer failed, duplicated listener! " + listener.toString());
      }
    }
  }