You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by MattJax <ma...@gmail.com> on 2008/09/24 10:10:14 UTC

Resource management and cleanup

Hi,

I have a servlet-based application which uses CXF as a client to call a
remote web service.

I am using the following code to create my webservice object within the
servlet code:

<snippet>
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.setServiceClass(WebServiceInterface.class);
    factory.setAddress(wsConfig.getWSDLUrl());
    webService = (WebServiceInterface) factory.create();
</snippet>

I then call methods (usually just one) on the webService object, and then
the servlet thread completes normally and I do not do anything explicitly to
close this webService object.

My question is: should I be closing this webService object explicitly?  It
this tying up resources?

Also, is the JaxWsProxyFactoryBean thread safe?  I.e. could I have an
initialised JaxWsProxyFactoryBean object as a static member of the servlet I
am using?

Regards,

Matt
-- 
View this message in context: http://www.nabble.com/Resource-management-and-cleanup-tp19644012p19644012.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Resource management and cleanup

Posted by Daniel Kulp <dk...@apache.org>.

Matt,

Any chance you can cobble together a small example using the hello world 
things or similar to show this leak?    If we can reproduce it easily, we can 
debug into it.

Also, what version of CXF?

Dan


On Monday 13 October 2008 9:38:30 am MattJax wrote:
> Hi Ulhas,
>
> Thanks for your last post.
>
> I've removed the Bus creation to a static member of the Handler class I am
> using.  I've got a monitor thread which kicks in every 2 minutes and acts
> as a heartbeat to a WS.  This basically boils down to creating a new
> instance of the following class and then calling 'process' :
>
> public class WSEnabledHandler {
>   private static Bus bus = null;
>   private IDSWSPortType webService = null;
>
>   public void process() {
>      createPortService();
>
>     // call the WS
>     webService.authenticate();
>
>     endProcess();
>   }
>
>   private void createPortService() {
> 	if(bus == null) {
> 		bus = BusFactory.newInstance().creaouteBus();
> 		BusFactory.setDefaultBus(bus);
> 		BusFactory.setThreadDefaultBus(bus);
> 	}
> 	JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
> 	factory.setBus(bus);
> 	factory.setServiceClass(IDSWSPortType.class);
> 	factory.setAddress(wsConfig.getWSDLUrl());
> 	webService = (IDSWSPortType) factory.create();
>
> 	// turn chunking off
> 	Client client = ClientProxy.getClient(webService);
> 	HTTPConduit httpConduit = (HTTPConduit) client.getConduit();
> 	HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
> 	httpClientPolicy.setAllowChunking(false);
> 	httpConduit.setClient(httpClientPolicy);
>   }
>   private void endProcess() {
>         // does nothing, but should it?
>   }
> }
>
> I'm getting a slow leak on memory - roughly 0.7Mb per call.  With this
> background thread, together with user activity (which does similar to
> above), we're quite quickly running out of memory.  Do I need to do
> anything to explicitly release resources tied up in the
> JaxWsProxyFactoryBean or Client?
>
> Regards
>
> Matt



-- 
Daniel Kulp
dkulp@apache.org
http://dankulp.com/blog

Re: Resource management and cleanup

Posted by MattJax <ma...@gmail.com>.
Hi Ulhas,

Thanks for your last post.

I've removed the Bus creation to a static member of the Handler class I am
using.  I've got a monitor thread which kicks in every 2 minutes and acts as
a heartbeat to a WS.  This basically boils down to creating a new instance
of the following class and then calling 'process' :

public class WSEnabledHandler {
  private static Bus bus = null;
  private IDSWSPortType webService = null;

  public void process() {
     createPortService();

    // call the WS
    webService.authenticate();

    endProcess();
  }

  private void createPortService() {
	if(bus == null) {
		bus = BusFactory.newInstance().createBus();
		BusFactory.setDefaultBus(bus);
		BusFactory.setThreadDefaultBus(bus); 
	}
	JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
	factory.setBus(bus);
	factory.setServiceClass(IDSWSPortType.class);
	factory.setAddress(wsConfig.getWSDLUrl());
	webService = (IDSWSPortType) factory.create();
	
	// turn chunking off
	Client client = ClientProxy.getClient(webService);
	HTTPConduit httpConduit = (HTTPConduit) client.getConduit();
	HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
	httpClientPolicy.setAllowChunking(false);
	httpConduit.setClient(httpClientPolicy);
  }
  private void endProcess() {
        // does nothing, but should it?
  }
}

