Section 18 RMI

Remote Method Interface:

Client calls remote method on the stub. So the Client is like making a local call.

The stub contacts the server, transfers information about the call and wait for a return for the server. Then stub unpacks the information and return vales to the client objects. A stub is to do the low-level communication.

The server has a skeleton. The skeleton receives the request and unpacks the information for the call and then invokes the real method on the service object. Then the skeleton gets the return values and packs it up and ships it back to the client.

Note: Skeleton is not used since Java 1.2. Stub can be generated automatically since Java 5. You don't need to call rmic.

Simple example of a client calling remote method in the server:

Serve Side:

1. Make a Server Remote Interface that is exposed to the Client. Every method throwsRemoteException. Every argument and return values are primitives or Serializable.

2. Implement the Server extending UnicastRemoteObject and implements the Server Remote Interface. The constructor should declares a RemoteException. Register the service with RMI registry using Naming.rebind("ServiceName", serviceObject)

3. Generate the stubs and skeletons using rmic ServerImpl, generating ServerImpl_Stub.class and ServerImpl_Skel.class.

4. Start the RMI registery using rmiregistry.

5. Start the Server.


Client Side:

1. Lookup on the RMI register using 

Naming.lookup("rmi://127.0.0.1/ServiceName");

2. RMI registry return the stub object. You must have stub class so that JVM can deserialize it. You can copy the ServerImpl_Stub.class from the Server.

Server server = (Server)Naming.lookup("rmi://127.0.0.1/ServiceObject");

3. Invoke a method on the stub.

server.doThing();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值