使用 Java DSL 中的 RequestConfig 在 Camel 中将套接字超时添加到 http4 客户端
Adding socket timeout to http4 client using RequestConfig in Java DSL in Camel
所以,我有一个 route
基本上看起来像这样-
from("direct:send_success")
.to("http4://localhost:8089/mock/success?httpClient.socketTimeout=1000");
使用这种方式,我能够成功应用 1 秒的套接字超时。我正在使用 ProducerTemplate
来调用这条路线。这一切都很好。但是当我将路线更改为-
from("direct:send_success")
.to("http4://localhost:8089/mock/success");
并将调用路由到-
ProducerTemplate pt = ctx.createProducerTemplate();
Exchange ex = pt.send("direct:send_success", exOb -> {
HttpComponent httpComp = exOb.getContext().getComponent("http4", HttpComponent.class);
exOb.getContext().getComponent("http4", HttpComponent.class).setHttpClientConfigurer(httpClientBuilder -> {
HttpClientBuilder
.create()
.setDefaultRequestConfig(requestConfigWithTimeout(1000))
.build();
});
});
而方法requestConfigWithTimeout()
为-
private static RequestConfig requestConfigWithTimeout(int timeoutInMilliseconds) {
return RequestConfig.copy(RequestConfig.DEFAULT)
.setSocketTimeout(timeoutInMilliseconds)
.build();
}
不应用超时设置。我哪里错了?
在创建并启动 Camel 路由后您无法更改 http 组件。然后路由是用 http4 组件创建的,它的配置没有用你做的额外代码改变。
因此请尽早配置 http4 组件,而不是在发送消息时配置。
所以,我有一个 route
基本上看起来像这样-
from("direct:send_success")
.to("http4://localhost:8089/mock/success?httpClient.socketTimeout=1000");
使用这种方式,我能够成功应用 1 秒的套接字超时。我正在使用 ProducerTemplate
来调用这条路线。这一切都很好。但是当我将路线更改为-
from("direct:send_success")
.to("http4://localhost:8089/mock/success");
并将调用路由到-
ProducerTemplate pt = ctx.createProducerTemplate();
Exchange ex = pt.send("direct:send_success", exOb -> {
HttpComponent httpComp = exOb.getContext().getComponent("http4", HttpComponent.class);
exOb.getContext().getComponent("http4", HttpComponent.class).setHttpClientConfigurer(httpClientBuilder -> {
HttpClientBuilder
.create()
.setDefaultRequestConfig(requestConfigWithTimeout(1000))
.build();
});
});
而方法requestConfigWithTimeout()
为-
private static RequestConfig requestConfigWithTimeout(int timeoutInMilliseconds) {
return RequestConfig.copy(RequestConfig.DEFAULT)
.setSocketTimeout(timeoutInMilliseconds)
.build();
}
不应用超时设置。我哪里错了?
在创建并启动 Camel 路由后您无法更改 http 组件。然后路由是用 http4 组件创建的,它的配置没有用你做的额外代码改变。
因此请尽早配置 http4 组件,而不是在发送消息时配置。