java rmi实际当中的详细步骤

详细步骤:

1:接口

package com.steven.remote;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Product extends Remote {
 String getDescription() throws RemoteException;
}

2:实现类

package com.steven.remote;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class ProductImpl extends UnicastRemoteObject implements Product {
 private String Desc;
 public ProductImpl(String name)throws RemoteException {
  // TODO Auto-generated constructor stub
  Desc=name;
 }
 public String getDescription() throws RemoteException {
  // TODO Auto-generated method stub
  return "This is "+Desc+" product";
 }

}
3:服务端

package com.steven.remote;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class ProductImpl extends UnicastRemoteObject implements Product {
 private String Desc;
 public ProductImpl(String name)throws RemoteException {
  // TODO Auto-generated constructor stub
  Desc=name;
 }
 public String getDescription() throws RemoteException {
  // TODO Auto-generated method stub
  return "This is "+Desc+" product";
 }

}

4:客户端

package com.steven.remote;

import java.rmi.Naming;
import java.rmi.RMISecurityManager;

public class ProductClient {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.println("begin to invoke remote method");
  //System.setSecurityManager(new RMISecurityManager());
  String url = "rmi://192.168.5.155:1099/";
  try {
   System.out.println("1");
   Product c1 = (Product) Naming.lookup(url + "toaster");
   Product c2 = (Product) Naming.lookup(url + "microwave");
   System.out.println("2");
   System.out.println(c1.getDescription());
   System.out.println("3");
   System.out.println(c2.getDescription());
  } catch (Exception ex) {
   System.out.println("error " + ex);
  }
 }
}

5:注意事项 

1:请注意客户端代码如果添加安全认证,则远程调用会出现访问拒绝异常.

2:客户端需要: Product.class ,ProductClient .class,ProductImpl_Stub.class(接口,客户端类,存根)

3:存根的生成:a:先编译所有类生成.class文件.b:在dos窗口下使用 rmic  ProductImpl(实现类)

4:实现类必须继承UnicastRemoteObject

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值