3 Replies Latest reply on May 20, 2016 11:20 AM by Max Barkley

    Multiple simultaneous instances of @Dependent classes

    Victor Wjugow Newbie


      FOB I'm using Errai 3.2.0.Final with Jboss AS 7.1.1


      Now my question. I've always seen that Errai creates a new instance of the widgets that are annotated with @Dependent scope each time the user visits the page that contains these widgets (not sure if this is precise, but I know it's not for every request. Please correct me if I'm wrong)

      In the cases where this problem really bothered me, I would go ahead and make the widget @ApplicationScope, but sometimes, of course, that is not a good solution because we need several instances of the it. The question then is, is there a way to make Errai kill/delete those old instances of the widgets that I'm not seeing any more ?


      Here are some screenshots from the chrome console that prove my point (Number indicates the amount of repeated messages) . In my app, I have a list of StrategySwitch widgets that observe some event, and each time I revisit the page that contains them (or some other action, not exactly sure what causes this), more and more widgets observe the event.






      Note, I know I can add the isAttached() condition in the observer method to ignore the event if that is not the case, but I still want to understand the situation/problem.

      Also, @Dependent is the default scope right?

      Thanks in advance.

        • 1. Re: Multiple simultaneous instances of @Dependent classes
          Max Barkley Expert

          Hi Victor,


          Dependent beans should be destroyed when the beans they are injected into are destroyed. If your observer beans are injected into an Errai Navigation page they should be destroyed when you navigate away from the page (otherwise you've found a bug). If you're creating your own instances of beans with the bean manager or an Instance<T> then you need to destroy those beans yourself when you're done with them, otherwise those beans and any dependent beans injected into them will continue being in service.

          • 2. Re: Multiple simultaneous instances of @Dependent classes
            Victor Wjugow Newbie

            Thanks mbarkley

            We do have some Instance<T> but I didn't know what you mentioned. I will try to remove those but it will take time. I'll try to get back to this post when that happens.


            But on the navigation topic you mentioned, with  'Errai Navigation page' you mean a Class annotated with @Page? We have those pages also annotated with @ApplicationScope, is that okay? not okay? is it meaningless?

            • 3. Re: Multiple simultaneous instances of @Dependent classes
              Max Barkley Expert

              Hi Victor,


              No need to remove the Instance<T>, just make sure you use Instance#destroy for any dependent beans you make when you're done with them.

              By Errai Navigation page I meant an @Page. Making a page @ApplicationScoped isn't wrong per se, but it means that your page will still exist after you navigate away from it. If the app scoped page has dependent instances injected into it, those instances will also still exist and observe events.

              My personal recommendation is that widgets/views should almost always be dependent, and @ApplicationScoped should be saved for stateful services that views/presenters can query when constructed. But that's just an opinion so take it as you will.