rmi简单使用

最近在做桌面应用,要使用rmi,于是学习下rmi的简单使用.
服务端:
package rmi.service;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
import rmi.model.*;

//此为远程对象调用的接口,必须继承Remote类  
public interface PersonService extends Remote {
	public List<PersonEntity> GetList() throws RemoteException;
	public String get(String val) throws RemoteException;
}



package rmi.service;

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

public interface AnimalService extends Remote {

	public String getName(String dog) throws RemoteException;
}

package rmi.serviceImpl;

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

import rmi.service.AnimalService;

public class AnimalServiceImpl extends UnicastRemoteObject implements AnimalService {

	public AnimalServiceImpl() throws RemoteException {
		super();
	}

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

	@Override
	public String getName(String dog) throws RemoteException {
		return "reci dog :" + dog;
	}

}

package rmi.serviceImpl;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.LinkedList;
import java.util.List;
import rmi.model.PersonEntity;
import rmi.service.*;

//此为远程对象的实现类,须继承UnicastRemoteObject  
public class PersonServiceImpl extends UnicastRemoteObject implements
		PersonService {
	public PersonServiceImpl() throws RemoteException {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<PersonEntity> GetList() throws RemoteException {
		// TODO Auto-generated method stub
		System.out.println("Get Person Start!");
		List<PersonEntity> personList = new LinkedList<PersonEntity>();

		PersonEntity person1 = new PersonEntity();
		person1.setAge(25);
		person1.setId(0);
		person1.setName("Leslie");
		personList.add(person1);

		PersonEntity person2 = new PersonEntity();
		person2.setAge(25);
		person2.setId(1);
		person2.setName("Rose");
		personList.add(person2);

		return personList;
	}

	@Override
	public String get(String val) throws RemoteException {
		System.out.println("receive data:"+val);
		return "server>>: hello client";
	}

}


package rmi.model;

import java.io.Serializable;

//注意对象必须继承Serializable  
public class PersonEntity implements Serializable {
	private int id;
	private String name;
	private int age;

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

	public int getId() {
		return id;
	}

	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;
	}
}


package rmi.remotingservice;

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

import rmi.service.*;
import rmi.serviceImpl.*;

public class Program {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			PersonService personService = new PersonServiceImpl();
			
			AnimalService animalService = new AnimalServiceImpl();
			// 注册通讯端口
			LocateRegistry.createRegistry(6600);
			// 注册通讯路径
			Naming.rebind("rmi://0.0.0.0:6600/PersonService", personService);
			
			Naming.rebind("rmi://0.0.0.0:6600/AnimalService", animalService);
			
			System.out.println("Service Start!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 

 客户端:
package rmi.remotingclient;

import java.rmi.Naming;
import java.util.List;
import rmi.model.PersonEntity;
import rmi.service.*;

public class Program {
	public static void main(String[] args) {
		try {
			// 调用远程对象,注意RMI路径与接口必须与服务器配置一致
			PersonService personService = (PersonService) Naming
					.lookup("rmi://192.168.0.193:6600/PersonService");
			
			AnimalService animalService = (AnimalService) Naming
			.lookup("rmi://192.168.0.193:6600/AnimalService");
			List<PersonEntity> personList = personService.GetList();
			for (PersonEntity person : personList) {
				System.out.println("ID:" + person.getId() + " Age:"
						+ person.getAge() + " Name:" + person.getName());
			}
			String val = personService.get("hello server");
			System.out.println(val);
			System.out.println(animalService.getName("jack"));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
 
 打印信息,我就不上传了。需要的朋友可以自己试下。

你可能感兴趣的:(rmi)