AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

JNDI, session beans and JBoss

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> JavaBeans
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Fri Feb 02, 2007 11:03 pm    Post subject: JNDI, session beans and JBoss Reply with quote



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
Back to top
Per Newgro
Guest





PostPosted: Sat Feb 03, 2007 4:42 pm    Post subject: Re: JNDI, session beans and JBoss Reply with quote



Hallo cold80 (AT) libero (DOT) it,

Quote:
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 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".

Quote:
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).

Quote:
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 don't get a Hello instance. It's a home interface you get.


Quote:
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...
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
Back to top
Guest






PostPosted: Mon Feb 05, 2007 2:23 pm    Post subject: Re: JNDI, session beans and JBoss Reply with quote



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:

Quote:
Hallo cold80 (AT) libero (DOT) it,

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 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

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 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

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...
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
Back to top
Per Newgro
Guest





PostPosted: Mon Feb 05, 2007 4:38 pm    Post subject: Re: JNDI, session beans and JBoss Reply with quote

Hallo cold80 (AT) libero (DOT) it,

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


Quote:
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

@Local. U have to specify it as @Remote

Quote:

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!!!";
}
}
The @Remote annotation is afaik not required if you added it to Hello.


Quote:

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)

Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> JavaBeans All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.