2017-02-28 118 views
0
在Java中使用DSL RequestConfig http4客戶

所以,我有一個route基本上看起來像這 -添加套接字超時在駱駝

from("direct:send_success") 
     .to("http4://localhost:8089/mock/success?httpClient.socketTimeout=1000"); 

使用這種方式,我能申請1秒的套接字超時成功。我正在使用ProducerTemplate來調用此路線。這很好。但是,當我改變TO-

from("direct:send_success") 
    .to("http4://localhost:8089/mock/success"); 

的路由和路由調用TO-

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(); 
} 

將不會應用這些超時設置的方法。我哪裏錯了?

回答

2

在創建並啓動駱駝路線之後,您不能更改之後的http組件。然後,路由是使用http4組件創建的,該組件沒有將其配置更改爲額外的代碼,您可以使用

因此,先配置http4組件,而不是在發送消息時。

+0

好的。我可以在請求頭中使用cxf的'ClientPolicy'對SOAP端點做同樣的事情,所以我想這裏也有一個解決方法。無論如何,感謝您的幫助。 – Abhishek