【问题标题】:Converting Promise to different type even if result not yet available即使结果尚不可用,也将 Promise 转换为不同的类型
【发布时间】:2017-06-26 07:22:31
【问题描述】:

我有以下活动:

@ActivityRegistrationOptions(defaultTaskScheduleToStartTimeoutSeconds = 300, defaultTaskStartToCloseTimeoutSeconds = 10)
@Activities(version="1.0")
public interface MyActivities {

    B first(A a) throws Exception;

    void second(C c) throws Exception;

}

我有以下工作流程:

public class MyWorkflowImpl implements MyWorkflow {

    @Autowired
    private MyActivitiesClient operations;

    @Override
    public void start(SomeType input) {
        A a = new A(...);

        Promise<B> b = operations.first(a);

        Promise<C> c = ...;
        /* Here, I would like to create c based on b */

        operations.second(c);
    }
}

现在b 在第一个操作完成之前不可用,但即使b 不可用,工作流也会继续。

有什么想法吗?

【问题讨论】:

    标签: java promise amazon-swf


    【解决方案1】:

    对方法使用@Asynchronous注解:

    public void start(SomeType input) {
        A a = new A(...);
    
        Promise<B> b = operations.first(a);
    
        Promise<C> c = ...;
    
        operations.second(c);
    }
    
    @Asynchronous
    private Promise<C> calculateC(Promise<B> b) {
        Settable<C> result = new Settable<C>();
               /* Here create c based on b */
        result.set(b.get()....);
        return result;
    }
    

    注解为@Asynchronous 的方法在内部转换为回调,当所有Promise 类型的参数都准备好时调用该回调。 @Asynchronous 实现依赖于 AspecJ,因此请务必仔细按照设置说明启用它。

    另一种选择是使用任务:

    @Override
    public void start(SomeType input) {
        A a = new A(...);
    
        Promise<B> b = operations.first(a);
    
        Promise<C> c = new Settable<C>();
        new Task(b) {
            protected void doExecute() {
                /* Here c based on b */
                c.set(b.get() ....);
            }
        }
        operations.second(c);
    }
    

    当传递给Task构造函数的Promise类型的所有参数都准备好时,调用Task doExecute方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 2013-03-28
      • 2020-01-19
      相关资源
      最近更新 更多