You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Alejandro Molinari <al...@gmail.com> on 2021/09/23 22:12:14 UTC

Stop cxf endpoint client request at specified time

Hello! I have a simple client to connect a web service destination:

		//Obtengo el acceso al servicio.
		final WsTransaccionOnlineReceptorImplService wsTOLReceptorImplService = new WsTransaccionOnlineReceptorImplService(wsdlURL, SERVICE_NAME);
		//Obtengo el port donde estan definidas las operaciones.
		final WsTransaccionOnlineReceptor port = wsTOLReceptorImplService.getWsTransaccionOnlineReceptorPort(); 
		
		//Asigno los timeouts correspondientes.
		final Client cliente = ClientProxy.getClient(port);
		final HTTPConduit http = (HTTPConduit) cliente.getConduit();
		final HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
		//Timeout para la conexion (milisegundos).
		httpClientPolicy.setConnectionTimeout(3000);
		//Timeout de recepcion de datos(milisegundos).
		httpClientPolicy.setReceiveTimeout(10000);		 
		http.setClient(httpClientPolicy);		

		final RespuestaType resp=port.aprobacionTransaccion(tol);
 
The ReceiveTimeout works as expected, when no data arrives in the specified ReceiveTimeout a java.net.SocketTimeoutException: Read timed out arises.
In some cases when there is network problems or may be the response is in "chunked transfer encoding" and there is delay between chunks, the response takes minutes to arrive and no exception is thrown (because data is incoming, but slowly).
This client is embedded in a JAX-WS web service and must return a response to the caller in certain amount of time and obviously I need an extra timeout, let's say a "ResponseTimeout".
I tried to start a thread and sleep "ResponseTimeout", after that, call a cliente.close() if response not arrived completely but has no effect in blocking mode and i think is not the adequate solution.
Should I use asynchronous  web services to achieve this?
Any suggestion will be appreciated!!
Thanks!


Re: Stop cxf endpoint client request at specified time

Posted by Alejandro Molinari <al...@gmail.com>.
Works perfectly.
Thanks again for your advice Mark!!


On 2021/09/24 14:55:22, Alejandro Molinari <al...@gmail.com> wrote: 
> Good point Mark!
> I'll follow that idea, put the client in a Callable task with the appropriate timeout in the ExecutorService.
> Thanks very much!
> 
> On 2021/09/23 22:23:46, Mark Presling <ma...@argonaut.nz> wrote: 
> > We have had this problem in the past also. Have a look at
> > https://www.baeldung.com/java-executor-service-tutorial
> > 
> > In particular, section 5. You can submit() a Callable task and then use the
> > Future to wait for it to complete, with a timeout...
> > 
> > String result = future.get(20, TimeUnit.SECONDS);
> > 
> > Here, if it takes longer than 20s it will throw an exception and you can
> > handle it as required.
> > 
> > 
> > 
> > On Fri, 24 Sept 2021 at 10:12, Alejandro Molinari <al...@gmail.com>
> > wrote:
> > 
> > > Hello! I have a simple client to connect a web service destination:
> > >
> > >                 //Obtengo el acceso al servicio.
> > >                 final WsTransaccionOnlineReceptorImplService
> > > wsTOLReceptorImplService = new
> > > WsTransaccionOnlineReceptorImplService(wsdlURL, SERVICE_NAME);
> > >                 //Obtengo el port donde estan definidas las operaciones.
> > >                 final WsTransaccionOnlineReceptor port =
> > > wsTOLReceptorImplService.getWsTransaccionOnlineReceptorPort();
> > >
> > >                 //Asigno los timeouts correspondientes.
> > >                 final Client cliente = ClientProxy.getClient(port);
> > >                 final HTTPConduit http = (HTTPConduit)
> > > cliente.getConduit();
> > >                 final HTTPClientPolicy httpClientPolicy = new
> > > HTTPClientPolicy();
> > >                 //Timeout para la conexion (milisegundos).
> > >                 httpClientPolicy.setConnectionTimeout(3000);
> > >                 //Timeout de recepcion de datos(milisegundos).
> > >                 httpClientPolicy.setReceiveTimeout(10000);
> > >                 http.setClient(httpClientPolicy);
> > >
> > >                 final RespuestaType resp=port.aprobacionTransaccion(tol);
> > >
> > > The ReceiveTimeout works as expected, when no data arrives in the
> > > specified ReceiveTimeout a java.net.SocketTimeoutException: Read timed
> > > out arises.
> > > In some cases when there is network problems or may be the response is in
> > > "chunked transfer encoding" and there is delay between chunks, the response
> > > takes minutes to arrive and no exception is thrown (because data is
> > > incoming, but slowly).
> > > This client is embedded in a JAX-WS web service and must return a response
> > > to the caller in certain amount of time and obviously I need an extra
> > > timeout, let's say a "ResponseTimeout".
> > > I tried to start a thread and sleep "ResponseTimeout", after that, call a
> > > cliente.close() if response not arrived completely but has no effect in
> > > blocking mode and i think is not the adequate solution.
> > > Should I use asynchronous  web services to achieve this?
> > > Any suggestion will be appreciated!!
> > > Thanks!
> > >
> > >
> > 
> 

Re: Stop cxf endpoint client request at specified time

Posted by Alejandro Molinari <al...@gmail.com>.
Good point Mark!
I'll follow that idea, put the client in a Callable task with the appropriate timeout in the ExecutorService.
Thanks very much!

