java.lang.UnsupportedOperationException 将连接超时设置为 HttpComponentsMessageSender
java.lang.UnsupportedOperationException when set connection timeout to HttpComponentsMessageSender
我有一个代码可以使用 SOAP 将消息发送到服务器。我的代码如下所示:
private WebServiceTemplate makeTemplate() throws Exception {
WebServiceTemplate localTemplate = new WebServiceTemplate();
Jaxb2Marshaller marshaller = makeMarshaller();
localTemplate.setMarshaller(marshaller);
localTemplate.setUnmarshaller(marshaller);
localTemplate.setMessageSender(httpComponentsMessageSender()); //<-- Add HttpComponentsMessageSender
return localTemplate;
}
我创建了一个 HttpComponentsMessageSender 来为请求添加一个 Trust Store(并添加一个超时)
public HttpComponentsMessageSender httpComponentsMessageSender() throws Exception {
HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
httpComponentsMessageSender.setHttpClient(httpClient());
httpComponentsMessageSender.setConnectionTimeout(30 * 1000);
httpComponentsMessageSender.setReadTimeout(30 * 1000);
return httpComponentsMessageSender;
}
public HttpClient httpClient() throws Exception {
return HttpClientBuilder.create().setSSLSocketFactory(sslConnectionSocketFactory())
.addInterceptorFirst(new RemoveSoapHeadersInterceptor()).build();
}
public SSLConnectionSocketFactory sslConnectionSocketFactory() throws Exception {
// NoopHostnameVerifier essentially turns hostname verification off as otherwise following error
// is thrown: java.security.cert.CertificateException: No name matching localhost found
return new SSLConnectionSocketFactory(sslContext() /*, NoopHostnameVerifier.INSTANCE */);
}
public SSLContext sslContext() throws Exception {
return SSLContextBuilder.create()
.loadTrustMaterial(MyTrustStore, MyPasswd.toCharArray()).build();
}
但是当我使用我的模板向服务器发送消息时,出现此错误:
SEVERE: null
java.lang.UnsupportedOperationException
at org.apache.http.impl.client.InternalHttpClient.getParams(InternalHttpClient.java:211)
at org.springframework.ws.transport.http.HttpComponentsMessageSender.setConnectionTimeout(HttpComponentsMessageSender.java:137)
at
cl.newit.wstgr.main.WStgr.httpComponentsMessageSender(WStgr.java:308)
at cl.newit.wstgr.main.WStgr.makeTemplate(WStgr.java:192)
at cl.newit.wstgr.main.WStgr.genericSend(WStgr.java:165)
at cl.newit.wstgr.main.WStgr.sendM1(WStgr.java:84)
at cl.newit.wstgr.main.tester.TestM1(tester.java:236)
at cl.newit.wstgr.main.tester.main(tester.java:67)
如果我删除带有超时的行:
// httpComponentsMessageSender.setConnectionTimeout(30 * 1000);
// httpComponentsMessageSender.setReadTimeout(30 * 1000);
一切正常。
为什么超时不起作用?
有人可以帮助我吗?
提前致谢。
原因是你在超时前设置了httpClient
,按这个顺序
httpComponentsMessageSender.setReadTimeout(3*1000);
httpComponentsMessageSender.setConnectionTimeout(3*1000);
httpComponentsMessageSender.setHttpClient(httpClient());
我有一个代码可以使用 SOAP 将消息发送到服务器。我的代码如下所示:
private WebServiceTemplate makeTemplate() throws Exception {
WebServiceTemplate localTemplate = new WebServiceTemplate();
Jaxb2Marshaller marshaller = makeMarshaller();
localTemplate.setMarshaller(marshaller);
localTemplate.setUnmarshaller(marshaller);
localTemplate.setMessageSender(httpComponentsMessageSender()); //<-- Add HttpComponentsMessageSender
return localTemplate;
}
我创建了一个 HttpComponentsMessageSender 来为请求添加一个 Trust Store(并添加一个超时)
public HttpComponentsMessageSender httpComponentsMessageSender() throws Exception {
HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
httpComponentsMessageSender.setHttpClient(httpClient());
httpComponentsMessageSender.setConnectionTimeout(30 * 1000);
httpComponentsMessageSender.setReadTimeout(30 * 1000);
return httpComponentsMessageSender;
}
public HttpClient httpClient() throws Exception {
return HttpClientBuilder.create().setSSLSocketFactory(sslConnectionSocketFactory())
.addInterceptorFirst(new RemoveSoapHeadersInterceptor()).build();
}
public SSLConnectionSocketFactory sslConnectionSocketFactory() throws Exception {
// NoopHostnameVerifier essentially turns hostname verification off as otherwise following error
// is thrown: java.security.cert.CertificateException: No name matching localhost found
return new SSLConnectionSocketFactory(sslContext() /*, NoopHostnameVerifier.INSTANCE */);
}
public SSLContext sslContext() throws Exception {
return SSLContextBuilder.create()
.loadTrustMaterial(MyTrustStore, MyPasswd.toCharArray()).build();
}
但是当我使用我的模板向服务器发送消息时,出现此错误:
SEVERE: null
java.lang.UnsupportedOperationException
at org.apache.http.impl.client.InternalHttpClient.getParams(InternalHttpClient.java:211)
at org.springframework.ws.transport.http.HttpComponentsMessageSender.setConnectionTimeout(HttpComponentsMessageSender.java:137)
at
cl.newit.wstgr.main.WStgr.httpComponentsMessageSender(WStgr.java:308)
at cl.newit.wstgr.main.WStgr.makeTemplate(WStgr.java:192)
at cl.newit.wstgr.main.WStgr.genericSend(WStgr.java:165)
at cl.newit.wstgr.main.WStgr.sendM1(WStgr.java:84)
at cl.newit.wstgr.main.tester.TestM1(tester.java:236)
at cl.newit.wstgr.main.tester.main(tester.java:67)
如果我删除带有超时的行:
// httpComponentsMessageSender.setConnectionTimeout(30 * 1000);
// httpComponentsMessageSender.setReadTimeout(30 * 1000);
一切正常。 为什么超时不起作用? 有人可以帮助我吗? 提前致谢。
原因是你在超时前设置了httpClient
,按这个顺序
httpComponentsMessageSender.setReadTimeout(3*1000);
httpComponentsMessageSender.setConnectionTimeout(3*1000);
httpComponentsMessageSender.setHttpClient(httpClient());