This content has been marked as final.
Show 2 replies
-
1. Re: How to inject into threads created with ExecutorService?
muallin Aug 10, 2011 10:44 AM (in response to yangju.richard.yang.pearson.com)Hi !
I came up with another aproach that i got somewhere else.
Instead of moving the bean from one class to another, i just get the BeanManager from the JNDI Context, and then, get the desired bean from its pool.
This way, you only use the bean wherever is needed.
To help with this, i created a singleton instance that retrieved the bean.
Hope this helps.
private static MyBeanInterfaz getBean() throws NamingException {
InitialContext jndiContext = new InitialContext();
BeanManager manager = (BeanManager) jndiContext.lookup("java:/comp/env/BeanManager");
Bean<MyBeanInterfaz> bean = (Bean<MyBeanInterfaz>) manager.getBeans(MyBeanInterfaz.class).iterator().next();
CreationalContext<MyBeanInterfaz> ctx = manager.createCreationalContext(bean);
MyBeanInterfaz interfaz = (MyBeanInterfaz) manager.getReference(bean, MyBeanInterfaz.class, ctx);
return interfaz;
}| -
2. Re: How to inject into threads created with ExecutorService?
ranophoenix Aug 10, 2011 11:26 AM (in response to yangju.richard.yang.pearson.com)Hi,
You can use generics:
public static <T> T getBean(Class clazz) { BeanManager bm = getBeanManager(); Bean<T> bean = (Bean<T>) bm.getBeans(clazz).iterator().next(); CreationalContext<T> ctx = bm.createCreationalContext(bean); T cdiBean = (T) bm.getReference(bean, clazz.getClass(), ctx); return cdiBean; }