关于EJB3.0的入门的一点疑问

08-10-17 leauch
关于EJB3.0的入门的一点疑问
我用IDE开发一个入门的打印hello这样的程序
首先开发的是EJB程序。然后经过JBOSS_4.2.2采取直接部署的方式进行。
然后开发的是EJB程序的Client进行调用,在此过程中我加入了JBOSS_4.2.2的client下的JAR包
然后运行主方法在此方法运行前我写了jndi.properties文件里面的内容如下:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
然后运行主程序进行调用,这时报错为:
Exception in thread "main" javax.naming.NameNotFoundException: FirstEjb not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
at org.jnp.server.NamingServer.lookup(NamingServer.java:267)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:667)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.lvye.ejb.FirstEjbClient.main(FirstEjbClient.java:11)
我估计是jndi.properties配置文件有错吗,但是又在网上查不到是什么错误。所以想请教下大家帮我看下。也请banq大哥帮小弟看下。EJB我也是感兴趣才看下的。本想一个入门应该不难但是一时半时不知道从何入手了。所以请大家帮忙解决下。小弟先谢了!

xmuzyu
2008-10-17 10:57
估计是LZ没有用正确的JNDI名去查找,默认情况下用以下第一种方式查找。"FirstEJB/remote"
第一种:也是默认的,"beanname/remote"
或者也可以用Jboss专用的RemoteBinding Annotation来注解。
第二种:使用@RemoteBinding注释。@RemoteBinding(jndiBinding="jndiName")

leauch
2008-10-17 11:17
我把所有代码贴出来看下:

首先开发的是EJB的开发:
接口:
package com.lvye.ejb;
public interface FirstEjb {
public String sayHello(String name) ;
}
接口实现类:
package com.lvye.ejb;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless
@Remote
public class FirstEjbBean implements FirstEjb {
public String sayHello(String name) {
return "Hello,"+name;
}
}
上述方法开发完成之后,我采用的是直接用JBOSS服务器部署的方式,不是打包的方法,但是原理其实也一样
这时我去开发客户端调用上述方法:

客户端代码如下:
package com.lvye.ejb;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class FirstEjbClient {
public static void main(String[] args) throws NamingException {
InitialContext ctx = new InitialContext() ;
FirstEjb ejb = (FirstEjb) ctx.lookup("FirstEjb/Remote") ;
String s = ejb.sayHello("张三") ;
System.out.println(s) ;
}
}

jndi.properties文件如下:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost

在此开发过程中我已把JBOSS下的Client下的所有JAR包加入到CLASSPATH下面。

此时运行上述CLIENT中的MAIN方法报错:
xception in thread "main" javax.naming.NameNotFoundException: FirstEjb not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
at org.jnp.server.NamingServer.lookup(NamingServer.java:267)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:667)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.lvye.ejb.FirstEjbClient.main(FirstEjbClient.java:11)

7upcat
2008-10-18 21:14
好像是你写错了吧..
方式一.
@Remote
接口名....


@Stateless
实现 implments 接口

方式二.
接口

@Stateless
@Remote (接口.class)
实现

可能是这个原因,你重新试一下子吧 : )