On 2021/09/23 22:23:46, Mark Presling <ma...@argonaut.nz> wrote: 
> We have had this problem in the past also. Have a look at
> https://www.baeldung.com/java-executor-service-tutorial
> 
> In particular, section 5. You can submit() a Callable task and then use the
> Future to wait for it to complete, with a timeout...
> 
> String result = future.get(20, TimeUnit.SECONDS);
> 
> Here, if it takes longer than 20s it will throw an exception and you can
> handle it as required.
> 
> 
> 
> On Fri, 24 Sept 2021 at 10:12, Alejandro Molinari <al...@gmail.com>
> wrote:
> 
> > Hello! I have a simple client to connect a web service destination:
> >
> >                 //Obtengo el acceso al servicio.
> >                 final WsTransaccionOnlineReceptorImplService
> > wsTOLReceptorImplService = new
> > WsTransaccionOnlineReceptorImplService(wsdlURL, SERVICE_NAME);
> >                 //Obtengo el port donde estan definidas las operaciones.
> >                 final WsTransaccionOnlineReceptor port =
> > wsTOLReceptorImplService.getWsTransaccionOnlineReceptorPort();
> >
> >                 //Asigno los timeouts correspondientes.
> >                 final Client cliente = ClientProxy.getClient(port);
> >                 final HTTPConduit http = (HTTPConduit)
> > cliente.getConduit();
> >                 final HTTPClientPolicy httpClientPolicy = new
> > HTTPClientPolicy();
> >                 //Timeout para la conexion (milisegundos).
> >                 httpClientPolicy.setConnectionTimeout(3000);
> >                 //Timeout de recepcion de datos(milisegundos).
> >                 httpClientPolicy.setReceiveTimeout(10000);
> >                 http.setClient(httpClientPolicy);
> >
> >                 final RespuestaType resp=port.aprobacionTransaccion(tol);
> >
> > The ReceiveTimeout works as expected, when no data arrives in the
> > specified ReceiveTimeout a java.net.SocketTimeoutException: Read timed
> > out arises.
> > In some cases when there is network problems or may be the response is in
> > "chunked transfer encoding" and there is delay between chunks, the response
> > takes minutes to arrive and no exception is thrown (because data is
> > incoming, but slowly).
> > This client is embedded in a JAX-WS web service and must return a response
> > to the caller in certain amount of time and obviously I need an extra
> > timeout, let's say a "ResponseTimeout".
> > I tried to start a thread and sleep "ResponseTimeout", after that, call a
> > cliente.close() if response not arrived completely but has no effect in
> > blocking mode and i think is not the adequate solution.
> > Should I use asynchronous  web services to achieve this?
> > Any suggestion will be appreciated!!
> > Thanks!
> >
> >
> 

Re: Stop cxf endpoint client request at specified time

Posted by Mark Presling <ma...@argonaut.nz>.
We have had this problem in the past also. Have a look at
https://www.baeldung.com/java-executor-service-tutorial

In particular, section 5. You can submit() a Callable task and then use the
Future to wait for it to complete, with a timeout...

String result = future.get(20, TimeUnit.SECONDS);

Here, if it takes longer than 20s it will throw an exception and you can
handle it as required.



On Fri, 24 Sept 2021 at 10:12, Alejandro Molinari <al...@gmail.com>
wrote:

> Hello! I have a simple client to connect a web service destination:
>
>                 //Obtengo el acceso al servicio.
>                 final WsTransaccionOnlineReceptorImplService
> wsTOLReceptorImplService = new
> WsTransaccionOnlineReceptorImplService(wsdlURL, SERVICE_NAME);
>                 //Obtengo el port donde estan definidas las operaciones.
>                 final WsTransaccionOnlineReceptor port =
> wsTOLReceptorImplService.getWsTransaccionOnlineReceptorPort();
>
>                 //Asigno los timeouts correspondientes.
>                 final Client cliente = ClientProxy.getClient(port);
>                 final HTTPConduit http = (HTTPConduit)
> cliente.getConduit();
>                 final HTTPClientPolicy httpClientPolicy = new
> HTTPClientPolicy();
>                 //Timeout para la conexion (milisegundos).
>                 httpClientPolicy.setConnectionTimeout(3000);
>                 //Timeout de recepcion de datos(milisegundos).
>                 httpClientPolicy.setReceiveTimeout(10000);
>                 http.setClient(httpClientPolicy);
>
>                 final RespuestaType resp=port.aprobacionTransaccion(tol);
>
> The ReceiveTimeout works as expected, when no data arrives in the
> specified ReceiveTimeout a java.net.SocketTimeoutException: Read timed
> out arises.
> In some cases when there is network problems or may be the response is in
> "chunked transfer encoding" and there is delay between chunks, the response
> takes minutes to arrive and no exception is thrown (because data is
> incoming, but slowly).
> This client is embedded in a JAX-WS web service and must return a response
> to the caller in certain amount of time and obviously I need an extra
> timeout, let's say a "ResponseTimeout".
> I tried to start a thread and sleep "ResponseTimeout", after that, call a
> cliente.close() if response not arrived completely but has no effect in
> blocking mode and i think is not the adequate solution.
> Should I use asynchronous  web services to achieve this?
> Any suggestion will be appreciated!!
> Thanks!
>
>