java rmi远程访问实例

package common;

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

public interface RemoteInterface extends Remote{
	
	public List<Student> getStudents ()  throws RemoteException ;

}
package common;

import java.io.Serializable;

public class Student implements Serializable{

	private static final long serialVersionUID = -8511318513177393420L;
	private int id;
	private String name;
	private String city;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String toString() {
		return "Student [city=" + city + ", id=" + id + ", name=" + name + "]";
	}
	public Student(int id, String name, String city) {
		super();
		this.id = id;
		this.name = name;
		this.city = city;
	}
	public Student(){}
	
	
}

package server;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import common.Student;

import common.RemoteInterface;

public class RemoteInterfaceImpl extends UnicastRemoteObject implements RemoteInterface{

	private static final long serialVersionUID = 2309898642675639793L;
	private static final Object lock = new Object();
	private static RemoteInterface instance;
	
	public static RemoteInterface getInstance()
	{
		if(instance==null)
		{
			synchronized (lock) {
				if(instance==null)
				{
					try {
						instance=new RemoteInterfaceImpl();
					} catch (RemoteException e) {
						e.printStackTrace();
					}
				}
				
			}
		}
		return instance;
		
	}
	
	protected RemoteInterfaceImpl() throws RemoteException {
		
		//super();
		
	}

	@Override
	public List<Student> getStudents()  throws RemoteException {
		List<Student> students=new ArrayList<Student>();
		students.add(new Student(1,"java","nanning"));
		students.add(new Student(2,"php","北京"));
		students.add(new Student(3,"C","广州"));
		students.add(new Student(4,"ASP","上海"));
		System.out.println(new Date()+" getStudents method called!");
		return students;
	}

}
package server;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class RMIServer {

	public static void main(String[] args) {
		
		System.out.println("RMI start...");
		
		if(System.getSecurityManager()==null)
		{
			System.out.println("System.getSecurityManager()==null");
			//System.setSecurityManager(new RMISecurityManager());
		}
		try {
			LocateRegistry.createRegistry(1099);
			Naming.rebind("MyChannelManager", RemoteInterfaceImpl.getInstance());
			
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		System.out.println("RMI Server ready...");
	}
}

package client;

import java.rmi.Naming;
import java.util.List;

import common.RemoteInterface;
import common.Student;

public class MyClient {

	public static void main(String[] args) {
		
		if(System.getSecurityManager()==null)
		{
			//System.setSecurityManager(new RMISecurityManager());
		}
		try {
			RemoteInterface remoteInterface=(RemoteInterface) Naming.lookup("rmi://192.168.0.23:1099/MyChannelManager");
			List<Student> students=remoteInterface.getStudents();
			for (Student student : students) {
				System.out.println(student);				
			}		
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值