这是 HystrixDemo 中的一个示例,可能会有所帮助。
public void executeSimulatedUserRequestForOrderConfirmationAndCreditCardPayment() throws InterruptedException, ExecutionException {
/* fetch user object with http cookies */
UserAccount user = new GetUserAccountCommand(new HttpCookie("mockKey", "mockValueFromHttpRequest")).execute();
/* fetch the payment information (asynchronously) for the user so the credit card payment can proceed */
Future<PaymentInformation> paymentInformation = new GetPaymentInformationCommand(user).queue();
/* fetch the order we're processing for the user */
int orderIdFromRequestArgument = 13579;
Order previouslySavedOrder = new GetOrderCommand(orderIdFromRequestArgument).execute();
CreditCardCommand credit = new CreditCardCommand(previouslySavedOrder, paymentInformation.get(), new BigDecimal(123.45));
credit.execute();
}
此方法有五个命令,其中一些命令相互依赖,但如果其中任何一个命令失败,则该方法将退出并出现异常。
我希望这有帮助。