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 用户技能一样。 因此,即使您不是 学软件的,而是未来的设计师、销售经理或营销策略师,熟悉编程也是一对你工作中有所帮助。
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 用户技能一样。 因此,即使您不是 学软件的,而是未来的设计师、销售经理或营销策略师,熟悉编程也是一对你工作中有所帮助。
相关文章
- java虚拟机学习-Java常量池理解与总结(13-2)
- Future 异步回调 大起底之 Java Future 与 Guava Future
- java理论基础学习三
- java安装1.8和1.7,报错:Error: Registry key 'SoftwareJavaSoftJava Runtime Environment'CurrentVers
- java基础-java语言中的关键字总结
- 【JAVA】关于【数组】的知识合集!(有新的学习,再追加更新)
- Java中的回调函数学习-深入浅出
- java在Linux执行命令Java在Windows执行命令
- Kotlin和Java相比有什么优点?Koltin全家桶学习资料领取
- 第44节:Java当中的JVM
- 基于Java+MySQL实现燃气管进销存管理系统【100010498】
- Java反射学习总结四(动态代理使用实例和内部原理解析)
- 大数据学习——java操作hdfs环境搭建以及环境测试
- JAVA_Android APK反编译就这么简单 详解(附图)
- Java面向对象(三)学习笔记
- 2023年java初级面试题(5道)
- Java魔法堂:URI、URL(含URL Protocol Handler)和URN
- 【转】Java学习---解析Java Servlet工作过程
- Java学习---JAVA的类设计
- java学习之路---线程(重点)
- 【Java】Java学习笔记-多线程
- 用java生成永远唯一的id
- JAVA_SE进阶day03面向对象高级 XMind及总结
- JAVA学习:成员内部类基本概念及用法
- java学习路线-Java技术人员之路从0基础到高级
- JAVA并发编程学习笔记之CAS操作
- Eclipse迅速执行:Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- java中的switch结构
- Java学习路线:day22 枚举类与注解
- Java学习路线:day14 面向对象(下)
- 连环清洁工之特殊任务--java资源如何关闭?
- Java 编程的动态性,第 8 部分: 用代码生成取代反射--转载
- Java小白入门200例39之Java字符串拼接(连接)
- 多种方式解决Java控制台报错 java.util.LinkedHashMap cannot be cast to.....
- Java StringBuffer和StringBuilder