openwebbeans-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Struberg <strub...@yahoo.de>
Subject Re: Instance<X> and @Disposes
Date Mon, 23 Jun 2014 15:10:34 GMT
Please take a look at Apache DeltaSpike BeanProvider#getDependent.
This gives you a wrapper object which allows manual disposal as well.

LieGrue,
strub


On Monday, 23 June 2014, 17:07, Xavier Dury <kalgon@hotmail.com> wrote:
 

>
>
>Hi Mark,
>
>Thanks for your answer.
>
>Indeed, my producer creates a @Dependent scoped FTPClient.
>
>I'm not using @RequestScoped or any other scope because this code is triggered by a @Schedule
method.
>
>I'm not directly @Injecting ftpClient in my MyService class because the same instance
of FTPClient would always be used, that's why I turned it into an Instance<FTPClient>
in the first place (to be sure to work with a fresh FTPClient everytime I call get()).
>
>But now, I don't really know how to dispose it properly (using @Disposes).
>
>The best solution would be to create some sort of ThreadLocalContext like this:
>
>public class FTPClientProducer {
>
>    @Produces
>    @ThreadLocalScoped
>    FTPClient newFTPClient() {...}
>
>}
>
>public MyService {
>
>   @RunInThreadLocalContext // activate ThreadLocalContext for this invocation through
an interceptor
>   void doSomething() {...}
>}
>
>but before going that way, I wanted to know if there was something in CDI which allowed
me to easily/simply dispose a bean.
>
>Regards,
>
>Xavier
>
>
>----------------------------------------
>> Date: Mon, 23 Jun 2014 15:40:55 +0100
>> From: struberg@yahoo.de
>> Subject: Re: Instance<X> and @Disposes
>> To: user@openwebbeans.apache.org
>>
>> Hi Xavier!
>>
>> Short answer: NormalScoped beans have their own lifecycle and will get destroyed
whenever the Context ends. Eg. for @RequestScoped FTPClient it will get destroyed at the end
of each request.
>>
>> For @Dependent scoped contextual instances we store the CreationalContext with the
bean containing the outermost Instance.
>>
>>
>> As I understand your method @Produces FTPClient newFTPClient() creates a @Dependent
scoped FTPClient, right?
>>
>> In this case the @Disposes method for all FTPClients created over time will get called
when your @Stateless MyService gets disposed. This happens if e.g. the container shuts down,
if the instance gets removed from the pool or (in some containers) if the pool timeout exceeds
(some containers only keep pooled @Stateless instances for 6000 seconds and then create fresh
ones).
>>
>>
>> Any further questions? Just keep asking :)
>>
>>
>> LieGrue,
>> strub
>>
>>
>>
>> On Monday, 23 June 2014, 16:26, Xavier Dury <kalgon@hotmail.com> wrote:
>>
>>
>>>
>>>
>>>Hi,
>>>
>>>I was wondering how an object provided by an Instance<X> could be
>>>disposed through the correct @Disposes method without explicitly
>>>calling that method.
>>>
>>>For example:
>>>
>>>public class FTPClientProducer {
>>>
>>> @Produces
>>> FTPClient newFTPClient() {...}
>>>
>>> void closeFTPClient(@Disposes FTPClient ftpClient) {...}
>>>}
>>>
>>>@Stateless
>>>public class MyService {
>>>
>>> @Inject
>>> Instance<FTPClient> ftpClientProvider;
>>>
>>> void doSomething() {
>>> FTPClient ftpClient = ftpClientProvider.get();
>>> try {
>>> ...
>>> } finally {
>>> ??? (close ftpClient )
>>> }
>>> }
>>>}
>>>
>>>Ideally, I would like to replace ??? by something like:
>>>
>>>ftpClientProvider.dispose(ftpClient);
>>>
>>>Is there something in CDI which provides that kind of feature?
>>>
>>>Regards,
>>>
>>>Xavier
>>>
>>>
>                          
>
>
Mime
View raw message