I don&#39;t have a solution, but BeanManager doesn&#39;t seem like the correct location either. Instance is part of a user facing API, BeanManager wasn&#39;t really supposed to be part of a standard user API.<div><br></div>

<div>Do we create another class, like a utility class called BeanDestroyer or something? Seems like javax.inject.Provider should have a destroy on it :(</div><div><br></div><div>If there isn&#39;t a good place for it, I guess BeanManager would work just as well.<br>

<br><div class="gmail_quote">On Mon, Mar 12, 2012 at 11:57, Pete Muir <span dir="ltr">&lt;<a href="mailto:pmuir@redhat.com">pmuir@redhat.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

We could do that, BUT we would still want to offer a convenient API for people to destroy beans. I think Marek is right, adding this to Instance isn&#39;t ideal. Any ideas about where a better place to put this? Simple API on BeanManager? :-/<br>


<div class="HOEnZb"><div class="h5"><br>
On 9 Mar 2012, at 19:43, Mark Struberg wrote:<br>
<br>
&gt; Not sure about the Instance#destroy() this might lead to a bunch of other problems...<br>
&gt; What about an additional Annotation to differentiate between beans which should get tracked and ones who should not?<br>
&gt;<br>
&gt; ... still trying to get the whole picture ...<br>
&gt;<br>
&gt; LieGrue,<br>
&gt; strub<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; ----- Original Message -----<br>
&gt;&gt; From: Marek Schmidt &lt;<a href="mailto:maschmid@redhat.com">maschmid@redhat.com</a>&gt;<br>
&gt;&gt; To: <a href="mailto:cdi-dev@lists.jboss.org">cdi-dev@lists.jboss.org</a><br>
&gt;&gt; Cc:<br>
&gt;&gt; Sent: Friday, March 9, 2012 6:37 PM<br>
&gt;&gt; Subject: Re: [cdi-dev] Are we always able to invoke @PreDestroy for @Dependent beans?<br>
&gt;&gt;<br>
&gt;&gt; On 09/03/12 17:47, Pete Muir wrote:<br>
&gt;&gt;&gt; I would propose we add Instance.destroy() as well, to make it easier…<br>
&gt;&gt;<br>
&gt;&gt; and what would happen if the bean is not @Dependent? (I would think it<br>
&gt;&gt; should be no-op for those, as otherwise I would need to start caring<br>
&gt;&gt; about the scope of the bean I am injecting via Instance.get()... but<br>
&gt;&gt; then, such API would probably be a bit confusing...<br>
&gt;&gt;<br>
&gt;&gt; --<br>
&gt;&gt; Marek Schmidt<br>
&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; On 9 Mar 2012, at 16:45, Mark Struberg wrote:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; Well, I think the newly proposed wording is still fine (sharing the<br>
&gt;&gt; CreationalContext of the @Dependent beans created by Instance.get() with the<br>
&gt;&gt; InjectionPoint.<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; What we need to do is to point people at manually creating the beans if<br>
&gt;&gt; they don&#39;t like this behaviour.<br>
&gt;&gt;&gt;&gt; In that case they still can use<br>
&gt;&gt;&gt;&gt; BeanManager#getBeans + bm#resolve + bm#getReference<br>
&gt;&gt;&gt;&gt; storing away the CreationalContext and later throw them away manually<br>
&gt;&gt; with bean.destroy.<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; LieGrue,<br>
&gt;&gt;&gt;&gt; strub<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; ----- Original Message -----<br>
&gt;&gt;&gt;&gt;&gt; From: Pete Muir &lt;<a href="mailto:pmuir@redhat.com">pmuir@redhat.com</a>&gt;<br>
&gt;&gt;&gt;&gt;&gt; To: Jozef Hartinger &lt;<a href="mailto:jharting@redhat.com">jharting@redhat.com</a>&gt;<br>
&gt;&gt;&gt;&gt;&gt; Cc: Mark Struberg &lt;<a href="mailto:struberg@yahoo.de">struberg@yahoo.de</a>&gt;; cdi-dev<br>
&gt;&gt; &lt;<a href="mailto:cdi-dev@lists.jboss.org">cdi-dev@lists.jboss.org</a>&gt;<br>
&gt;&gt;&gt;&gt;&gt; Sent: Thursday, March 8, 2012 11:35 PM<br>
&gt;&gt;&gt;&gt;&gt; Subject: Re: [cdi-dev] Are we always able to invoke @PreDestroy for<br>
&gt;&gt; @Dependent beans?<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; T his is still an issue for Instance.get() :-(<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; On 8 Mar 2012, at 15:47, Jozef Hartinger wrote:<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; I think you are wrong. The spec says:<br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; &quot;Any instance of the bean injected into method parameters<br>
&gt;&gt; of a<br>
&gt;&gt;&gt;&gt;&gt; disposer<br>
&gt;&gt;&gt;&gt;&gt;&gt; method or observer method exists to service the<br>
&gt;&gt;&gt;&gt;&gt;&gt; method invocation only (except for observer methods of<br>
&gt;&gt; container<br>
&gt;&gt;&gt;&gt;&gt;&gt; lifecycle events).&quot;<br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; So the instance should actually be destroyed immediately after<br>
&gt;&gt; the<br>
&gt;&gt;&gt;&gt;&gt;&gt; observer method invocation finishes.<br>
&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt; On 03/08/2012 01:47 PM, Mark Struberg wrote:<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; My answer sadly is no.<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; Dependent objects are stored in the CreationalContext which<br>
&gt;&gt; is held in<br>
&gt;&gt;&gt;&gt;&gt; the context which holds the outer NormalScoped contextual instance.<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; If the outer NormalScoped contextual instance gets<br>
&gt;&gt; desroyed, we also<br>
&gt;&gt;&gt;&gt;&gt; properly destroy all @Dependent beans in the CreationalContext.<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; So far so good, but now let&#39;s consider hte following<br>
&gt;&gt; scenario:<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; @ApplicationScoped<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; public class MyService {<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;     void doStats(@Observes RequestStatistics, UserHelper uh)<br>
&gt;&gt; {<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; UserHelper is @Dependent and a new instance will get<br>
&gt;&gt; created each time<br>
&gt;&gt;&gt;&gt;&gt; doStats() gets called.<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; What happens is that we don&#39;t know whether the<br>
&gt;&gt; UserHelper<br>
&gt;&gt;&gt;&gt;&gt; internally gets stored or just thrown away. In our sample most<br>
&gt;&gt; probably the<br>
&gt;&gt;&gt;&gt;&gt; later.<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; Each UserHelper will get stored in the CreationalContext of<br>
&gt;&gt; our single<br>
&gt;&gt;&gt;&gt;&gt; MyService instance. After 500.000 requests, our CreationalContext<br>
&gt;&gt; would contain<br>
&gt;&gt;&gt;&gt;&gt; 500.000 UserHelpers. Not smart, eh? This would just create a fat<br>
&gt;&gt; memory leak...<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; What we can do is to store only WeakReferences. E.g. via a<br>
&gt;&gt; WeakHashmap.<br>
&gt;&gt;&gt;&gt;&gt; This will automatically drop the entries if the instances in it get<br>
&gt;&gt; garbage<br>
&gt;&gt;&gt;&gt;&gt; collected. But in this case are not able to invoke @PreDestroy<br>
&gt;&gt; anymore I fear,<br>
&gt;&gt;&gt;&gt;&gt; because gone is gone ...<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; LieGrue,<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; strub<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; cdi-dev mailing list<br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; <a href="mailto:cdi-dev@lists.jboss.org">cdi-dev@lists.jboss.org</a><br>
&gt;&gt;&gt;&gt;&gt;&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/cdi-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/cdi-dev</a><br>
&gt;&gt;&gt;&gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt;&gt;&gt;&gt; cdi-dev mailing list<br>
&gt;&gt;&gt;&gt;&gt;&gt; <a href="mailto:cdi-dev@lists.jboss.org">cdi-dev@lists.jboss.org</a><br>
&gt;&gt;&gt;&gt;&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/cdi-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/cdi-dev</a><br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt; cdi-dev mailing list<br>
&gt;&gt;&gt; <a href="mailto:cdi-dev@lists.jboss.org">cdi-dev@lists.jboss.org</a><br>
&gt;&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/cdi-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/cdi-dev</a><br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; cdi-dev mailing list<br>
&gt;&gt; <a href="mailto:cdi-dev@lists.jboss.org">cdi-dev@lists.jboss.org</a><br>
&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/cdi-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/cdi-dev</a><br>
&gt;&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; cdi-dev mailing list<br>
&gt; <a href="mailto:cdi-dev@lists.jboss.org">cdi-dev@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/cdi-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/cdi-dev</a><br>
<br>
<br>
_______________________________________________<br>
cdi-dev mailing list<br>
<a href="mailto:cdi-dev@lists.jboss.org">cdi-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/cdi-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/cdi-dev</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>Jason Porter<br><a href="http://lightguard-jp.blogspot.com" target="_blank">http://lightguard-jp.blogspot.com</a><br><a href="http://twitter.com/lightguardjp" target="_blank">http://twitter.com/lightguardjp</a><br>

<br>Software Engineer<br>Open Source Advocate<br>Author of Seam Catch - Next Generation Java Exception Handling<br><br>PGP key id: 926CCFF5<br>PGP key available at: <a href="http://keyserver.net" target="_blank">keyserver.net</a>, <a href="http://pgp.mit.edu" target="_blank">pgp.mit.edu</a><br>


</div>