1 2 Previous Next 19 Replies Latest reply on Sep 21, 2015 3:31 AM by mkouba Go to original post
      • 15. Re: Do I need to explicitly release a contextual JDialog created with CDI?
        marcos_aps

        Martin Kouba escreveu:

        By the way, I don't know why Weld doesn't implement this. It is in the specification, so it is not going agains anything here. What's the point of a CDI provider to retain a lost instance in memory if it's not used anywhere else?

        Because if you have a dependent bean with @PreDestroy callback you can't simply use a java.lang.ref.WeakReference - the container is required to destroy the bean instance correctly. I'm not aware of any reliable way of doing this in Java. But I'm ready to explore any tips

        Yes, you're right. This is a real impediment. Thinking better now, if the @PreDestroy was the only reason for the impediment, I could live without it in my dependent beans, but not without transitive dependencies.

        • 16. Re: Do I need to explicitly release a contextual JDialog created with CDI?
          mkouba

          If these dependencies are normal scoped or dependent without @PreDestroy/disposer you should be fine too (I mean transitivity requirement should only apply to dependent dependencies hierarchy, but I'm not 100% sure). Anyway, it's just an optimization - nothing you should rely on.

          • 17. Re: Do I need to explicitly release a contextual JDialog created with CDI?
            marcos_aps

            Martin Kouba escreveu:

             

            If these dependencies are normal scoped or dependent without @PreDestroy/disposer you should be fine too (I mean transitivity requirement should only apply to dependent dependencies hierarchy, but I'm not 100% sure). Anyway, it's just an optimization - nothing you should rely on.

            I created this simple proof of concept test:

             

            public class MyBean1

            {

                @Inject

                private MyBean2 _myBean2;

             

                @Override

                protected void finalize() throws Throwable

                {

                    super.finalize();

                    System.out.println("MyBean1 released");

                }

            }

             

            public class MyBean2

            {

                @Override

                protected void finalize() throws Throwable

                {

                    super.finalize();

                    System.out.println("MyBean2 released");

                }

            }

             

            private void shortLivedMethod()

            {

                CDI.current().select(MyBean1.class).get();

                Timer timer = new Timer(true);

                timer.schedule(

                    new TimerTask()

                    {

                        @Override

                        public void run()

                        {

                            System.gc();

                        }

                    },

                    2000, 3000

                );

            }

             

            And it really worked as you said: with no @PreDestroy method, the dependent beans were released by CDI (the messages were printed on the console). Just wondering: does this still work if I apply interceptors and decorators to the dependent beans? Can you tell me something about this? (I think we're almost there).

            • 18. Re: Do I need to explicitly release a contextual JDialog created with CDI?
              marcos_aps

              I've just checked with interceptors: it didn't work. I applied an interceptor to the MyBean class and the bean was not released automatically by CDI anymore. I suppose the same is true for decorators and even events.

              • 19. Re: Do I need to explicitly release a contextual JDialog created with CDI?
                mkouba

                It should work the same for decorators but not for events. Any @Dependent bean instance created to receive an observer method invocation is destroyed when the invocation completes. Also @Dependent bean instances injected into method parameters of an observer method are destroyed when the invocation completes. See also http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#dependent_destruction.

                1 2 Previous Next