在Websphere上调用EJB2的远程接口

来源:互联网 发布:宁夏大学怎么样知乎 编辑:程序博客网 时间:2024/05/17 01:18

先部署ejb jar到websphere

在MyEclipse上新建调用ejb的项目,加入ejb接口,项目引用的jar包是

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/IBM"/>---IBM的JDK

 <classpathentry kind="lib" path="D:/IBM/WebSphere/AppServer/runtimes/com.ibm.ws.admin.client_6.1.0.jar"/>
 <classpathentry kind="lib" path="D:/IBM/WebSphere/AppServer/lib/j2ee.jar"/>
 <classpathentry kind="lib" path="lib/ejb-remote.jar"/>--部署到websphere上而生成的jar包, 路径类似  AppSrv02\installedApps\testNode02Cell\ejb-remote_jar.ear\ejb-remote.jar

调用远程接口的代码如下:

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import ejb.remote.interfaces.AccessBean;
import ejb.remote.interfaces.AccessBeanHome;


public class TestRemote {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  AccessBeanHome accessHome = null;
  AccessBean accessBean = null;
  String JNDIName="ejb/AccessBean";
  Properties p=new Properties();
  p.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
  p.put(Context.PROVIDER_URL,"iiop://192.168.83.124:2810/");
 
  InitialContext initContext;
  try{
   initContext=new InitialContext(p);
   Object obj= initContext.lookup(JNDIName);
   accessHome=(AccessBeanHome)PortableRemoteObject.narrow(obj, AccessBeanHome.class);
   accessBean=accessHome.create();
   accessBean.testRemoteEJB("lidong");
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }

}

 

原创粉丝点击