com.google.ipc.invalidation.util.NamedRunnable Java Examples

The following examples show how to use com.google.ipc.invalidation.util.NamedRunnable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AndroidStorage.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void readKey(final String key, final Callback<SimplePair<Status, byte[]>> done) {
  scheduler.execute(new NamedRunnable("AndroidStorage.readKey") {
    @Override
    public void run() {
        byte [] value = properties.get(key);
        if (value != null) {
          done.accept(SimplePair.of(SUCCESS, value));
        } else {
          Status status =
              Status.newInstance(Status.Code.PERMANENT_FAILURE, "No value in map for " + key);
          done.accept(SimplePair.of(status, (byte []) null));
        }
    }
  });
}
 
Example #2
Source File: RecurringTask.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private NamedRunnable createRunnable() {
  return new NamedRunnable(name) {
    @Override
    public void run() {
      Preconditions.checkState(scheduler.isRunningOnThread(), "Not on scheduler thread");
      isScheduled = false;
      if (runTask()) {
        // The task asked to be rescheduled, so reschedule it after a timeout has occured.
        Preconditions.checkState((delayGenerator != null) || (initialDelayMs != 0),
            "Spinning: No exp back off and initialdelay is zero");
        ensureScheduled(true, "Retry");
      } else if (delayGenerator != null) {
        // The task asked not to be rescheduled.  Treat it as having "succeeded" and reset the
        // delay generator.
        delayGenerator.reset();
      }
    }
  };
}
 
Example #3
Source File: AndroidStorage.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void readKey(final String key, final Callback<SimplePair<Status, byte[]>> done) {
  scheduler.execute(new NamedRunnable("AndroidStorage.readKey") {
    @Override
    public void run() {
        byte [] value = properties.get(key);
        if (value != null) {
          done.accept(SimplePair.of(SUCCESS, value));
        } else {
          Status status =
              Status.newInstance(Status.Code.PERMANENT_FAILURE, "No value in map for " + key);
          done.accept(SimplePair.of(status, (byte []) null));
        }
    }
  });
}
 
Example #4
Source File: MemoryStorageImpl.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void writeKey(final String key, final byte[] value, final Callback<Status> callback) {
  // Need to schedule immediately because C++ locks aren't reentrant, and
  // C++ locking code assumes that this call will not return directly.

  // Schedule the write even if the resources are started since the
  // scheduler will prevent it from running in case the resources have been
  // stopped.
  scheduler.schedule(Scheduler.NO_DELAY,
      new NamedRunnable("MemoryStorage.writeKey") {
    @Override
    public void run() {
      ticlPersistentState.put(key, value);
      callback.accept(Status.newInstance(Status.Code.SUCCESS, ""));
    }
  });
}
 
Example #5
Source File: MemoryStorageImpl.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void readKey(final String key, final Callback<SimplePair<Status, byte[]>> done) {
  scheduler.schedule(Scheduler.NO_DELAY,
      new NamedRunnable("MemoryStorage.readKey") {
    @Override
    public void run() {
      byte[] value = TypedUtil.mapGet(ticlPersistentState, key);
      final SimplePair<Status, byte[]> result;
      if (value != null) {
        result = SimplePair.of(Status.newInstance(Status.Code.SUCCESS, ""), value);
      } else {
        String error = "No value present in map for " + key;
        result = SimplePair.of(Status.newInstance(Status.Code.PERMANENT_FAILURE, error), null);
      }
      done.accept(result);
    }
  });
}
 
Example #6
Source File: AndroidResourcesFactory.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void schedule(final int delayMs, final Runnable runnable) {
  // For simplicity, schedule first and then check when the event runs later if the resources
  // have been shut down.
  scheduler.schedule(new NamedRunnable("AndroidScheduler") {
    @Override
    public void run() {
      if (thread != Thread.currentThread()) {
        // Either at initialization or if the thread has been killed or restarted by the
        // Executor service.
        thread = Thread.currentThread();
        thread.setName(threadName);
      }

      if (systemResources.isStarted()) {
        runnable.run();
      } else {
        systemResources.getLogger().warning("Not running on internal thread since resources " +
          "not started %s, %s", delayMs, runnable);
      }
    }
  }, delayMs, TimeUnit.MILLISECONDS);
}
 
Example #7
Source File: AndroidInternalScheduler.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@Override
public void schedule(int delayMs, Runnable runnable) {
  if (!(runnable instanceof NamedRunnable)) {
    throw new RuntimeException("Unsupported: can only schedule named runnables, not " + runnable);
  }
  // Create an intent that will cause the service to run the right recurring task. We explicitly
  // target it to our AlarmReceiver so that no other process in the system can receive it and so
  // that our AlarmReceiver will not be able to receive events from any other broadcaster (which
  // it would be if we used action-based targeting).
  String taskName = ((NamedRunnable) runnable).getName();
  long executeMs = clock.nowMs() + delayMs;
  while (scheduledTasks.containsKey(executeMs)) {
    ++executeMs;
  }
  scheduledTasks.put(executeMs, taskName);
  ensureIntentScheduledForSoonestTask();
}
 
