All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.linkedin.playparseq.j.PlayParSeqImpl Maven / Gradle / Ivy

/*
 * Copyright 2015 LinkedIn Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */
package com.linkedin.playparseq.j;

import com.linkedin.parseq.Engine;
import com.linkedin.parseq.Task;
import com.linkedin.parseq.promise.Promises;
import com.linkedin.parseq.promise.SettablePromise;
import com.linkedin.playparseq.j.stores.ParSeqTaskStore;
import com.linkedin.playparseq.utils.PlayParSeqHelper;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionStage;
import javax.inject.Inject;
import javax.inject.Singleton;
import play.mvc.Http;


/**
 * The class PlayParSeqImpl is an implementation of the interface {@link PlayParSeq} with the help from the class
 * {@link PlayParSeqHelper}.
 *
 * @author Yinan Ding ([email protected])
 */
@Singleton
public class PlayParSeqImpl extends PlayParSeqHelper implements PlayParSeq {

  /**
   * The field DEFAULT_TASK_NAME is the default name of ParSeq Task.
   */
  public final static String DEFAULT_TASK_NAME = "fromPlayCompletionStage";

  /**
   * The field _engine is a ParSeq Engine for running ParSeq Task.
   */
  private final Engine _engine;

  /**
   * The field _parSeqTaskStore is a {@link ParSeqTaskStore} for storing ParSeq Tasks.
   */
  private final ParSeqTaskStore _parSeqTaskStore;

  /**
   * The constructor injects the ParSeq Engine and the {@link ParSeqTaskStore}.
   *
   * @param engine The injected ParSeq Engine component
   * @param parSeqTaskStore The injected {@link ParSeqTaskStore} component
   */
  @Inject
  public PlayParSeqImpl(final Engine engine, final ParSeqTaskStore parSeqTaskStore) {
    _engine = engine;
    _parSeqTaskStore = parSeqTaskStore;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public  Task toTask(final String name, final Callable> f) {
    // Bind a Task to the CompletionStage for both success and failure
    return Task.async(name, () -> {
      SettablePromise promise = Promises.settable();
      f.call().whenCompleteAsync((result, exception) -> {
        if (exception != null) {
          promise.fail(exception);
        } else {
          promise.done(result);
        }
      });
      return promise;
    });
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public  Task toTask(final Callable> f) {
    return toTask(DEFAULT_TASK_NAME, f);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public  CompletionStage runTask(final Http.Context context, final Task task) {
    // Bind a CompletionStage to the ParSeq Task
    CompletionStage completionStage = bindTaskToCompletionStage(task);
    // Put the ParSeq Task into store
    _parSeqTaskStore.put(context, task);
    // Run the ParSeq Task
    _engine.run(task);
    // Return the CompletionStage
    return completionStage;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy