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 <[email protected]> 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                           
>
>

Reply via email to