aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes EJB3 Session bean and JBoss 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB3 Session bean and JBoss 5" Watch "EJB3 Session bean and JBoss 5" New topic
Author

EJB3 Session bean and JBoss 5

Logan Lee
Greenhorn

Joined: Oct 28, 2009
Posts: 18

Hello,
I am trying to learn EJB3 with the book "EJB3 in action".
As a starting point, I wanted to create a very simple EJB3 stateless session bean that returns a String object.
As a client I am using a simple servlet.
The application server I am using is JBoss 5.1.0.GA.

I have created:
- ICatalogService: the remote interface
- CatalogService: the stateless session bean
- ejb-jar.xml deployment descriptor

Unfortunately, when I deploy the EAR file, I get this error:


I already had a look at the new ejb-jar XSD at http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd
and it states:
Either both the local-home and the local elements or both the home and the remote elements must be specified.


I now am a bit confused. I thought the EJB Home interface was something of the past (EJB 2.x) and wasn't needed anymore in EJB3.
How can I solve this?
Thanks.

This is my servlet:

The remote interface:

The bean:


ejb-jar.xml:
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10202
    
166

First of all, for EJB3 you no longer need the ejb-jar.xml. But if you want to package it then its OK too.

EJB CatalogService has defined EJB2.x remote component interface of be.work.apps.bookstore.ejb.service.ICatalogService but has no home; ; Incomplete EJB2.x View [JBMETA-130]




Unfortunately, one of the most common confusion with the remote/local in EJB3 is the use of the remote/business-remote (or local/business-local). A "remote" element in the xml represents the EJB2.x remote interface, which is expected to extend from EJBObject. Whereas, the business-remote element is what needs to be used for EJB3 beans which expose the POJO interface. So in your example you have to change it to:



[My Blog] [JavaRanch Journal]
Logan Lee
Greenhorn

Joined: Oct 28, 2009
Posts: 18

Thanks Jaikiran, that solved the deployment problem. (Strange that in the EJB3 in action book they use remote/local)

but, now I receive a nullpointer in my servlet: catalogService is null. It seems the bean isn't injected...


so I decided to write a small client to test the bean via JNDI lookup. the lookup fails with a ClassCastException.
Exception in thread "main" java.lang.ClassCastException: javax.naming.Reference

Java client:

jndi.properties:


As you can see the bean is successfully mapped to a JNDI name:

Also in the JBoss JMX JNDI Global NS everything looks fine.


I'm puzzled. What am I doing wrong here?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10202
    
166

Logan Lee wrote:
but, now I receive a nullpointer in my servlet: catalogService is null. It seems the bean isn't injected...



Your web.xml will require the 2.5 xsd http://www.coderanch.com/t/426854/EJB-Other-Java-EE-Technologies/java/EJB-doing-nothing-jboss-GA#1894841
Logan Lee wrote:
so I decided to write a small client to test the bean via JNDI lookup. the lookup fails with a ClassCastException.
Exception in thread "main" java.lang.ClassCastException: javax.naming.Reference



For this you will require the jbossall-client.jar and all the jar files listed in the META-INF/MANIFEST.MF of that jar, to be present in the client's classpath. Those jars are available in the JBOSS_HOME/client folder.
Logan Lee
Greenhorn

Joined: Oct 28, 2009
Posts: 18

Thanks for the advice Jaikiran, I am able to access the EJB now.
Changing the XSD to version 2.5 solved part of the Direct Injection problem, there was another catch.

After I changed the web-app element to:

I stumbled upon the following error:
java.lang.IllegalStateException: Resolution should not happen via injection container

Apparently this is a bug:
When a servlet is packaged standalone (outside ear) and an EJB is packaged standalone, the EJB can't be injected into the servlet without specifying a JNDI name somewhere.

https://jira.jboss.org/jira/browse/JBAS-6332

So to solve this I added the mappedName attribute to the @EJB annotation. The mappedName element should contain the JNDI lookup name of your interface(remote or local)
Csaba Toth
Greenhorn

Joined: Sep 13, 2010
Posts: 6
Jaikiran Pai wrote:
Logan Lee wrote:
so I decided to write a small client to test the bean via JNDI lookup. the lookup fails with a ClassCastException.
Exception in thread "main" java.lang.ClassCastException: javax.naming.Reference



For this you will require the jbossall-client.jar and all the jar files listed in the META-INF/MANIFEST.MF of that jar, to be present in the client's classpath. Those jars are available in the JBOSS_HOME/client folder.


I faced this exception during test runs. I added the JBOSS_HOME/client folder to the classpath of the xyzTest.java file, but I still get the exception. What am I doing wrong?
Csaba Toth
Greenhorn

Joined: Sep 13, 2010
Posts: 6
Csaba Toth wrote:
Jaikiran Pai wrote:
I faced this exception during test runs. I added the JBOSS_HOME/client folder to the classpath of the xyzTest.java file, but I still get the exception. What am I doing wrong?


Additional comments:
1. Only my test runs didn't work, when I deployed the application it could communicate. So there must be some problem with the difference between the execution environment, version numbers of jars, classpath in case of the test. I even installed the 5.1.0.GA jbossall-client from the JBOSS_HOME/client directory into my Maven repository, to use exactly the same versions. I use the jdk6 version of 5.1.0.GA JBoss, 1.6.0_21 SUN JDK.

2. if I remove the ejb-jar.xml from my EJB module, my app deploys OK, but it can't find the EJB any more, so not just the tests doesn't work, but the real life app any more.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EJB3 Session bean and JBoss 5