Using ZK JndiVariableResolver - Retrieve Session Beans and EntityManagerFactory

Jeff Liu, Engineer, Potix Corporation
November 29, 2007

Version

Applicable to ZK 3.0.1 Freshly (zk-3.0.1-FL-2007-12-07 and later)

Applicable to JBoss AS 4.0.5.GA

Introduction

Java Platform, Enterprise Edition (Java EE) is the industry standard for developing portable, robust, scalable and secure server-side Java applications. Enterprise JavaBeans (EJB) technology is the server-side component architecture for Java Platform, Enterprise Edition (Java EE). EJB technology enables rapid and simplified development of distributed, transactional, secure and portable applications based on Java technology.


By bundling ZK into a JavaEE application server (JBoss is used in this tutorial) and using ZK JndiVariableResolver, ZK developers are now able to combine EJB into ZK application easily.

If you are not familiar with EJB, here is a comprehensive EJB tutorial http://www.laliluna.de/ejb-3-tutorial-jboss.html

Retrieving Session Beans and EntityManagerFactory from ZK

Following I am going to present you an example on how to use ZK JndiVariableResolver to retrieve session beans and entityManagerFactory

Add JndiVariabelResolver to Page

Using ZK JndiVariableResolver, session beans and entityManagerFactory can be retrieved by JNDI binding. First, Declare the directive

Using Default Constructor

<?variable-resolver class="org.zkoss.zkplus.jndi.JndiVariableResolver" ?>
<window>
...
</window>

Also, JndiVariableResolver constructor can take two extra agruments for special JNDI patterns.

  1. prepend - The prepended part of JNDI name
    Ex: "prepend/ejbBean/local"
  2. mapping - The key-value pairs for JNDI name and its corresponding variable name
    Ex:"a=custom/MySession,b=custom/MySession2,emf=java:/EntityManagerFactory"
/*
	arg0 is first argument of variable-resolver constructor
	arg1 is second argument of variable-resolver constructor
*/
<?variable-resolver class="org.zkoss.zkplus.jndi.JndiVariableResolver" agr0="ZkEJB3Demo"
arg1="mail=java:comp/env/mailing,sec=java:comp/security/module" ?>
<window>
...
</window>

JndiVariableResolver 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.
  5. The key-value pairs which is defined by mapping

Retrieve session beans

Since the session bean is bound to the java:comp/env by the configuration in jboss-web.xml and web.xml, the session bean can be easily retrieved by using JndiVariableResolver.

Declaring an EJB reference in jboss-web.xml and web.xml

jboss-web.xml

<ejb-local-ref>
	<ejb-ref-name>personLocalBean</ejb-ref-name>
	<ejb-ref-type>Session</ejb-ref-type>
	<local>demo.PersonBeanLocal</local>
	<local-jndi-name>ZkEJB3Demo/PersonBean/local</local-jndi-name>
</ejb-local-ref>

web.xml

<ejb-local-ref>
	<ejb-ref-name>personLocalBean</ejb-ref-name>
	<ejb-ref-type>Session</ejb-ref-type>
	<local-home>demo.PersonBeanLocal</local-home>
	<local>demo.PersonBeanLocal</local>
</ejb-local-ref>

person.zul

<?variable-resolver class="org.zkoss.zkplus.jndi.JndiVariableResolver" ?>
<window>
	<zscript>
		//Use session bean simply by its name "personLocalBean"
		personLocalBean.createDemoData();
		List t = personLocalBean.getAllPersons();
	</zscript>
	<listbox width="600px">
	<listhead sizable="true">
		<listheader label="name" sort="auto"/>
		<listheader label="email" sort="auto"/>
	</listhead>
	<listitem forEach="${t}">
		<listcell label="${each.name}"/>
		<listcell label="${each.email}"/>
	</listitem>
	</listbox>
</window>

Snapshot:

Retrieve EntityManagerFactory

Persistence units are not bound into JNDI by default, we define JBoss specific properties in persistence.xml to bind them into JNDI. As a result, we are able to retrieve the entity manager factory by JndiVariableResolver

persistence.xml

...
  <properties>
     <property name="jboss.entity.manager.factory.jndi.name" value="java:comp/entityManagerFactory"/>
  </properties>
</persistence-unit>
...

person2.zul

