范围100到299的HTTP响应代码在Camel中被视为成功响应。我的要求是,如果响应代码不是200,那么将其视为失败响应。我试图使用自定义负载均衡器来实现这一目标。问题是当处理交换时,如果响应代码是299,那么对于Camel来说,它是成功的并且它离开了customLoadBalancer。我需要一种方法来添加自定义故障转移,我可以在其中检查响应头值并基于该路由到不同的端点。
<route id="client_http" errorHandlerRef="noErrorHandler">
<from uri="direct:test"/>
<loadBalance inheritErrorHandler="false">
<custom ref="customLoadBalancer" />
<!-- TODO configurable failover attempts -->
<to ref="http4.1.to"/>
<to ref="http4.2.to"/>
<to ref="http4.3.to"/>
</loadBalance>
</route>
@Component
public class CustomLoadBalancer extends LoadBalancerSupport {
public boolean process(Exchange exchange, AsyncCallback callback) {
Logger log = Logger.getLogger(CustomLoadBalancer.class);
String body = exchange.getIn().getBody(String.class);
log.debug("Headers " + exchange.getIn().getHeaders()); //null because exchange is not processed
getProcessors().get(0).process(exchange);
log.debug("Headers " + exchange.getIn().getHeaders()); //line never executes because HTTP response was 299 and Camel considered that as success.
}