您好,登錄后才能下訂單哦!
本篇文章為大家展示了如何在JAVA中實現(xiàn)工作流,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
1、Apache Commons Chain 中的角色有:chain、context、command。
2、在我們訂單系統(tǒng)有這樣的業(yè)務(wù),就是退票的時候,會根據(jù)核損后的訂單價格,給客人退錢,但是訂單的金額,由幾部分組成
有現(xiàn)金、商旅卡、有優(yōu)惠券。所以根據(jù)需求,我們需要一個工作流來走下退款流程,我們的流程流轉(zhuǎn)的步驟是這樣的:
先退商旅卡-----如果還有余額退現(xiàn)金-----------還有余額再退優(yōu)惠券,分析一下這樣的需求,剛好可以用這個工具,直接上代碼了
先引入包
<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; } }
/** * 退現(xiàn)金 * 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:{}退款開始,第二步:退現(xiàn)金,金額:{}",refundContext.getOrderId(),"5"); return false; } }
/** * 退優(yōu)惠券 * 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:{} 退款開始,第二步:退優(yōu)惠券,金額:{}",refundContext.getOrderId(),"20"); return false; } }
/** * Created by 一代天驕 on 2018/7/1. */ @Data public class RefundContext extends ContextBase { /** * 訂單號 */ private Integer orderId; }
/** * * 退票的工作流實現(xiàn) * Created by 一代天驕 on 2018/7/1. */ public class RefundTicketChain extends ChainBase { public void init() { //退商旅卡 this.addCommand(new RefundBusinessCardCommand()); //退現(xiàn)金 this.addCommand(new RefundCashCommand()); //退優(yōu)惠券 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); } }
上述內(nèi)容就是如何在JAVA中實現(xiàn)工作流,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。