6 Replies Latest reply on Oct 17, 2012 7:55 AM by nimo22

    get the instance of a weld managed bean via session map

    nimo22

      How can I get the instance of a weld managed bean via session map?

       

      For example, I have this:

       

      {code}

      HttpSession session = getUserSession();

       

      for (Enumeration<String> i = session.getAttributeNames(); i.hasMoreElements();) {

            String _s = i.nextElement();

             log.infov("_s: {0}", _s);

      }

                         

      //  I got the needed key for my instance by printing session.getAttributeNames()

      MyBean mybean = (MyBean) session.getAttribute("org.jboss.weld.context.http.HttpSessionContext#org.jboss.weld.bean-serview.war/D:/jboss-as-7.1.1.Final/standalone/deployments/test.war/WEB-INF/classes-ManagedBean-class com.MyBean");

      {code}

       

      However, this does not work.

      I get a java.lang.ClassCastException: org.jboss.weld.context.SerializableContextualInstanceImpl cannot be cast to com.MyBean

       

      Is there a solution for that?

        • 1. Re: get the instance of a weld managed bean via session map
          nimo22

          The thing what I want to achieve is:

           

          Set a value within a bean by retrieving the instance of that bean from sessionMap.

          • 2. Re: get the instance of a weld managed bean via session map
            luksa

            You shouldn't use the session map for this. You should obtain the BeanManager and then get the bean from the BeanManager.

            1 of 1 people found this helpful
            • 3. Re: get the instance of a weld managed bean via session map
              nimo22

              Thanks! I looked at the BeanManager-API and found the way how to obtain the reference via BeanManager:

               

              {code}

              for (Iterator<Bean<?>> _i = beanManager.getBeans(MySessionScopeClass.class).iterator(); _i.hasNext();) {

                          Bean<MySessionScopeClass> bean = (Bean<MySessionScopeClass>) _i.next();

                          MySessionScopeClass myInstance = (MySessionScopeClass) beanManager.getReference(bean, MySessionScopeClass.class, beanManager.createCreationalContext(bean));

                          log.infov("myInstance{0}", myInstance);

              }

              {code}

              However, the Iterator only logs these instances bind to the actual session!

               

              Imagine two sessions which create myInstance for each session.

              How can I retrieve ALL instances of MySessionScopeClass.

               

              I have a ApplicationScoped Bean and wants to retrieve ALL sessionscoped instances of MySessionScopeClass,

              not only the one that is in my session. How can I do that?

              • 4. Re: get the instance of a weld managed bean via session map
                nimo22

                According to Weld Documentation:

                 

                There's an easy way to find out what beans exist in the application:

                Set<Bean<?>> allBeans = beanManager.getBeans(Obect.class, new AnnotationLiteral<Any>() {});

                 

                 

                I use that in my method:

                {code}

                public static void logAllMyBeanInstances(){

                 

                Set<Bean<?>> allBeans = beanManager.getBeans(MySessionScopedBean.class, new AnnotationLiteral<Any>() {});

                 

                for(Bean<?> b: allBeans){

                log.infov("b.getBeanClass().getSimpleName {0}", b.getBeanClass().getSimpleName());

                log.infov("b.getScope {0}", b.getScope());

                log.infov("b.getName {0}", b.getName());

                }

                }

                {code}

                 

                However, I get not all (actual) instances of "MySessionScopedBean". I have indeed 2 active sessions, both have created a instance of the bean "MySessionScopedBean".

                But I am not able to get all my (active) instances of that bean type with the method logAllMyBeanInstances.

                 

                Does anyone know, what is wrong?

                 

                I guess, it must be possible, because BeanManager is ApplicationScoped (Singleton) and must know all instances. Am I right?

                 

                 

                • 5. Re: get the instance of a weld managed bean via session map
                  mkouba

                  Hi,

                   

                  firstly, a bean represents component metadata only. It's not the same as the bean instance - concrete object living in well-defined context (concrete instance of such component). In other words you will never get bean instances using BeanManager.getBeans(), only the components metadata. Secondly, AFAIK there is no neat way to get ALL instances of @SessionScoped bean -> each one lives in different session context and HTTP session respectively (which are naturally isolated).

                  1 of 1 people found this helpful
                  • 6. Re: get the instance of a weld managed bean via session map
                    nimo22

                    Is there anything else such as BeanManager, which is is Singleton or ApplicationScoped? If so, I can get all instances from it.

                     

                    Does Weld stores instances of its created beans? If so, where does Weld stores all the instances of its created Bean?

                     

                    {quote}

                    Secondly, AFAIK there is no neat way to get ALL instances of @SessionScoped bean

                    {quote}

                     

                    Is this a limitation or for what reason I cannot get these instances?

                     

                    Should I make a future request?