-
1. Re: get the instance of a weld managed bean via session map
nimo22 Oct 16, 2012 5:06 AM (in response to 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 Oct 16, 2012 5:23 AM (in response to nimo22)1 of 1 people found this helpfulYou shouldn't use the session map for this. You should obtain the BeanManager and then get the bean from the BeanManager.
-
3. Re: get the instance of a weld managed bean via session map
nimo22 Oct 16, 2012 9:25 AM (in response to luksa)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 Oct 17, 2012 5:43 AM (in response to 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 Oct 17, 2012 6:29 AM (in response to nimo22)1 of 1 people found this helpfulHi,
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).
-
6. Re: get the instance of a weld managed bean via session map
nimo22 Oct 17, 2012 7:55 AM (in response to mkouba)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?