首页 > 代码库 > java远程调用rmi入门实例

java远程调用rmi入门实例

      RMI是Java的一组拥护开发分布式应用程序的API。RMI使用Java语言接口定义了远程对象,它集合了Java序列化和Java远程方法协议(Java Remote Method Protocol)。简单地说,这样使原先的程序在同一操作系统的方法调用,变成了不同操作系统之间程序的方法调用,由于J2EE是分布式程序平台,它以RMI机制实现程序组件在不同操作系统之间的通信。比如,一个EJB可以通过RMI调用Web上另一台机器上的EJB远程方法。

    用例服务器端结构大概如此

    

首先定义要传送的实体类,因为要网络传输,必须实现序列化

User.java

package com.yiyuwanglu.rmi.entity;

import java.io.Serializable;

public class User implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8400949180923337013L;
	private String id;
	private String name;
	private int age;

	

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	}

	public int getAge() {
		return age;
	}

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

}
第二定义实体接口,必须继承 java.rmi.Remote

UserService

package com.yiyuwanglu.rmi.service;

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

import com.yiyuwanglu.rmi.entity.User;

public interface UserService extends Remote {
	public User getUserById(String id)throws RemoteException;
}
第三,实现接口 记得继承UnicastRemoteObject

UserServiceImpl

package com.yiyuwanglu.rmi.service.impl;

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

import com.yiyuwanglu.rmi.entity.User;
import com.yiyuwanglu.rmi.service.UserService;

public class UserServiceImpl extends UnicastRemoteObject  implements UserService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6222175854495075991L;

	public UserServiceImpl() throws RemoteException {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public User getUserById(String id) {
		User user=new User();
		user.setId(id);
		user.setAge(100);
		user.setName("测试");
		return user;
	}

}

服务器端启动

Program.java

package com.yiyuwanglu.rmi.program;

import java.net.MalformedURLException;
import java.nio.channels.AlreadyBoundException;
import java.rmi.Naming;  
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;  

import com.yiyuwanglu.rmi.service.UserService;
import com.yiyuwanglu.rmi.service.impl.UserServiceImpl;
  
public class Program{  
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        try {  
            UserService userService=new UserServiceImpl();  
            //注册通讯端口  
            LocateRegistry.createRegistry(6600);  
            //注册通讯路径  
            Naming.rebind("rmi://127.0.0.1:6600/userService", userService);  
            System.out.println("Service Start!");  
        }  catch (RemoteException e) {
            System.out.println("创建远程对象发生异常!");
            e.printStackTrace();
        } catch (AlreadyBoundException e) {
            System.out.println("发生重复绑定对象异常!");
            e.printStackTrace();
        } catch (MalformedURLException e) {
            System.out.println("发生URL畸形异常!");
            e.printStackTrace();
        } 
    }  
}  

客户端测试,最好找另一台机子(当然同一台机子也许,不过为了模拟真实情况,还是另一台机子,记得iP要换)创建客户端,切记:相关的类的包的路径要跟服务器一样

例如:客户端的 UserService

package com.yiyuwanglu.rmi.service;
最后客户端调用

package com.yiyuwanglu.rmi.program;

import java.rmi.Naming;

import com.yiyuwanglu.rmi.entity.User;
import com.yiyuwanglu.rmi.service.UserService;

public class Client {
	 public static void main(String[] args){  
	        try{  
	            //调用远程对象,注意RMI路径与接口必须与服务器配置一致  
	        	UserService userService=(UserService)Naming.lookup("rmi://127.0.0.1:6600/userService");  
	            User user =userService.getUserById("1245");
	             System.out.println(user.getName());
	        }catch(Exception ex){  
	            ex.printStackTrace();  
	        }  
	    }  
}


先启动服务器端
Program
然后启动客户端
Client
显示结果分别是

Service Start!

客户端

测试



java远程调用rmi入门实例