Example #8
Source File: RecurringTask.java    From 365browser with Apache License 2.0 6 votes vote down vote up
private NamedRunnable createRunnable() {
  return new NamedRunnable(name) {
    @Override
    public void run() {
      Preconditions.checkState(scheduler.isRunningOnThread(), "Not on scheduler thread");
      isScheduled = false;
      if (runTask()) {
        // The task asked to be rescheduled, so reschedule it after a timeout has occured.
        Preconditions.checkState((delayGenerator != null) || (initialDelayMs != 0),
            "Spinning: No exp back off and initialdelay is zero");
        ensureScheduled(true, "Retry");
      } else if (delayGenerator != null) {
        // The task asked not to be rescheduled.  Treat it as having "succeeded" and reset the
        // delay generator.
        delayGenerator.reset();
      }
    }
  };
}
 
Example #9
Source File: MemoryStorageImpl.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@Override
public void writeKey(final String key, final byte[] value, final Callback<Status> callback) {
  // Need to schedule immediately because C++ locks aren't reentrant, and
  // C++ locking code assumes that this call will not return directly.

  // Schedule the write even if the resources are started since the
  // scheduler will prevent it from running in case the resources have been
  // stopped.
  scheduler.schedule(Scheduler.NO_DELAY,
      new NamedRunnable("MemoryStorage.writeKey") {
    @Override
    public void run() {
      ticlPersistentState.put(key, value);
      callback.accept(Status.newInstance(Status.Code.SUCCESS, ""));
    }
  });
}
 
Example #10
Source File: MemoryStorageImpl.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@Override
public void readKey(final String key, final Callback<SimplePair<Status, byte[]>> done) {
  scheduler.schedule(Scheduler.NO_DELAY,
      new NamedRunnable("MemoryStorage.readKey") {
    @Override
    public void run() {
      byte[] value = TypedUtil.mapGet(ticlPersistentState, key);
      final SimplePair<Status, byte[]> result;
      if (value != null) {
        result = SimplePair.of(Status.newInstance(Status.Code.SUCCESS, ""), value);
      } else {
        String error = "No value present in map for " + key;
        result = SimplePair.of(Status.newInstance(Status.Code.PERMANENT_FAILURE, error), null);
      }
      done.accept(result);
    }
  });
}
 
Example #11
Source File: AndroidInternalScheduler.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void schedule(int delayMs, Runnable runnable) {
  if (!(runnable instanceof NamedRunnable)) {
    throw new RuntimeException("Unsupported: can only schedule named runnables, not " + runnable);
  }
  // Create an intent that will cause the service to run the right recurring task. We explicitly
  // target it to our AlarmReceiver so that no other process in the system can receive it and so
  // that our AlarmReceiver will not be able to receive events from any other broadcaster (which
  // it would be if we used action-based targeting).
  String taskName = ((NamedRunnable) runnable).getName();
  Intent eventIntent = ProtocolIntents.newSchedulerIntent(taskName, ticlId);
  eventIntent.setClass(context, AlarmReceiver.class);

  // Create a pending intent that will cause the AlarmManager to fire the above intent.
  PendingIntent sender = PendingIntent.getBroadcast(context,
      (int) (Integer.MAX_VALUE * Math.random()), eventIntent, PendingIntent.FLAG_ONE_SHOT);

  // Schedule the pending intent after the appropriate delay.
  AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  long executeMs = clock.nowMs() + delayMs;
  alarmManager.set(AlarmManager.RTC, executeMs, sender);
}
 
Example #12
Source File: AndroidInternalScheduler.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void schedule(int delayMs, Runnable runnable) {
  if (!(runnable instanceof NamedRunnable)) {
    throw new RuntimeException("Unsupported: can only schedule named runnables, not " + runnable);
  }
  // Create an intent that will cause the service to run the right recurring task. We explicitly
  // target it to our AlarmReceiver so that no other process in the system can receive it and so
  // that our AlarmReceiver will not be able to receive events from any other broadcaster (which
  // it would be if we used action-based targeting).
  String taskName = ((NamedRunnable) runnable).getName();
  Intent eventIntent = ProtocolIntents.newSchedulerIntent(taskName, ticlId);
  eventIntent.setClass(context, AlarmReceiver.class);

  // Create a pending intent that will cause the AlarmManager to fire the above intent.
  PendingIntent sender = PendingIntent.getBroadcast(context,
      (int) (Integer.MAX_VALUE * Math.random()), eventIntent, PendingIntent.FLAG_ONE_SHOT);

  // Schedule the pending intent after the appropriate delay.
  AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  long executeMs = clock.nowMs() + delayMs;
  alarmManager.set(AlarmManager.RTC, executeMs, sender);
}
 
