2:远程方法调用:
前提:被调用者实现了REMOTE接口,适用于不在同一模块中的ejb,servlet.
public static void main(String[] args) ...{ MapSessionHome mapSessionHome = null; MapSession mapSession = null; InitialContext initContext = null; final String JNDIName = "ejb/co/test/bean/MapSessionHome"; try ...{ System.out.println("in MapSessionClient!!!!!!!"); initContext = new InitialContext(); Object obj = initContext.lookup(JNDIName); mapSessionHome = (MapSessionHome) PortableRemoteObject.narrow( obj, MapSessionHome.class); mapSession = mapSessionHome.create(); Person person1 = new Person("zhangsan", 100); mapSession.setMapValue("key1",person1); Person tempPerson = (Person)mapSession.getMapValue("key1"); tempPerson.setName("lisi"); tempPerson.setAge(500); System.out.println("before changed: " + tempPerson.getName() + "---" + tempPerson.getAge()); Person changedPerson = (Person)mapSession.getMapValue("key1"); System.out.println("after changed: " + changedPerson.getName() + "---" + changedPerson.getAge()); } catch (Exception e) ...{ e.printStackTrace(); System.exit(0); } }
值得一提的是,在远程调用中,对一个object的操作,经过了corba处理,是不在同一内存块中进行的。具体到上面的代码,tempPerson的改变,不影响changedPerson的值(其实理所当然,一个是远程的对象,你个是本地内存对象)。
3:远程调用:适用于不在同一机器的远程调用:
对于websphere:
Initial Factory: (INITIAL_CONTEXT_FACTORY): com.ibm.websphere.naming.WsnInitialContextFactory Provider URL: (PROVIDER_URL): iiop://server ip:2809/
其中:server ip为ejb容器ip地址.必须注意:在websphere服务器的配置中,有一项orb bootstrap setting的配置,它的默认配置如下:
Port:2809
hostname:localhost
其中,hostname必须改为server的ip地址,
hostname:192.168.0.81
否则,远程调用不能成功. 调用代码如下:
public static void main(String[] args) ...{ System.out.println("in MapSessionRemoteTest"); MapSessionHome mapSessionHome = null; MapSession mapSession = null; String JNDIName = "ejb/co/test/bean/MapSessionHome"; Properties p = new Properties(); p.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory"); p.put(Context.PROVIDER_URL,"iiop://192.168.0.81:2809/"); InitialContext initContext; try ...{ initContext = new InitialContext(p); Object obj = initContext.lookup(JNDIName); mapSessionHome = (MapSessionHome) PortableRemoteObject.narrow( obj, MapSessionHome.class); mapSession = mapSessionHome.create(); Person person1 = new Person("zhangsan", 100); mapSession.setMapValue("key1",person1); Person tempPerson = (Person)mapSession.getMapValue("key1"); tempPerson.setName("lisi"); tempPerson.setAge(500); System.out.println("before changed: " + tempPerson.getName() + "---" + tempPerson.getAge()); Person changedPerson = (Person)mapSession.getMapValue("key1"); System.out.println("after changed: " + changedPerson.getName() + "---" + changedPerson.getAge()); } catch (Exception e) ...{ // TODO Auto-generated catch block e.printStackTrace(); } }