選擇語言
繁體中文
繁體中文 Traditional Chinese
简体中文
简体中文 Simplified Chinese
English
English English
日本語
日本語 Japanese
AI 助手

今日情報速問。不用選範圍,直接問今天值得看什麼。

完整分析

通知訊訊

暫無新訊訊

public class HelloClientJNDI { static Hello helloImpl; public static void main(String[] args) throws Exception { ORB orb = ORB.init(args, null); Hashtable env = new Hashtable(5, 0.75f); env.put("java.naming.corba.orb", orb); Context ic = new InitialContext(env); Hello helloRef = HelloHelper.narrow((org.omg.CORBA.Object)ic.lookup("corbaname::127.0.0.1:1050#Hello")); System.out.println(helloRef.sayHello()); } } CORBA网络流量分析 servant side 服务端流量大致分为两个部分: 获取Naming Service 注册servant side 获取Naming Service的流量如下: 在返回的响应中,拿到了RootPOA: 对应的代码为: 接着检测获取到的NamingService对象是否为NamingContextExt类的示例: 对应代码: 最后发送op=to_name和op=rebind两个指令: 分别为设置引用名,和设置绑定信息,来看一下op=rebind的数据包: 这里通过IOR信息表示了servant side的相关rpc信息。 client side 这里以NameServer方式生成stub为例: 获取nameservice、op=_is_a判断 根据引用名获取servant side的接口Stub 发送方法名,调用远程方法,得到结果 分别对应代码步骤: RMI-IIOP RMI-IIOP出现以前,只有RMI和CORBA两种选择来进行分布式程序设计,二者之间不能协作。RMI-IIOP综合了RMI和CORBA的优点,克服了他们的缺点,使得程序员能更方便的编写分布式程序设计,实现分布式计算。 Demo: RMI-IIOP远程调用 参考文档Tutorial: Getting Started Using RMI-IIOP所述,一共四个步骤,对应的文件如下: 定义远程接口类:HelloInterface.java 编写实现类:HelloImpl.java, 实现接口HelloInterface 编写服务端类:HelloServer.java, RMI服务端实例远程类,将其绑定到name service中 编写客户端类:HelloClient.java, 调用远程方法sayHello() 实现接口类,必须要实现Remote远程类,且抛出java.rmi.RemoteException异常。 HelloInterface.java 代码 import java.rmi.Remote; public interface HelloInterface extends java.rmi.Remote { public void sayHello( String from ) throws java.rmi.RemoteException; } 实现接口类,必须写构造方法调用父类构造方法,给远程对象初始化使用,同时要实现一个方法给远程调用使用(sayHello()) HelloImpl.java 代码 import javax.rmi.PortableRemoteObject; public class HelloImpl extends PortableRemoteObject implements HelloInterface { public HelloImpl() throws java.rmi.RemoteException { super(); // invoke rmi linking and remote object initialization } public void sayHello( String from ) throws java.rmi.RemoteException { System.out.println( "Hello from " + from + "!!" ); System.out.flush(); } } 编写服务端,创建servant实例,绑定对象。 HelloServer.java 代码 import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import java.util.Hashtable; public class HelloServer { public final static String JNDI_FACTORY = "com.sun.jndi.cosnaming.CNCtxFactory"; public static void main(String[] args) { try { //实例化Hello servant HelloImpl helloRef = new HelloImpl(); //使用JNDI在命名服务中发布引用 InitialContext initialContext = getInitialContext("iiop://127.0.0.1:1050"); initialContext.rebind("HelloService", helloRef); System.out.println("Hello Server Ready..."); Thread.currentThread().join(); } catch (Exception ex) { ex.printStackTrace(); } } private static InitialContext getInitialContext(String url) throws NamingException { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY); env.put(Context.PROVIDER_URL, url); return new InitialContext(env); } } 编写客户端类,远程调用sayHello()方法。 HelloClient.java 代码 import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import java.util.Hashtable; public class HelloClient { public static void main( String args[] ) { Context ic; Object objref; HelloInterface hi; try { Hashtable env = new Hashtable(); env.put("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory"); env.put("java.naming.provider.url", "iiop://127.0.0.1:1050"); ic = new InitialContext(env); // STEP 1: Get the Object reference from the Name Service // using JNDI call. objref = ic.lookup("HelloService"); System.out.println("Client: Obtained a ref. to Hello server."); // STEP 2: Narrow the object reference to the concrete type and // invoke the method. hi = (HelloInterface) PortableRemoteObject.narrow( objref, HelloInterface.class); hi.sayHello( " MARS " ); } catch( Exception e ) { System.err.println( "Exception " + e + "Caught" ); e.printStackTrace( ); } } }