Example #13
Source File: MemoryStorageImpl.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void readKey(final String key, final Callback<SimplePair<Status, byte[]>> done) {
  scheduler.schedule(Scheduler.NO_DELAY,
      new NamedRunnable("MemoryStorage.readKey") {
    @Override
    public void run() {
      byte[] value = TypedUtil.mapGet(ticlPersistentState, key);
      final SimplePair<Status, byte[]> result;
      if (value != null) {
        result = SimplePair.of(Status.newInstance(Status.Code.SUCCESS, ""), value);
      } else {
        String error = "No value present in map for " + key;
        result = SimplePair.of(Status.newInstance(Status.Code.PERMANENT_FAILURE, error), null);
      }
      done.accept(result);
    }
  });
}
 
Example #14
Source File: RecurringTask.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private NamedRunnable createRunnable() {
  return new NamedRunnable(name) {
    @Override
    public void run() {
      Preconditions.checkState(scheduler.isRunningOnThread(), "Not on scheduler thread");
      isScheduled = false;
      if (runTask()) {
        // The task asked to be rescheduled, so reschedule it after a timeout has occured.
        Preconditions.checkState((delayGenerator != null) || (initialDelayMs != 0),
            "Spinning: No exp back off and initialdelay is zero");
        ensureScheduled(true, "Retry");
      } else if (delayGenerator != null) {
        // The task asked not to be rescheduled.  Treat it as having "succeeded" and reset the
        // delay generator.
        delayGenerator.reset();
      }
    }
  };
}
 
Example #15
Source File: MemoryStorageImpl.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void writeKey(final String key, final byte[] value, final Callback<Status> callback) {
  // Need to schedule immediately because C++ locks aren't reentrant, and
  // C++ locking code assumes that this call will not return directly.

  // Schedule the write even if the resources are started since the
  // scheduler will prevent it from running in case the resources have been
  // stopped.
  scheduler.schedule(Scheduler.NO_DELAY,
      new NamedRunnable("MemoryStorage.writeKey") {
    @Override
    public void run() {
      ticlPersistentState.put(key, value);
      callback.accept(Status.newInstance(Status.Code.SUCCESS, ""));
    }
  });
}
 
Example #16
Source File: AndroidResourcesFactory.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void schedule(final int delayMs, final Runnable runnable) {
  // For simplicity, schedule first and then check when the event runs later if the resources
  // have been shut down.
  scheduler.schedule(new NamedRunnable("AndroidScheduler") {
    @Override
    public void run() {
      if (thread != Thread.currentThread()) {
        // Either at initialization or if the thread has been killed or restarted by the
        // Executor service.
        thread = Thread.currentThread();
        thread.setName(threadName);
      }

      if (systemResources.isStarted()) {
        runnable.run();
      } else {
        systemResources.getLogger().warning("Not running on internal thread since resources " +
          "not started %s, %s", delayMs, runnable);
      }
    }
  }, delayMs, TimeUnit.MILLISECONDS);
}
 
Example #17
Source File: AndroidStorage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void readAllKeys(final Callback<SimplePair<Status, String>> keyCallback) {
  scheduler.execute(new NamedRunnable("AndroidStorage.readAllKeys") {
    @Override
    public void run() {
      for (String key : properties.keySet()) {
        keyCallback.accept(SimplePair.of(SUCCESS, key));
      }
    }
  });
}
 
Example #18
Source File: SafeStorage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void readKey(String key, final Callback<SimplePair<Status, byte[]>> done) {
  delegate.readKey(key, new Callback<SimplePair<Status, byte[]>>() {
    @Override
    public void accept(final SimplePair<Status, byte[]> result) {
      scheduler.schedule(NO_DELAY, new NamedRunnable("SafeStorage.readKey") {
        @Override
        public void run() {
          done.accept(result);
        }
      });
    }
  });
}
 
Example #19
Source File: AndroidStorage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void deleteKey(final String key, final Callback<Boolean> done) {
  scheduler.execute(new NamedRunnable("AndroidStorage.deleteKey") {
    @Override
    public void run() {
      properties.remove(key);
      store();
      done.accept(true);
    }
  });
}
 
Example #20
Source File: CheckingInvalidationListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void invalidateUnknownVersion(final InvalidationClient client, final ObjectId objectId,
    final AckHandle ackHandle) {
  Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
  Preconditions.checkNotNull(ackHandle);
  listenerScheduler.schedule(NO_DELAY,
      new NamedRunnable("CheckingInvalListener.invalidateUnknownVersion") {
    @Override
    public void run() {
      statistics.recordListenerEvent(ListenerEventType.INVALIDATE_UNKNOWN);
      delegate.invalidateUnknownVersion(client, objectId, ackHandle);
    }
  });
}
 
