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(); } } }
相关文章推荐
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL
- 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。
- 学习笔记:JAVA RMI远程方法调用简单实例
- 学习笔记:JAVA RMI远程方法调用简单实例
- java RMI(java远程方法调用实例解析)
- 学习笔记:JAVA RMI远程方法调用简单实例
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL
- 远程客户端 访问 ASM 实例
- 远程客户端 访问 ASM 实例
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管 分类: 错误积累 2012-03-09 09:22 6511人阅读 评论(4) 收藏
- 学习笔记:JAVA RMI远程方法调用简单实例
- 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。
- 远程客户端 访问 ASM 实例
- 远程客户端 访问 ASM 实例
- JAVA RMI远程方法调用简单实例
- JAVA RMI远程方法调用简单实例(转载)