Ok,

spec says:

"an event with qualifier @Destroyed(X.class) when a custom context is
destroyed, i.e. after the actual destruction,"

So we are sadly good and you need to use the @PreDestroy trick

Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<https://blog-rmannibucau.rhcloud.com> | Old Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2017-05-29 11:05 GMT+02:00 Romain Manni-Bucau <[email protected]>:

> oops, did a magic shortcut deleting half of the mail :s, sorry about it
>
> here is the bean which works:
>
> @ApplicationScoped
> public static class Listener {
>     private boolean init = false;
>     public void start(@javax.enterprise.event.Observes 
> @Initialized(ApplicationScoped.class) Object start) {
>         init = true;
>         log(start);
>     }
>
>     @PreDestroy
>     public void end() {
>         System.out.println();
>         System.out.println(this);
>         System.out.println(System.identityHashCode(this));
>         System.out.println(init);
>         System.out.println();
>     }
>
>     private void log(final Object event) {
>         System.out.println();
>         System.out.println(this);
>         System.out.println(System.identityHashCode(this));
>         System.out.println(event);
>         System.out.println(System.identityHashCode(event));
>         System.out.println();
>     }
> }
>
>
> And its output:
>
>
> org.apache.tomee.embedded.ClasspathAsWebappTest$Listener@4e2916c3
> 1311315651
> org.apache.catalina.core.ApplicationContextFacade@64aad6db
> 1688917723
>
> org.apache.tomee.embedded.ClasspathAsWebappTest$Listener@4e2916c3
> 1311315651
> true
>
>
> The destroy event is fired once the bean was already destroyed it seems,
> not a blocker clearly but will check why it breaks your case
>
> Romain Manni-Bucau
> @rmannibucau <https://twitter.com/rmannibucau> |  Blog
> <https://blog-rmannibucau.rhcloud.com> | Old Blog
> <http://rmannibucau.wordpress.com> | Github
> <https://github.com/rmannibucau> | LinkedIn
> <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
> <https://javaeefactory-rmannibucau.rhcloud.com>
>
> 2017-05-29 11:01 GMT+02:00 Romain Manni-Bucau <[email protected]>:
>
>> Just tested with tomee embedded this listener:
>>
>> @ApplicationScoped
>> public static class Listener {
>>     public void start(@javax.enterprise.event.Observes 
>> @Initialized(ApplicationScoped.class) Object start) {
>>         log(start);
>>     }
>>     public void end(@javax.enterprise.event.Observes 
>> @Destroyed(ApplicationScoped.class) Object end) {
>>         log(end);
>>     }
>>
>>     private void log(final Object event) {
>>         System.out.println();
>>         System.out.println(this);
>>         System.out.println(System.identityHashCode(this));
>>         System.out.println(event);
>>         System.out.println(System.identityHashCode(event));
>>         System.out.println();
>>     }
>> }
>>
>>
>> And got:
>>
>> org.apache.tomee.embedded.ClasspathAsWebappTest$Listener@21c747a
>> 35419258
>> org.apache.catalina.core.ApplicationContextFacade@77d4ac52
>> 2010426450
>>
>> org.apache.tomee.embedded.ClasspathAsWebappTest$Listener@1913053c
>> 420676924
>> org.apache.catalina.core.ApplicationContextFacade@77d4ac52
>> 2010426450
>>
>> so looks good
>>
>>
>> Romain Manni-Bucau
>> @rmannibucau <https://twitter.com/rmannibucau> |  Blog
>> <https://blog-rmannibucau.rhcloud.com> | Old Blog
>> <http://rmannibucau.wordpress.com> | Github
>> <https://github.com/rmannibucau> | LinkedIn
>> <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
>> <https://javaeefactory-rmannibucau.rhcloud.com>
>>
>> 2017-05-29 10:26 GMT+02:00 tonywestonuk <[email protected]>:
>>
>>>
>>> MyService is an @ApplicationScoped bean
>>>
>>> And, yet the identity hashcodes change between the init, and destroy
>>> methods.
>>>
>>> Which means, I don't have access to the same instance variables in
>>> destory,
>>> that I constructed in the init.
>>>
>>> This is the problem.
>>>
>>>
>>> In my specific case, I am creating a UDP socket in init, storing it as an
>>> instance variable. And want to close it in destroy. However, the destroy
>>> method cant access the socket - crashes with null pointer.  I cant
>>> hot-redeploy as the UDP socket remains open. :-(
>>>
>>>
>>>
>>>
>>>
>>> --
>>> View this message in context: http://tomee-openejb.979440.n4
>>> .nabble.com/CDI-Observes-ApplicationScoped-class-tp4681766p4681768.html
>>> Sent from the TomEE Users mailing list archive at Nabble.com.
>>>
>>
>>
>

Reply via email to