0

How to invoke glassfish's EJB from ZK pages?

asked 2010-01-06 19:14:27 +0800

dukeim gravatar image dukeim
54 2

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.

delete flag offensive retag edit

17 Replies

Sort by » oldest newest

answered 2010-01-06 22:02:43 +0800

PeterKuo gravatar image PeterKuo
481 2

I found an article,
ZK 3.6.3 with Netbeans 6.8 Beta on Glassfish V3
http://javadude.wordpress.com/2009/11/06/zk-3-6-3-with-netbeans-6-8-beta-on-glassfish-v3/

can it help you?

link publish delete flag offensive edit

answered 2010-01-07 01:23:43 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

updated 2010-01-07 01:25:15 +0800

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

link publish delete flag offensive edit

answered 2010-01-07 16:32:42 +0800

dukeim gravatar image dukeim
54 2

Hi PeterKuo

I reviewed http://javadude.wordpress.com/2009/11/06/zk-3-6-3-with-netbeans-6-8-beta-on-glassfish-v3/ but they don´t use EJB
and I need to use EJB in Zk pages.

Thansk anyway.
Julio

link publish delete flag offensive edit

answered 2010-01-07 16:34:10 +0800

dukeim gravatar image dukeim
54 2

Hello Andy.

Can you give me some code example about your own class and how to use it in an real example.

Thanks.
Julio

link publish delete flag offensive edit

answered 2010-01-08 04:57:27 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

updated 2010-01-08 06:17:03 +0800

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()
	}

link publish delete flag offensive edit

answered 2010-01-08 11:53:21 +0800

dukeim gravatar image dukeim
54 2

And, what about JndiVariableResolver?

link publish delete flag offensive edit

answered 2010-01-08 13:15:29 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

I do not use it - I do not like it.

link publish delete flag offensive edit

answered 2010-01-08 17:37:34 +0800

dukeim gravatar image dukeim
54 2

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

link publish delete flag offensive edit

answered 2010-01-11 01:54:20 +0800

xmedeko gravatar image xmedeko
1031 1 16
http://xmedeko.blogspot.c...

Hi Julio,

I do not know. I have used just session beans by JNDI calls.

Cheers
Andy

link publish delete flag offensive edit

answered 2010-01-11 13:21:35 +0800

dukeim gravatar image dukeim
54 2

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

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-01-06 19:14:27 +0800

Seen: 1,861 times

Last updated: Feb 21 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More