Example #21
Source File: SafeStorage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void writeKey(String key, byte[] value, final Callback<Status> done) {
  delegate.writeKey(key, value, new Callback<Status>() {
    @Override
    public void accept(final Status status) {
      scheduler.schedule(NO_DELAY, new NamedRunnable("SafeStorage.writeKey") {
        @Override
        public void run() {
          done.accept(status);
        }
      });
    }
  });
}
 
Example #22
Source File: CheckingInvalidationListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void invalidate(final InvalidationClient client, final Invalidation invalidation,
    final AckHandle ackHandle) {
  Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
  Preconditions.checkNotNull(ackHandle);
  listenerScheduler.schedule(NO_DELAY, new NamedRunnable("CheckingInvalListener.invalidate") {
    @Override
    public void run() {
      statistics.recordListenerEvent(ListenerEventType.INVALIDATE);
      delegate.invalidate(client, invalidation, ackHandle);
    }
  });
}
 
Example #23
Source File: AndroidStorage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void writeKey(final String key, final byte[] value, final Callback<Status> done) {
  scheduler.execute(new NamedRunnable("AndroidStorage.writeKey") {
    @Override
    public void run() {
      properties.put(key, value);
      store();
      done.accept(SUCCESS);
    }
  });
}
 
Example #24
Source File: MemoryStorageImpl.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void readAllKeys(final Callback<SimplePair<Status, String>> done) {
  scheduler.schedule(Scheduler.NO_DELAY,
      new NamedRunnable("MemoryStorage.readAllKeys") {
    @Override
    public void run() {
      Status successStatus = Status.newInstance(Status.Code.SUCCESS, "");
      for (String key : ticlPersistentState.keySet()) {
        done.accept(SimplePair.of(successStatus, key));
      }
      done.accept(null);
    }
  });
}
 
Example #25
Source File: MemoryStorageImpl.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void deleteKey(final String key, final Callback<Boolean> done) {
  scheduler.schedule(Scheduler.NO_DELAY,
      new NamedRunnable("MemoryStorage.deleteKey") {
    @Override
    public void run() {
      TypedUtil.remove(ticlPersistentState, key);
      done.accept(true);
    }
  });
}
 
Example #26
Source File: MemoryStorageImpl.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void deleteKey(final String key, final Callback<Boolean> done) {
  scheduler.schedule(Scheduler.NO_DELAY,
      new NamedRunnable("MemoryStorage.deleteKey") {
    @Override
    public void run() {
      TypedUtil.remove(ticlPersistentState, key);
      done.accept(true);
    }
  });
}
 
Example #27
Source File: MemoryStorageImpl.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void readAllKeys(final Callback<SimplePair<Status, String>> done) {
  scheduler.schedule(Scheduler.NO_DELAY,
      new NamedRunnable("MemoryStorage.readAllKeys") {
    @Override
    public void run() {
      Status successStatus = Status.newInstance(Status.Code.SUCCESS, "");
      for (String key : ticlPersistentState.keySet()) {
        done.accept(SimplePair.of(successStatus, key));
      }
      done.accept(null);
    }
  });
}
 
Example #28
Source File: AndroidStorage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void writeKey(final String key, final byte[] value, final Callback<Status> done) {
  scheduler.execute(new NamedRunnable("AndroidStorage.writeKey") {
    @Override
    public void run() {
      properties.put(key, value);
      store();
      done.accept(SUCCESS);
    }
  });
}
 
Example #29
Source File: CheckingInvalidationListener.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void invalidate(final InvalidationClient client, final Invalidation invalidation,
    final AckHandle ackHandle) {
  Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
  Preconditions.checkNotNull(ackHandle);
  listenerScheduler.schedule(NO_DELAY, new NamedRunnable("CheckingInvalListener.invalidate") {
    @Override
    public void run() {
      statistics.recordListenerEvent(ListenerEventType.INVALIDATE);
      delegate.invalidate(client, invalidation, ackHandle);
    }
  });
}
 
Example #30
Source File: CheckingInvalidationListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void informRegistrationFailure(final InvalidationClient client, final ObjectId objectId,
    final boolean isTransient, final String errorMessage) {
  Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
  listenerScheduler.schedule(NO_DELAY, new NamedRunnable("CheckingInvalListener.regFailure") {
    @Override
    public void run() {
      statistics.recordListenerEvent(ListenerEventType.INFORM_REGISTRATION_FAILURE);
      delegate.informRegistrationFailure(client, objectId, isTransient, errorMessage);
    }
  });
}