示例#1
0
  public void readInterventions() {
    InterventionMap newMap = new InterventionMap();

    List<Criterion> c = new ArrayList<Criterion>();
    c.add(Restrictions.isNull("endTime"));

    for (Intervention intervention :
        database.getList(Intervention.class, null, null, null, c, null)) {
      newMap.put(intervention.getDevice(), intervention);
    }

    if (!interventions.equals(newMap)) {
      interventions = newMap;
      if (eventBus != null) {
        log.info("Read Interventions from DB");
        InterventionMapChangedRemoteEvent.fire(this, eventBus, interventions);
      }
    }
  }
示例#2
0
  private boolean checkUpdateAndConnection() throws HibernateException {

    long now = new Date().getTime();

    try {
      Date lastUpdate = database.getLastMeasurementUpdateTime();
      if (lastUpdate != null) {
        long time = lastUpdate.getTime();
        updated = (time > now - delay) ? Ternary.True : Ternary.False;
        updatedCause = "Last Update: " + new Date(time);
      } else {
        updated = Ternary.False;
        updatedCause = "Never Updated";
      }
      connected = Ternary.True;
      connectedCause = "";
    } catch (HibernateException e) {
      connected = Ternary.False;
      connectedCause = e.getMessage();
      throw e;
    } finally {
      if (eventBus != null) {
        if (!updated.equals(wasUpdated)) {
          ConnectionStatusChangedRemoteEvent.fire(
              eventBus, ConnectionType.databaseUpdate, updated, updatedCause);
        }

        if (!connected.equals(wasConnected)) {
          ConnectionStatusChangedRemoteEvent.fire(
              eventBus, ConnectionType.databaseConnect, connected, connectedCause);
        }
      }
    }

    return !updated.isFalse();
  }
示例#3
0
  private DatabaseHandler(final RemoteEventBus eventBus) {
    this.eventBus = eventBus;

    delay =
        ServerStorage.getLocalStorageIfSupported()
                .getInt("APVS.database.updateDelay", DEFAULT_MAX_UPDATE_DELAY)
            * SECONDS;

    log.info("Using update delay: " + delay + " ms");

    database = Database.getInstance();

    RequestRemoteEvent.register(
        this,
        eventBus,
        new RequestRemoteEvent.Handler() {

          @Override
          public void onRequestEvent(RequestRemoteEvent event) {
            String type = event.getRequestedClassName();

            if (type.equals(InterventionMapChangedRemoteEvent.class.getName())) {
              log.info("Request to DB " + event);
              InterventionMapChangedRemoteEvent.fire(this, eventBus, interventions);
            } else if (type.equals(ConnectionStatusChangedRemoteEvent.class.getName())) {
              ConnectionStatusChangedRemoteEvent.fire(
                  eventBus, ConnectionType.databaseConnect, connected, connectedCause);
              ConnectionStatusChangedRemoteEvent.fire(
                  eventBus, ConnectionType.databaseUpdate, updated, updatedCause);
            }
          }
        });

    try {
      checkUpdateAndConnection();
      readInterventions();
    } catch (HibernateException e1) {
      log.warn("Problem", e1);
    }

    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    executor.scheduleWithFixedDelay(
        new Runnable() {

          // ScheduledFuture<?> watchdog;

          @Override
          public void run() {
            try {
              if (!checkUpdateAndConnection()) {
                log.warn("DB " + updatedCause);
              } else if (!isConnected()) {
                log.warn("DB " + connectedCause);
              }
            } catch (HibernateException e) {
              log.warn("Could not update or reach DB: ", e);
            }
          }
        },
        0,
        30,
        TimeUnit.SECONDS);
  }