Not able to Add CONNECTION Timeout to a CXF WebService with HttpConduit - java

The following code is what i have used to add Connection and Receive timeout to my CXF Web Service,Receive timeout works fine but Connection timeout does not and i think i have a clue why ,the service tries to connect at line1 but the code for setting timeout follows the code,as it always gets stuck at line1 ,how will it ever set a timeout at line2.Please share if there is any other way to set the timeout.
CalculatorService cal = new CalculatorService(); //line1
Calculator port= cal.getCalculatorPort();
Client cl = ClientProxy.getClient(port);
HTTPConduit http = (HTTPConduit)cl.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(5000); //line2
httpClientPolicy.setReceiveTimeout(4000);
http.setClient(httpClientPolicy);
port.add(1, 2);

Related

CXF Soap Client fails to connect on first run

When I create a cxf client to call a SOAP web service it returns connection refused or Unexpected end of file from server. The thing is though, it just happens on the first request, afterwards it's like the client is "warmed up" and actually starts working.
Any idea why this is happening?
...
if(port == null) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setAddress("http://localhost:9000/helloWorld");
port = factory.create(HelloWorld.class);
...
Client client= ClientProxy.getClient(port);
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setAllowChunking(false);
http.setClient(httpClientPolicy);
}
port.someMethod(); // fails on first run, but succeeds on all following runs
...
Removing the if-statement would cause the client to fail on every call not just the first. I'm really stuck and would appreciate any help.
It seems that the issue was caused through TLS 1.2. In Java8 the default TLS version changed from TLSv1 to TLSv1.2. TLS 1.2 didn't seem to work even with the latest version of CXF (3.1.7). So the Solution was to set the TLS version to be used for CXF:
...
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setAllowChunking(false);
httpClientPolicy.setAutoRedirect(true);
httpClientPolicy.setConnection(ConnectionType.KEEP_ALIVE);
String proxyUrl = "http://proxy.com";
String proxyPortString = "8080";
HTTPConduit http = (HTTPConduit)client.getConduit();
SSLContext sslContext = SSLContext.getInstance("TLSv1");
sslContext.init(null, null, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
TLSClientParameters tlsClientParameters = new TLSClientParameters();
tlsClientParameters.setUseHttpsURLConnectionDefaultSslSocketFactory(true);
http.setTlsClientParameters(tlsClientParameters);
http.setClient(httpClientPolicy);

Apache CXF Client proxy settings

I am trying to Develop a Consumer for Soap Service using the tutorial at
http://cxf.apache.org/docs/developing-a-consumer.html
In the section ,"Setting Connection Properties with Contexts" I am looking at the code below
// Set request context property.
java.util.Map<String, Object> requestContext =
((javax.xml.ws.BindingProvider)port).getRequestContext();
requestContext.put(ContextPropertyName, PropertyValue);
// Invoke an operation.
port.SomeOperation();
Can someone tell me if I can set the proxy server settings using the requestContext properties and how ?. My code is running behind a proxy and I need the outgoings SOAP calls to use proxy server settings.
Proxy setting are usually set by using httpconduit object
HelloService hello = new HelloService();
HelloPortType helloPort = cliente.getHelloPort();
org.apache.cxf.endpoint.Client client = ClientProxy.getClient(helloPort);
HTTPConduit http = (HTTPConduit) client.getConduit();
http.getClient().setProxyServer("proxy");
http.getClient().setProxyServerPort(8080);
http.getProxyAuthorization().setUserName("user proxy");
http.getProxyAuthorization().setPassword("password proxy");

How to set timeout to JAX-RS client with CXF

I am working on a Rest Client and I am using CXF with JAX-RS.
The problem that I have is that I cannot find any way to override the default timeout values of the client.
A simple client:
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/MyApp");
target = target.path("jsp/Test.jsp");
Response response = target.request().get();
I have read that there are two timeout properties in CXF called ReceiveTimeout and ConnectionTimeout but I have not managed to find a way to set them in my client.
I have tried client.property("ReceiveTimeout", 5000); but it doesn't work.
I have seen examples of using an xml configuration file to configure the client but I prefer not to take that path if it is possible.
Any ideas?
HTTPConduit conduit = WebClient.getConfig(webClient).getHttpConduit();
conduit.getClient().setConnectionTimeout(1000 * 3);
conduit.getClient().setReceiveTimeout(1000 * 3);
You can find the correct properties in org.apache.cxf.jaxrs.client.spec.ClientImpl:
"http.connection.timeout" and "http.receive.timeout"
So just use them as property when building the client:
ClientBuilder.newClient().property("http.receive.timeout", 1000);
With JAX-RS 2.1 (supported from CXF 3.2) you can use these standard methods in ClientBuilder:
connectTimeout(long timeout, TimeUnit unit);
readTimeout(long timeout, TimeUnit unit);
See also: https://github.com/eclipse-ee4j/jaxrs-api/issues/467
You can try something like this:
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(30000);
http.setClient(httpClientPolicy);
see http://cxf.apache.org/javadoc/latest/org/apache/cxf/transports/http/configuration/HTTPClientPolicy.html

java WebService request timeout

My problem is that I call a remote web service that requires more than 60 seconds to respond and this causes a timeout exception. I do not want any timeout check: I just want the sender to wait until the web service ends. I tried to set:
HttpSession httpSession = getThreadLocalRequest().getSession();
httpSession.setMaxInactiveInterval(120000);
getThreadLocalRequest().setAttribute("session", httpSession);
to modify the web.xml session-timeout (even though I do not think that it is related with my problem) to create a custom HttpRequest. Timeout persists. Is there any way to shutdown this check?
Found the solution:
/* Connect to the service */
ClientProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(MyService.class);
factoryBean.setAddress("service-url");
myService = (MyService) factoryBean.create();
/* Retrive HTTP client policy and set the receive timeout */
Client client = ClientProxy.getClient(myService);
HTTPConduit httpConduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = httpConduit.getClient();
httpClientPolicy.setReceiveTimeout(timeoutMilliseconds);
httpSession.setMaxInactiveInterval is not what you're after.
You probably want to set connectTimeout and readTimeout on URLConnection.
How to do that, depends on what tool you use to call the remote webservice.
Can you add some more details about the service, if it's a SOAP-service, REST-service etc, and what library you use to call the service?

Apache CXF 2.6.6, ReceiveTimeout, new endpoint

Hi I have a problem with change ReceiveTimeout, and setup new endpoint
I have a code:
to change the endpoint I'm using code:
Map<String, Object> prop = ((BindingProvider) port).getRequestContext();
prop.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpoint);
and change timeout :
Client client = ClientProxy.getClient(port);
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = http.getClient();
httpClientPolicy.setReceiveTimeout(5000);
but this is doesn't work
the last part is setup ReceiveTimeout but for default endpoit.
I use server jboss 6.1
How to change ReceiveTimeout for new endpoint ??

Categories