-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello friends.
I want to invoke EJB session beans from Zk pages. How can i do that?
I saw http://www.zkoss.org/smalltalks/jndi/ but there only talk about Jboss.
They talk about JndiVariableResolver and said that it will resolve variable in following order:
1 java:comp/env
2 java:comp
3 java:
4 The variable will be look up as a session beans with prepend.
Does Glassfish use the same scheme with jndi??
Even more, they use <property name="jboss.entity.manager.factory.jndi.name" value="java:comp/entityManagerFactory"/>
to call an EntityManagerFactory to work with persistence units. Is there something like that with Glassfish?
For resume, I want to invoke GlassFish's EJB from ZK pages. How can i do it?
I hope someone can help me about it.
Thanks in advance.
Hi dukeim,
I have made my own class which get session beans by JNDI calls. I call this class from the <zscript> script. You can also cache an SB proxy into the desktop to be easily available. (It is not a good think to cache them in the session, since session can be accessed from different threads, while desktop can be accessed just from one thread.)
I am not sure how JndiVariableResolver get the beans, but usually may need different approach to stateless and stateful beans.
Cheers
Andy
It is very primite, no caching (caching in desktop is sometimes made in Composers)
public class EBeanUtils { private static final Logger LOG = Logger.getLogger(EBeanUtils.class); private static final String JNDI_PREFIX = "tiger-ear/"; private static final String JNDI_SUFFIX = "Bean/local"; /** * <p> * Get a bean. (Always a new bean.) * * @param clazz * Bean class. * @return Bean, every time a new JNDI lookup, (i.e. a new bean is returned). In case of troubles return {@code null}. */ @SuppressWarnings("unchecked") public static <T> T getBean(Class<T> clazz) { T bean = null; try { String clazzName = clazz.getSimpleName(); bean = (T) InitialContext.doLookup(JNDI_PREFIX + clazzName + JNDI_SUFFIX); // beanLocations.get(clazz) if (LOG.isTraceEnabled()) { LOG.tracef("getBean(%s) got bean proxy, hash code: %h", clazz.getName(), bean); } } catch (Exception e) { LOG.errorf(e, "Error getting bean %s: %s", clazz.getName(), e); } if (bean == null) { LOG.warn("getBean() bean not found: " + clazz); } return bean; } }
Note, you may also make some static HashMap wich will map the classes to the names.
Usage in ZUL:
<zscript><![CDATA[ import xxx.MyBean; MyBean myBean = EBeanUtils.getBean(MyBean.class); // get a bean Object data = myBean.getData(); // call a bean ]]></zscript> <label value="${data}" /> <label value="${myBean.data}" /><!-- call a bean --> ...
But usually I get a bean in an GenericForwardComposer, then i made it visible for the ZUL in doBeforeComposeChildren() method by:
@Override public void doBeforeComposeChildren(Component comp) throws Exception { super.doBeforeComposeChildren(comp); comp.setVariable("controller", this, true); comp.setVariable("myBean", myBean, true); // no necessary, if this controller has a method public MyBean getMyBean() }
I do not use it - I do not like it.
Hello Andy.
do you use the same InitialContext.doLookup whit persistence units (JPA)? or are there something like:
<property name="jboss.entity.manager.factory.jndi.name" value="java:comp/entityManagerFactory"/> for Glassfish??
Thanks a lot.
Julio
Hi Julio,
I do not know. I have used just session beans by JNDI calls.
Cheers
Andy
Hi Andy.
I tried it and I have used EJB from ZK pages without do a lookup (JNDI). I only did an import xxxx.MyBean in my ZK page and it works!!
The unique problem was I had to deploy an EAR and don't the EJB module and WEb module separately.
Bye
Julio
Asked: 2010-01-06 19:14:27 +0800
Seen: 1,861 times
Last updated: Feb 21 '11