I'm getting a slow leak on memory - roughly 0.7Mb per call.  With this
background thread, together with user activity (which does similar to
above), we're quite quickly running out of memory.  Do I need to do anything
to explicitly release resources tied up in the JaxWsProxyFactoryBean or
Client?

Regards

Matt
-- 
View this message in context: http://www.nabble.com/Resource-management-and-cleanup-tp19644012p19955118.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Resource management and cleanup

Posted by Ulhas Bhole <ul...@iona.com>.
Hi Matt,
The bus would be created on the first call in your code path which 
creates JaxWsProxyFactoryBean. If you want to Isolate it you will need 
to create bus yourself and set the default bus to instance you created. 
The code will look like this.

Bus bus = BusFactory.getDefaultBus();

This will return you the bus and also will remove the bus creation out 
of your equation.

Regards,

Ulhas Bhole

MattJax wrote:
> Thanks for your reply Daniel.
>
> dkulp wrote:
>   
>> ... the above will create a Bus object that won't be released.  The Bus is
>> the registry for all the plugins and stuff.   However, creating the bus is
>> expensive so it's definitely not something you want to do each time if you
>> can avoid it so holding onto that is probably OK.
>>
>>     
>
> I'm not clear on where the bus is created in my snippet.  I'd guess as part
> of the factory.create().  This does indeed take a while the first time it is
> used and I can see it do lots of initialising stuff (looking for the cxf.xml
> etc).  Subsequent uses of the factory.create() are pretty quick though.
>
> How would I remove the Bus initialisation from the equation and move this
> elsewhere?  What's the recommended approach for this?  This is being used in
> an application which is not Spring-enabled, so a code-based approach would
> be great.
>
> Thanks in advance,
>
> Matt
>   

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland

Re: Resource management and cleanup

Posted by MattJax <ma...@gmail.com>.
Thanks for your reply Daniel.

dkulp wrote:
> 
> ... the above will create a Bus object that won't be released.  The Bus is
> the registry for all the plugins and stuff.   However, creating the bus is
> expensive so it's definitely not something you want to do each time if you
> can avoid it so holding onto that is probably OK.
> 

I'm not clear on where the bus is created in my snippet.  I'd guess as part
of the factory.create().  This does indeed take a while the first time it is
used and I can see it do lots of initialising stuff (looking for the cxf.xml
etc).  Subsequent uses of the factory.create() are pretty quick though.

How would I remove the Bus initialisation from the equation and move this
elsewhere?  What's the recommended approach for this?  This is being used in
an application which is not Spring-enabled, so a code-based approach would
be great.

Thanks in advance,

Matt
-- 
View this message in context: http://www.nabble.com/Resource-management-and-cleanup-tp19644012p19651442.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Resource management and cleanup

Posted by Daniel Kulp <dk...@apache.org>.
On Wednesday 24 September 2008 4:10:14 am MattJax wrote:
> I have a servlet-based application which uses CXF as a client to call a
> remote web service.
>
> I am using the following code to create my webservice object within the
> servlet code:
>
> <snippet>
>     JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
>     factory.setServiceClass(WebServiceInterface.class);
>     factory.setAddress(wsConfig.getWSDLUrl());
>     webService = (WebServiceInterface) factory.create();
> </snippet>
>
> I then call methods (usually just one) on the webService object, and then
> the servlet thread completes normally and I do not do anything explicitly
> to close this webService object.
>
> My question is: should I be closing this webService object explicitly?  It
> this tying up resources?

The service object it self will get garbage collected and anything it uses 
will go away.   However, the above will create a Bus object that won't be 
released.  The Bus is the registry for all the plugins and stuff.   However, 
creating the bus is expensive so it's definitely not something you want to do 
each time if you can avoid it so holding onto that is probably OK.   

If you need/want to kill that as well, you can call 
BusFactory.getDefaultBus().shutdown(true);

> Also, is the JaxWsProxyFactoryBean thread safe?  I.e. could I have an
> initialised JaxWsProxyFactoryBean object as a static member of the servlet
> I am using?

No.   The factorybeans are not reusable.   Once they have "created" their 
proxy they should be discarded.


-- 
Daniel Kulp
dkulp@apache.org
http://www.dankulp.com/blog