Sagewire Logo

JNDI, session beans and JBoss

4 Message(s) by 2 Author(s) originally posted in java beans


From: cold80 Date:   Friday, February 02, 2007
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'd be really appreciated...

Cold


From: Per Newgro Date:   Saturday, February 03, 2007
Hallo cold80@xxxxxxxxxxx,

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


I expect Hello ist your BusinessInterface. But the lookup only returns the
HomeInterface. AFAIK you've 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 isn't bound under that name (see META-INF/ejb-jar.xml for
details).

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


Because you do not 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
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'd 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've a defined way for accessing your
beans.

Cheers
Per


From: cold80 Date:   Monday, February 05, 2007
I did not 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;

@xxxxxxxxxxx
@xxxxxxxxxxx(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 annotation s
on the code. Do you think I've to write the ejb-jar.xml file as
well? I mean, is it because I did not write it that I can not 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@xxxxxxxxxxx,
> 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());
> }
> }
I expect Hello ist your BusinessInterface. But the lookup only returns the
HomeInterface. AFAIK you've 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 isn't bound under that name (see META-INF/ejb-jar.xml for
details).
> 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
Because you do not 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
>
> 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'd 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've a defined way for accessing your
beans.
Cheers
Per






From: Per Newgro Date:   Monday, February 05, 2007
Hallo cold80@xxxxxxxxxxx,

I did not 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
package examples.session.stateless;
public interface Hello {
public String hello();
}


In ejb-3.0 spec the "default" for the business interface (here Hello) is
@xxxxxxxxxxx U have to specify it as @xxxxxxxxxxx

and a class implementation
package examples.session.stateless;
import JAVAx.ejb.Stateless;
import JAVAx.ejb.Remote;
@xxxxxxxxxxx
@xxxxxxxxxxx(Hello.class)
public class HelloBean implements Hello{
public String hello(){
System.out.println("hello()");
return "Hello world!!!";
}
}


The @xxxxxxxxxxx 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've to write the ejb-jar.xml file as
well?


Nope. Benefit of EJB3 is afaik the simplification of deployment to. SO
ejb-jar.xml isn't required (but possible)

I mean, is it because I did not write it that I can not 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



Next Message: Online Store Application Using Spring + Hibernate + Velocity + Ajax


Blogs related to JNDI, session beans and JBoss

Re: How to retrieve the LocalObject of a stateless SessionBean
IHelloWorld HelloWorld session> beans> jboss> the client java code: package meinclient; import javax.naming.*; import java.rmi.*; import javax.rmi. ...

Hibernate Users :: Doubts about configuration - Dialects
I'm also using spring 2.0, jboss 4.0.5, with Eclipse IDE. I'm having a strange problem when configuring my application. I've a class to control hibernate session like the following: ...

Re: sharing same session across multiple webapp
Spring framework or something like Jboss.... something that lets you >> share a bean. >> Granted... they huge fat frameworks if thats all you want to do.. >> >> I think you need to qualify the question a little. ...

Connection problems when using Spring+Hibernate+jboss jndi
<session-factory> java:/EDBServer org.hibernate.dialect.PostgreSQLDialect ...

Re: Jackrabbit 1.2.3 on JBoss 4.0.4 GA
And I have also figured out the problem. The JNDI name "jcr/local" is available to applications running in the same VM only. I was trying to lookup "java:jcr/local" from a main class while my Jackrabbit was deployed on JBoss. ...

[JBoss Seam] - Conversion Error on Registration example
17:46:35064 INFO reading properties from: /jndi.properties | 17:46:35079 INFO initializing Seam | 17:46:35079 INFO scanning: /C:/Java/jboss-4.0.5.GA/server/default/tmp/deploy/tmp59795reg-ear-1.0.ear-contents/reg-ejb-1.0.jar ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional