您的位置:首页 > 编程语言 > Java开发

java rmi远程访问实例

2014-06-30 16:56 225 查看
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();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