コード例 #1
0
 /**
  * Submits or schedules an {@link ApplicationTask} on the the shared executor service.
  *
  * @param <T> The task result class
  * @param applicationName The application's full name, or null to default to current application's
  *     name
  * @param code The code to execute
  * @param context The context made available to the task
  * @param delay Initial delay in milliseconds, or zero for ASAP
  * @param repeatEvery Repeat delay in milliseconds, or zero for no repetition
  * @param fixedRepeat Whether repetitions are at fixed times, or if the repeat delay begins when
  *     the task ends
  * @return A future for the task
  * @see #getExecutor()
  */
 public <T> Future<T> codeTask(
     String applicationName,
     String code,
     Object context,
     int delay,
     int repeatEvery,
     boolean fixedRepeat) {
   Application application =
       applicationName == null ? this.application : InstanceUtil.getApplication(applicationName);
   ApplicationTask<T> task = new ApplicationTask<T>(application, code, context);
   return task(task, delay, repeatEvery, fixedRepeat);
 }
コード例 #2
0
  /**
   * Gets the shared executor service, creating one if it doesn't exist.
   *
   * <p>If shared globals are not set up, gets the application's executor service.
   *
   * <p>This setting can be configured by setting an attribute named <code>
   * com.threecrickets.prudence.executor</code> in the component's {@link Context}.
   *
   * @return The executor service
   */
  public ExecutorService getExecutor() {
    if (executor == null) {
      ConcurrentMap<String, Object> attributes = getSharedGlobals();

      if (attributes != null) {
        executor = (ExecutorService) attributes.get(InstanceUtil.EXECUTOR_ATTRIBUTE);

        if (executor == null) {
          executor =
              Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() * 2 + 1);

          ExecutorService existing =
              (ExecutorService) attributes.putIfAbsent(InstanceUtil.EXECUTOR_ATTRIBUTE, executor);
          if (existing != null) executor = existing;
        }
      } else executor = InstanceUtil.getComponent().getTaskService();
    }

    return executor;
  }