Java远程方法调用(RMI)

Java中的远程方法调用(RMI,Remote Method Invocation),是一种计算机之间对象互相调用对方方法,启动对方进程的一种机制。使用这种机制,某一台计算机上的对象在调用另外一台计算机上的方法时,使用的程序语法规则和在本地机上对象间的方法调用的语法规则一样。

RMI应用程序通常包括两个独立的程序:服务器程序和客户机程序。典型的服务器应用程序将创建多个远程对象,使这些远程对象能够被引用,然后等待客户机调用这些远程对象的方法。而典型的客户机程序则从服务器中得到一个或多个远程对象的引用,然后调用远程对象的方法。RMI为服务器和客户机进行通信和信息传递提供了一种机制。 

构建RMI应用的步骤基本如下:
      服务器端
•  创建远程接口
•  实现远程接口提供的服务(即方法)
•  启动注册服务器, 创建、注册服务实例
      客户端:
•  根据注册的服务名查找服务得到实例的引用,调用实例方法

-----------------------------分割线-------------------------

在RMI分布式应用系统中,服务器与客户机之间传递的Java对象必须是可序列化的对象,不可序列化的对象不能在对象流中进行传递,所以必须继承Serializable。上面用到的就是一个PersonEntity类,封装了姓名和年龄数据。


/**
 * 对象必须继承Serializable
 */
public class PersonEntity implements Serializable
{
	private static final long	serialVersionUID	= 1L;
	private String					name;
	private int						age;

	public void setName(String name)
	{
		this.name = name;
	}

	public String getName()
	{
		return name;
	}

	public void setAge(int age)
	{
		this.age = age;
	}

	public int getAge()
	{
		return age;
	}
}

创建远程接口:

/**
 * 此为远程服务接口类,必须继承Remote类
 */
public interface PersonService extends Remote
{
	public List
  
  
   
    GetList() throws RemoteException;

	public void addAPerson(PersonEntity personEntity) throws RemoteException;

	public void sayHello() throws RemoteException;

	public void setName(String name) throws RemoteException;
}

  
  

实现远程接口提供的服务(即方法):


/**
 * 远程对象的实现类,须继承UnicastRemoteObject
 */
public class PersonServiceImpl extends UnicastRemoteObject implements PersonService
{
	private static final long	serialVersionUID	= 1L;
	private	static String nameString = null;
	
	public PersonServiceImpl() throws RemoteException
	{
		super();
	}

	@Override
	public List
  
  
   
    GetList() throws RemoteException
	{
		System.out.println("远程方法被调用了~");
		List
   
   
    
     personList = new LinkedList
    
    
     
     ();

		PersonEntity person1 = new PersonEntity();
		person1.setAge(20);
		person1.setName("张三");
		personList.add(person1);

		PersonEntity person2 = new PersonEntity();
		person2.setAge(22);
		person2.setName("李四");
		personList.add(person2);

		return personList;
	}

	@Override
	public void sayHello() throws RemoteException
	{
		System.out.println("Hello, " + nameString);
	}

	@Override
	public void setName(String name) throws RemoteException
	{
		nameString = name;
	}

}

    
    
   
   
  
  

启动注册服务器和创建、注册服务实例:

public class ServiceProgram
{
	public static void main(String[] args)
	{
		try
		{
			PersonService personService = new PersonServiceImpl();
			
			PersonEntity person1 = new PersonEntity();
			person1.setAge(20);
			person1.setName("张三");
			personService.addAPerson(person1);
			
			PersonEntity person2 = new PersonEntity();
			person2.setAge(22);
			person2.setName("李四");
			personService.addAPerson(person2);
			
			LocateRegistry.createRegistry(6600);// 注册通讯端口
			Naming.rebind("rmi://127.0.0.1:6600/PersonService", personService); // 注册通讯路径
			System.out.println("远程服务已经已启动");
		} catch (RemoteException e)
		{
			System.out.println("无法联系注册表 ");
		} catch (MalformedURLException e)
		{
			System.out.println("名称不是适当格式化的 URL");
		}
	}
}

客户端:根据注册的服务名查找服务得到实例的引用,调用实例方法

public class ClientProgram
{
	public static void main(String[] args)
	{
		PersonService personService = null;

		try
		{
			// 调用远程对象,注意RMI路径与接口必须与服务器配置一致
			personService = (PersonService) Naming.lookup("rmi://127.0.0.1:6600/PersonService");
			personService.setName("Shawn");
			personService.sayHello();
			
			List
   
   
    
     personList = personService.GetList();
			for (PersonEntity person : personList)
			{
				System.out.println( " 姓名: " + person.getName()+"   年龄: " + person.getAge() );
			}
		} catch (MalformedURLException e)
		{
			System.out.println("名称不是适当格式化的 URL");
		} catch (RemoteException e)
		{
			System.out.println("Naming.lookup,无法联系注册表 ");
		} catch (NotBoundException e)
		{
			System.out.println("当前未绑定名称 ");
		}
	}
}

   
   


资源下载: Java RMI(远程方法调用)Demo

参考资料:Java序列化和反序列化的内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值