java - 如何重试骆驼直达路线一次?
问题描述
使用骆驼,我想发送一个 HTTP 请求,如果失败,请采取一些纠正措施并再次尝试。
发送请求被包装在一个direct
路由中,并且需要生成一个唯一的令牌,除此之外,请求应该完全相同。如果请求第二次失败,它应该以“正常”的方式失败。
我希望在它自己的路由中发送逻辑,因为有时我想在不重试的情况下调用它。
如何使用骆驼 DSL 设置此场景?我尝试了 onException 和 errorHandler 的各种组合,但它似乎甚至没有捕捉到异常。
示例(不起作用):
from("direct:send-request-no-retry")
.setHeader("token").exchange(this::generateToken)
.to("http://example.com");
from("direct:fix-the-error")
// ...
;
from("direct:send-request")
.onException(HttpOperationFailedException.class)
.to("direct:fix-the-error")
.maximumRedeliveries(1)
.useOriginalMessage()
.end()
.to("direct:send-request-no-retry");
解决方案
我主要使用 onException() 子句作为全局异常处理程序。通过快速测试,当直接放入路由时,它确实不会捕获异常。我想到的几个选择是:
第一个选项 do-try 子句:
from("direct:send-request")
.doTry()
.to("direct:send-request-no-retry")
.doCatch(HttpOperationFailedException.class)
.to("direct:fix-the-error")
.to("direct:send-request-no-retry")
.doFinally()
.whatevs()
.endDoTry();
但这有点麻烦,因为您必须从 doCatch 子句再次处理异常。另一种是从“send-request-no-retry”中抛出自定义异常。
from("direct:send-request-no-retry")
.setHeader("token").exchange(this::generateToken)
.doTry()
.to("http://example.com")
.doCatch(HttpOperationFailedException.class)
.throwException(new CustomExceptionThatExtendsRunTimeException())
.doFinally();
onException(CustomExceptionThatExtendsRunTimeException.class)
.handled(true)
.to("direct:fix-the-error")
.maximumRedeliveries(1)
.useOriginalMessage()
.to("direct:send-request-no-retry")
.end();
我没有对此进行测试,所以我不确定这是否会产生无限反馈循环,或者 maximumRedeliveries 是否会切断它。如果是这样,您可以实现另一个“direct:send-request-no-retry”路由,该路由不会抛出 customException,但只是静默失败或抛出 HttpOperationFailedException 并从 onException() 传递请求。或者将 retryAmount 设置为一个标头,并在 onException 处理程序中检查该标头,看看我们是否想再试一次。
customException 的目的是让 onException() 子句从指定类型的每个路由中捕获所有异常。而且我认为您不想将每个失败的 http 请求都传递给您的重试路由。
推荐阅读
- powershell - 如何提取句点、星号和单词之间的字符串
- python - 如何使用网络抓取或 IMDbPY 来获取演员 Instagram 帐户的链接?
- java - 为什么签名指定接口时调用超类方法?
- json - 打印对象中不同条目的键和值
- google-chrome - 如何使用 Chrome DevTools 协议访问 XHR 请求?
- laravel - 普通 Web 和 API 中间件的 Laravel Echo 身份验证
- plot - 牛虻中的水平线
- sql - 部分更新:查询然后更新 SQL 中的一行以确保只更新修改后的值
- github - 如何隐藏一些代码部分并在机器中完整运行
- c++ - MySQL 连接器/C++ 8 X 插件已禁用