Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / JavaBeans / February 2007

Tip: Looking for answers? Try searching our database.

JNDI, session beans and JBoss

Thread view: 
cold80@libero.it - 02 Feb 2007 17:03 GMT
I was trying the first EJB example of the book "Mastering Enterprise
Java Beans". After building the bean and deploying it successfully I
had many problems making the client work. The client code is very
simple

public class Main {
   public static void main(String[] args) throws Exception{
       Context ctx=new InitialContext();

       Hello hello=(Hello)ctx.lookup("HelloBean");
       System.out.println(hello.hello());
   }
}

But I found many problems using the lookup method. Using the full
"path" for the interface "examples.session.stateless.Hello", as
specified in the book, I got

Exception in thread "main" javax.naming.NameNotFoundException:
examples.session.stateless.Hello not bound

Otherwise, using the line

Hello hello=(Hello)ctx.lookup("HelloBean");

I got the error

Exception in thread "main" java.lang.ClassCastException:
org.jnp.interfaces.NamingContext cannot be cast to
examples.session.stateless.Hello

So I think I was grabbing the wrong object from JBoss. Looking on many
posts and web pages I found that using

Hello hello=(Hello)ctx.lookup("HelloBean/remote");

I can get the right object. Why? It's really difficult for me to
understand the way JNDI is naming my resources...can you give me
informations about that? It would be really appreciated...

Cold
Per Newgro - 03 Feb 2007 10:42 GMT
Hallo cold80@libero.it,

> I was trying the first EJB example of the book "Mastering Enterprise
> Java Beans". After building the bean and deploying it successfully I
[quoted text clipped - 9 lines]
>     }
> }
I expect Hello ist your BusinessInterface. But the lookup only returns the
HomeInterface. AFAIK you have to do the following
Object o = new InitialContext().lookup("HelloBean");
HelloHome home = (HelloHome) PortableRemoteObject.narrow(o,
HelloHome.class);
HelloRemote remote = home.create();

With this i expect you deployed your bean under the name "HelloBean".

> But I found many problems using the lookup method. Using the full
> "path" for the interface "examples.session.stateless.Hello", as
> specified in the book, I got
>
> Exception in thread "main" javax.naming.NameNotFoundException:
> examples.session.stateless.Hello not bound
Because the bean is not bound under that name (see META-INF/ejb-jar.xml for
details).

> Otherwise, using the line
>
[quoted text clipped - 5 lines]
> org.jnp.interfaces.NamingContext cannot be cast to
> examples.session.stateless.Hello
Because you don't get a Hello instance. It's a home interface you get.

> So I think I was grabbing the wrong object from JBoss. Looking on many
> posts and web pages I found that using
[quoted text clipped - 4 lines]
> understand the way JNDI is naming my resources...can you give me
> informations about that? It would be really appreciated...
JNDI is a way to bind a name to a class and get a remote (or local) instance
of this class. The EJB spec requires the definition of a deployment
descriptor file ejb-jar.xml. Here you can define the bean environment
(jndi-name, home class, remote class etc.). The container reads this file
and configures your beans. So you have a defined way for accessing your
beans.

Cheers
Per
cold80@libero.it - 05 Feb 2007 08:23 GMT
I didn't mentioned that my example is developed using EJB 3.0. Maybe
the container is defining the local and remote interfaces for me, but
I just define a interface as

package examples.session.stateless;

public interface Hello {
   public String hello();
}

and a class implementation

package examples.session.stateless;

import javax.ejb.Stateless;
import javax.ejb.Remote;

@Stateless
@Remote(Hello.class)
public class HelloBean implements Hello{
   public String hello(){
       System.out.println("hello()");
       return "Hello world!!!";
   }
}

So, as you see, I'm not using ejb-jar.xml file to define the
informations needed by the container. Intead I'm writing annotations
on the code. Do you think I have to write the ejb-jar.xml file as
well? I mean, is it because I didn't write it that I can't access it?
Do you know where I can find informations about this matter on the
Internet (specific for Jboss, actually)?

Thank you again for your help

Cold

Per Newgro ha scritto:

> Hallo cold80@libero.it,
>
[quoted text clipped - 57 lines]
> Cheers
> Per
Per Newgro - 05 Feb 2007 10:38 GMT
Hallo cold80@libero.it,

> I didn't mentioned that my example is developed using EJB 3.0.
Aha.

> Maybe the container is defining the local and remote interfaces for me,
> but I just define a interface as
[quoted text clipped - 4 lines]
>     public String hello();
> }
In ejb-3.0 spec the "default" for the business interface (here Hello) is
@Local. U have to specify it as @Remote

> and a class implementation
>
[quoted text clipped - 11 lines]
>     }
> }
The @Remote annotation is afaik not required if you added it to Hello.

> So, as you see, I'm not using ejb-jar.xml file to define the
> informations needed by the container. Intead I'm writing annotations
> on the code. Do you think I have to write the ejb-jar.xml file as
> well?
Nope. Benefit of EJB3 is afaik the simplification of deployment to. SO
ejb-jar.xml is not required (but possible)

> I mean, is it because I didn't write it that I can't access it?
> Do you know where I can find informations about this matter on the
> Internet (specific for Jboss, actually)?
I strongly recommend you to read the ejb-3.0 spec. It's realy simple to
read. After this much of the new concepts are clear. Trust me.
You can download it here:
http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html

Tell me if problems still occur.

Cheers
Per


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.