本篇文章為大家展示了如何在JAVA中實現工作流,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
1、Apache Commons Chain 中的角色有:chain、context、command。

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中實現工作流,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。