Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

EJB3 Session bean and JBoss 5

 
Greenhorn
Posts: 18
Mac Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Logan Lee
Greenhorn
Posts: 18
Mac Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 https://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
Posts: 18
Mac Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic