1,开发接口
package com.myeclipseide.ejb3;
import java.io.Serializable;
public interface IMyBean extends Serializable {
public void doSomething();
}
2,实现接口的local和remote接口
package com.myeclipseide.ejb3;
import javax.ejb.Local;
@Local
public interface MyBeanLocal extends IMyBean{
}
package com.myeclipseide.ejb3;
import javax.ejb.Remote;
@Remote
public interface MyBeanRemote extends IMyBean{
}
3,实现类接口
package com.myeclipseide.ejb3;
import javax.ejb.Stateless;
@Stateless
public class MyBean implements MyBeanLocal, MyBeanRemote {
public void doSomething() {
// TODO Auto-generated method stub
System.out.println("Hello EJB World!");
}
}
4,编写测试类
package com.myeclipseide.ejb3;
import java.util.Hashtable;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.myeclipseide.ejb3.*;
public class MyBeanClient {
/**
* @param args
*/
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
InitialContext ctx = new InitialContext();
MyBeanRemote bean = ( MyBeanRemote) ctx.lookup(" com.myeclipseide.ejb3.MyBeanRemote");
bean.doSomething();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
出现错误1:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.myeclipseide.ejb3.MyBeanClient.main(MyBeanClient.java:23)
解决1:
Hashtable<String,String> prop=new Hashtable<String,String>();
prop.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
prop.put("java.naming.provider.url", "localhost:1099");
prop.put("java.naming.factory.url.pkgs", "org.jboss.naming");
try{
InitialContext ctx = new InitialContext(prop);
MyBeanRemote bean = (MyBeanRemote) ctx.lookup("com.myeclipseide.ejb3.MyBeanRemote");
bean.doSomething();
}catch(NamingException e){
e.printStackTrace();
}*/
出现问题2:
javax.naming.NameNotFoundException: com.myeclipseide.ejb3.MyBeanRemote 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:296)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
at sun.rmi.transport.Transport$1.run(Transport.java:153)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:466)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:707)
at java.lang.Thread.run(Thread.java:595)
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.myeclipseide.ejb3.MyBeanClient.main(MyBeanClient.java:35)
解决2:
import com.myeclipseide.ejb3.*;
更改MyBeanRemote bean = (MyBeanRemote) ctx.lookup("MyBean/remote");
//根据JNDI的名字,查找EJB ,此JNDI名字命名格式为:实现接口的类/remote (local)
令外一种解决方法:
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
props.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
InitialContext ctx;
try {
ctx = new InitialContext(props);
//Service service = (MyBeanRemote) ctx.lookup("ServiceBean/remote");
MyBeanRemote bean = (MyBeanRemote) ctx.lookup("MyBean/remote");
//service.sayHelloFromServiceBean();
bean.doSomething();
} catch (NamingException e) {
System.out.println(e.getMessage());
}