Myeclipse + Jboss开发EJB3

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值