<?variable-resolver class="org.zkoss.zkplus.jndi.JndiVariableResolver" ?>
<window>
	<zscript>
		import javax.persistence.EntityManager;
		//Use EntityManagerFactory simply by its name "entityManagerFactory"
		EntityManager em = entityManagerFactory.createEntityManager();
		List persons= em.createQuery("from Person").getResultList();
		em.close();
	</zscript>
	<listbox width="600px">
	<listhead sizable="true">
		<listheader label="name" sort="auto"/>
		<listheader label="email" sort="auto"/>
	</listhead>
	<listitem forEach="${persons}">
		<listcell label="${each.name}"/>
		<listcell label="${each.email}"/>
	</listitem>
	</listbox>
</window>

Bundle ZK into JBoss by Eclipse

Step by Step

  • Create a Dynamic Web Project and bundle ZK to it, refer to ZK Quick Start Guide
  • Create an EJB3 Project
  • Create a JavaEE- Enterprise Application Project and setup the module dependencies for the dynamic web project and EJB3 project in previous steps

After the steps above, the Deployment Descriptor(application.xml) should be look like this:

<?xml version="1.0" encoding="UTF-8"?>
<application id="Application_ID" version="1.4" ...>
	<display-name>
	ZkEJB3Demo</display-name>
	<module>
		<web>
			<web-uri>DemoWeb.war</web-uri>
			<context-root>DemoWeb</context-root>
		</web>
	</module>
	<module >
		<ejb>DemoEJB3.jar</ejb>
	</module>
</application>

Download

Deploy ZkEJB3Demo.ear

Notice: before deploy the demo project, please make sure that the JBoss AS datasource is setup correctly. Refer to http://wiki.jboss.org/wiki/Wiki.jsp?page=ConfigDataSources
  • Stop JBoss Application Server
  • Copy ZkEJB3Demo.ear to JBOSS_HOME\server\default\deploy\
  • Start JBoss Application Server

Conclusion

ZK developres should have a general idea of ZK JndiVariableResolver now. Using JndiVariableResolver, ZK developers can retrieve any JNDI env variables, not only EJB3 variables. For example: If you have a mailing java object which is bound to java:comp/env/mail, ZK developers can retrieve it easily by JndiVariableResolver. If you have futher question, feel free to leave comment here.

Comments
 
Julio Monteiro
2008-01-03

Thanks for the wonderful smalltalk!

Just a small typo:
In the first section after the Introduction, in the example code, you mispelled <?variable-resolver>.

Cheers and Happy New Year!
Julio Monteiro

Jeff Liu
2008-01-04

Thank you for inform. It is corrected. Thank you!

Ayman Elgharabawy
2008-02-13

i am so exciting about it but

i am just asking about if there any example using ejb3

with oc4j 10.1.3 ??

Ayman Elgharabawy
2008-02-13

is there any example for applying it on oc4j 10.1.3???

Ilko Iliev
2008-11-27

someone has already asked but without an answer - how can I use @EJB annotation to call session EJB 3.0? or is there another similar way?

thanks

Ilko Iliev
2008-11-29

Ok, I've got the "magic" now, trying unsuccessfully to put it to work with JEE5 and real EJB3 project... This is completely tricky example, mixing J2EE 1.4 EAR with JPA module, declaring Stateless EJB 3.0 inside it. I'm not sure, if this is really a good idea - could this be run with "clean" JEE5 project, using real EJB3 and 2.5 Web module?

LINDA
2009-02-12

How can I access the remote session bean using

Ondrej Medek
2009-08-06

You do not need jboss-web.xml, you can specify the mapping of a bean by <mapped-name> element in web.xml.

Also, it is not clear, when the JNDI lookup is made - it is important for handling stateful beans, each JNDI lookup creates a new stateful bean.

sadeg
2009-12-05

The name of God
Hello
Of my fourth semester student of Mohammad Sadeq rare car mechanic I am.
Servant keen to New Fields Science robotic, but I Since the general financial situation in terms of students is not good
I can not easily do the purchases because mostly are expensive.
Since my subject is technical
General interest to a lot of technical things like.
Also tell us to place junior college
Is to visit the Academic Nbrdh can judge yourself how my students can familiarize with the industry.
I request you that you can increase the knowledge Any Vartqa
Vigor Scientific Kshvrbray servant post
Of course, no money
Such as: training CD, book,catalog.handbook.encylopedia. article, tools, construction robots
Even if the lesions are each summary
How can you even think of things that do not eat pain pain servant eats.
Even if you can not do it this message to you who can help me to refer.
Finally, even if NOT wish added
I can open applications, he answered, I thank you
Thanked the students learned a


Hoping for an Iranian Abadan and free
Address: iran-Guilan - Bandar Anzali – sahele goo – peesh sakhte - St. 13 - No. 7
Postal Code 4315867669






