温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在JAVA中实现工作流

发布时间:2021-06-04 17:10:24 来源:亿速云 阅读:420 作者:Leah 栏目:编程语言

本篇文章为大家展示了如何在JAVA中实现工作流,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

1、Apache Commons Chain 中的角色有:chain、context、command。

如何在JAVA中实现工作流

2、在我们订单系统有这样的业务,就是退票的时候,会根据核损后的订单价格,给客人退钱,但是订单的金额,由几部分组成

有现金、商旅卡、有优惠券。所以根据需求,我们需要一个工作流来走下退款流程,我们的流程流转的步骤是这样的:

先退商旅卡-----如果还有余额退现金-----------还有余额再退优惠券,分析一下这样的需求,刚好可以用这个工具,直接上代码了

先引入包

 <dependency>
      <groupId>commons-chain</groupId>
      <artifactId>commons-chain</artifactId>
      <version>1.2</version>
    </dependency>

编写command

/**
 * 退商旅卡Cash
 * Created by 一代天骄 on 2018/7/1.
 */
@Slf4j
public class RefundBusinessCardCommand implements Command{
  public boolean execute(Context context) throws Exception {
    RefundContext refundContext = (RefundContext) context;
    log.info("orderId:{} 退款开始,第一步:退商旅卡,金额:{}",refundContext.getOrderId(),"10");
    return false;
  }
}
/**
 * 退现金
 * Created by 一代天骄 on 2018/7/1.
 */
@Slf4j
public class RefundCashCommand implements Command {
 
  public boolean execute(Context context) throws Exception {
    RefundContext refundContext = (RefundContext) context;
    log.info("orderId:{}退款开始,第二步:退现金,金额:{}",refundContext.getOrderId(),"5");
    return false;
  }
}
/**
 * 退优惠券
 * Created by 一代天骄 on 2018/7/1.
 */
@Slf4j
public class RefundPromotionCommand implements Command{
 
 
  public boolean execute(Context context) throws Exception {
    RefundContext refundContext = (RefundContext) context;
    log.info("orderId:{} 退款开始,第二步:退优惠券,金额:{}",refundContext.getOrderId(),"20");
    return false;
  }
}
/**
 * Created by 一代天骄 on 2018/7/1.
 */
@Data
public class RefundContext extends ContextBase {
 
  /**
   * 订单号
   */
  private Integer orderId;
 
 
}
/**
 *
 * 退票的工作流实现
 * Created by 一代天骄 on 2018/7/1.
 */
public class RefundTicketChain extends ChainBase {
 
  public void init() {
    //退商旅卡
    this.addCommand(new RefundBusinessCardCommand());
    //退现金
    this.addCommand(new RefundCashCommand());
    //退优惠券
    this.addCommand(new RefundPromotionCommand());
  }
 
 
  public static void main(String[] args) throws Exception {
    RefundTicketChain refundTicketChain = new RefundTicketChain();
    refundTicketChain.init();
    RefundContext context = new RefundContext();
    context.setOrderId(1621940242);
    refundTicketChain.execute(context);
  }
}

上述内容就是如何在JAVA中实现工作流,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI