I'm trying to set timeout for webServiceTemplate, but when I execute marshalSendAndReceive()
the timeout ignores. Request takes about 5 second, but in coinfiguration I have set 5 millis. I tried to use HttpComponentsMessageSender
instead of HttpWebServiceMessageSenderBuilder
, but it also has no effect. So is there any way to solve this problem?
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan(applicationProps.getPackagesToScan());
return marshaller;
}
@Bean
public WebServiceTemplate webServiceTemplate(final WebServiceTemplateBuilder builder) {
return builder.setMarshaller(marshaller()).setUnmarshaller(marshaller())
.messageSenders(List.of(
new BasicAuthHttpsConnectionMessageSender(props.getUsername(), props.getPassword()),
new HttpWebServiceMessageSenderBuilder()
.setConnectTimeout(Duration.ofMillis(5))
.setReadTimeout(Duration.ofMillis(5)).build()
)).build();
}
CodePudding user response:
Here,
new HttpWebServiceMessageSenderBuilder()
.setConnectTimeout(Duration.ofMillis(5))
.setReadTimeout(Duration.ofMillis(5)).build();
You need to provide a large time duration i.e. Duration.ofMillis(500)
now you can see the effect. So it is like
new HttpWebServiceMessageSenderBuilder()
.setConnectTimeout(Duration.ofMillis(500))
.setReadTimeout(Duration.ofMillis(500)).build();
The Duration of 5 millis
is very short so you can't notice it.
See here
CodePudding user response:
Instead of using Duration.ofMillis(5) just use setConnectTimeout(5)
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan(applicationProps.getPackagesToScan());
return marshaller;
}
@Bean
public WebServiceTemplate webServiceTemplate(final WebServiceTemplateBuilder
builder) {
return builder.setMarshaller(marshaller()).setUnmarshaller(marshaller())
.messageSenders(List.of(
new BasicAuthHttpsConnectionMessageSender(props.getUsername(), props.getPassword()),
new HttpWebServiceMessageSenderBuilder()
.setConnectTimeout(5)
.setReadTimeout(5).build()
)).build();
}