Alfa_tj52@yahoo.com
In addition, tools have serious interest in radio control
If possible email all Cooperators pick me to post via email
I also very interested to have control of the radio equipment, but is very expensive Vsaylsh

Please send anything you can
Thanked the

If possible send me their products can

hthach
2010-01-27

What is the subsitute for jboss-web.xml when we use Glassfish ? Can we have an example ? Thanks

Masum
6 days ago

Hi ,
I am trying this example.
http://www.zkoss.org/smalltalks/jndi/

I am using ZK+EJB3+jboss5.1+mysql+netbeans .


My web inf jboss-web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Service Reference 4.2//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd">
<jboss-web>
<ejb-local-ref>
<ejb-ref-name>personLocalBean</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>demo.PersonBeanLocal</local>
<local-jndi-name>ZkEJB3Demo/PersonBean/local</local-jndi-name>
</ejb-local-ref>
</jboss-web>

Here I have mentioned : ''ejb-ref-type'
but when I am run this web client then I have found that :
Element type "ejb-ref-type" must be declared. @ vfszip:/C:/jboss-5.0.1.GA/server/default/deploy/DemoWeb.war/WEB-INF/jboss-web.xml[7,17]

-----------------------------------------------------------------------------------------------------------------------------------------------
Error : jboss server log :

Caused by: org.jboss.xb.binding.JBossXBException: Failed to parse source: Element type "ejb-ref-type" must be declared. @ vfszip:/C:/jboss-5.0.1.GA/server/default/deploy/DemoWeb.war/WEB-INF/jboss-web.xml[7,17]
at org.jboss.xb.binding.parser.sax.SaxJBossXBParser.parse(SaxJBossXBParser.java:203)
at org.jboss.xb.binding.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:168)
at org.jboss.deployers.vfs.spi.deployer.JBossXBDeployerHelper.parse(JBossXBDeployerHelper.java:199)
at org.jboss.deployers.vfs.spi.deployer.JBossXBDeployerHelper.parse(JBossXBDeployerHelper.java:170)
at org.jboss.deployers.vfs.spi.deployer.SchemaResolverDeployer.parse(SchemaResolverDeployer.java:132)
at org.jboss.deployers.vfs.spi.deployer.SchemaResolverDeployer.parse(SchemaResolverDeployer.java:118)
at org.jboss.deployers.vfs.spi.deployer.AbstractVFSParsingDeployer.parseAndInit(AbstractVFSParsingDeployer.java:256)
at org.jboss.deployers.vfs.spi.deployer.AbstractVFSParsingDeployer.parse(AbstractVFSParsingDeployer.java:188)
at org.jboss.deployers.spi.deployer.helpers.AbstractParsingDeployerWithOutput.createMetaData(AbstractParsingDeployerWithOutput.java:323)
... 29 more
Caused by: org.xml.sax.SAXException: Element type "ejb-ref-type" must be declared. @ vfszip:/C:/jboss-5.0.1.GA/server/default/deploy/DemoWeb.war/WEB-INF/jboss-web.xml[7,17]
at org.jboss.xb.binding.parser.sax.SaxJBossXBParser$MetaDataErrorHandler.error(SaxJBossXBParser.java:426)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.jboss.xb.binding.parser.sax.SaxJBossXBParser.parse(SaxJBossXBParser.java:199)
... 37 more
05:09:40,640 WARN Failed to process changes
org.jboss.deployers.client.spi.IncompleteDeploymentException: Summary of incomplete deployments (SEE PREVIOUS ERRORS FOR DETAILS):

DEPLOYMENTS IN ERROR:
Deployment "vfszip:/C:/jboss-5.0.1.GA/server/default/deploy/DemoWeb.war/" is in error due to the following reason(s): org.xml.sax.SAXException: Element type "ejb-ref-type" must be declared. @ vfszip:/C:/jboss-5.0.1.GA/server/default/deploy/DemoWeb.war/WEB-INF/jboss-web.xml[7,17]

at org.jboss.deployers.plugins.deployers.DeployersImpl.checkComplete(DeployersImpl.java:863)
at org.jboss.deployers.plugins.main.MainDeployerImpl.checkComplete(MainDeployerImpl.java:806)
at org.jboss.system.server.profileservice.hotdeploy.HDScanner.scan(HDScanner.java:293)
at org.jboss.system.server.profileservice.hotdeploy.HDScanner.run(HDScanner.java:221)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:181)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:205)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)

 
 
Leave a Reply
 
Name (required)
Mail (will not be published) (required)
Website
(Case Insensitive)
Bold textItalic textUnderLine textSource CodeHorizontal rulerExternal Link
Post
Preview