zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java学习之路-RMI学习

JAVA学习 之路 RMI
2023-09-11 14:20:31 时间
Java远程方法调用,即Java RMI(Java Remote Method Invocation)是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口。它使客户机上运行的程序可以调用远程服务器上的对象。远程方法调用特性使Java编程人员能够在网络环境中分布操作。RMI全部的宗旨就是尽可能简化远程接口对象的使用。 一、创建RMI程序的4个步骤 1、定义一个远程接口的接口,该接口中的每一个方法必须声明它将产生一个RemoteException异常。 2、定义一个实现该接口的类。 3、创建一个服务,用于发布2中定义的类。 4、创建一个客户程序进行RMI调用。 二、程序的详细实现 1.首先我们先创建一个实体类,这个类需要实现Serializable接口,用于信息的传输。
2.定义一个接口,这个接口需要继承Remote接口,这个接口中的方法必须声明RemoteException异常。 1 import java.rmi.Remote; 3 import java.rmi.RemoteException; 5 import java.util.List; 6 public interface StudentService extends Remote { 12   List Student getList() throws RemoteException; 14 } 3.创建一个类,并实现步骤2中的接口,但还需要继承UnicastRemoteObject类和显示写出无参的构造函数。
7 import java.util.List; 11 public class StudentServiceImpl extends UnicastRemoteObject implements 13       StudentService { 15   public StudentServiceImpl() throws RemoteException { 17   } 21   public List Student getList() throws RemoteException { 23       List Student list=new ArrayList Student 25       Student s1=new Student(); 27       s1.setName("张三"); 29       s1.setAge(15); 31       Student s2=new Student(); 33       s2.setName("李四"); 35       s2.setAge(20); 37       list.add(s1); 39       list.add(s2); 41       return list; 43   } 4.创建服务并启动服务
10             StudentService studentService=new StudentServiceImpl(); 12             LocateRegistry.createRegistry(5008);//定义端口号 14             Naming.rebind("rmi://127.0.0.1:5008/StudentService", studentService); 16             System.out.println("服务已启动"); 18         } catch (Exception e) { 20             e.printStackTrace(); 22         } 24     }
11       try { 13           StudentService studentService=(StudentService) Naming.lookup("rmi://127.0.0.1:5008/StudentService"); 15           List Student list = studentService.getList(); 17           for (Student s : list) { 19               System.out.println("姓名:"+s.getName()+",年龄:"+s.getAge()); 21           } 23       } catch (Exception e) { 25           e.printStackTrace(); 27       } 29   }
将Rmi和Spring结合起来用的话,比上面实现Rmi服务要方便的多。 1.首先我们定义接口,此时定义的接口不需要继承其他接口,只是一个普通的接口 1 package service; 3 import java.util.List; 5 public interface StudentService { 7      List Student getList(); 2.定义一个类,实现这个接口,这个类也只需实现步骤一定义的接口,不需要额外的操作
9 public class StudentServiceImpl implements StudentService { 11  public List Student getList() { 13   List Student list=new ArrayList Student 15   Student s1=new Student(); 17   s1.setName("张三"); 19   s1.setAge(15); 21   Student s2=new Student(); 23   s2.setName("李四"); 25   s2.setAge(20); 27   list.add(s1); 29   list.add(s2); 31   return list; 33  } 3.接一下来在applicationContext.xml配置需要的信息 a.首先定义服务bean bean id="studentService" /bean b.定义导出服务 bean p:service-ref="studentService" p:serviceInterface="service.StudentService" p:serviceName="StudentService" p:registryPort="5008" 也可以增加p:registryHost属性设置主机 c.在客户端的applicationContext.xml中定义得到服务的bean(这里的例子是把导出服务bean和客户端的bean放在一个applicationContext.xml中的) bean id="getStudentService" p:serviceUrl="rmi://127.0.0.1:5008/StudentService" p:serviceInterface="service.StudentService" d.配置的东西就这么多,是不是比上面的现实要方便的多呀!现在我们来测试一下
3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 public class Test { 6 public static void main(String[] args) { 7   ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); 8   StudentService studentService=(StudentService) ctx.getBean("getStudentService"); 9   List Student list = studentService.getList(); 10   for (Student s : list) { 11    System.out.println("姓名:"+s.getName()+",年龄:"+s.getAge()); 12   } 13  }
============================= 上面的mian方法运行可能会报错,应该是spring的jar少了,自己注意添加。 最新内容请见作者的GitHub页:http://qaseven.github.io/

学习java知道这五个网站就够了 好吧,这很难反驳!如今,编程比以往任何时候都更加蓬勃发展,而且这种趋势预计只会增长。根据美国 code.org 的数据,未来 10 年计算机科学领域将有 140 万个工作岗位。编程技能将很快成为任何行业更高级别专业人员的标准要求,就像现在的 PC 用户技能一样。 因此,即使您不是 学软件的,而是未来的设计师、销售经理或营销策略师,熟悉编程也是一对你工作中有所帮助。