Java学习之路-RMI学习

简介:
  Java远程方法调用,即Java RMI(Java Remote Method Invocation)是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口。它使客户机上运行的程序可以调用远程服务器上的对象。远程方法调用特性使Java编程人员能够在网络环境中分布操作。RMI全部的宗旨就是尽可能简化远程接口对象的使用。
   一、创建RMI程序的4个步骤
  1、定义一个远程接口的接口,该接口中的每一个方法必须声明它将产生一个RemoteException异常。
  2、定义一个实现该接口的类。
  3、创建一个服务,用于发布2中定义的类。
  4、创建一个客户程序进行RMI调用。
   二、程序的详细实现
  1.首先我们先创建一个实体类,这个类需要实现Serializable接口,用于信息的传输。
1 import java.io.Serializable;
3 public class Student implements Serializable {
5   private String name;
7   private int age;
9   public String getName() {
11       return name;
13   }
15   public void setName(String name) {
17       this.name = name;
19   }
21   public int getAge() {
23       return age;
25   }
27   public void setAge(int age) {
29       this.age = age;
31   }
33 }
  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类和显示写出无参的构造函数。
1 import java.rmi.RemoteException;
3 import java.rmi.server.UnicastRemoteObject;
5 import java.util.ArrayList;
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   }
45 }
 4.创建服务并启动服务
1 import java.rmi.Naming;
2 import java.rmi.registry.LocateRegistry;
4 public class SetService {
6     public static void main(String[] args) {
8         try {
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     }
26 }
  5. 创建一个客户程序进行RMI调用。
1 import java.rmi.Naming;
3 import java.util.List;
5 public class GetService {
9   public static void main(String[] args) {
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   }
33 }
  6.控制台显示结果
  =============控制台============
  姓名:张三,年龄:15
  姓名:李四,年龄:20
  ===============================
  在Spring中配置Rmi服务
  将Rmi和Spring结合起来用的话,比上面实现Rmi服务要方便的多。
  1.首先我们定义接口,此时定义的接口不需要继承其他接口,只是一个普通的接口
  1 package service;
  3 import java.util.List;
  5 public interface StudentService {
  7      List<Student> getList();
  9 }
  2.定义一个类,实现这个接口,这个类也只需实现步骤一定义的接口,不需要额外的操作
1 package service;
4 import java.util.ArrayList;
6 import java.util.List;
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  }
35 }
 3.接一下来在applicationContext.xml配置需要的信息
  a.首先定义服务bean
  <bean id="studentService" class="service.StudentServiceImpl"></bean>
  b.定义导出服务
  <bean class="org.springframework.remoting.rmi.RmiServiceExporter"
  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"
  class="org.springframework.remoting.rmi.RmiProxyFactoryBean"
  p:serviceUrl="rmi://127.0.0.1:5008/StudentService"
  p:serviceInterface="service.StudentService"
  />
  d.配置的东西就这么多,是不是比上面的现实要方便的多呀!现在我们来测试一下
1 package service;
2 import java.util.List;
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  }
14 }
  =============控制台============
  姓名:张三,年龄:15
  姓名:李四,年龄:20
  =============================
  上面的mian方法运行可能会报错,应该是spring的jar少了,自己注意添加。
最新内容请见作者的GitHub页:http://qaseven.github.io/
相关文章
|
15天前
|
消息中间件 前端开发 Java
java学习路径
【4月更文挑战第9天】java学习路径
17 1
|
1月前
|
安全 Java 程序员
学习Java类加载机制
在Java的世界里,每一个类或者接口,在经历编译器后,都会生成一个个.class文件。
18 0
|
1月前
|
Java 关系型数据库 MySQL
37、一篇文章学习 Java 中的日期相关类(Date 和 Calendar),非常常用
37、一篇文章学习 Java 中的日期相关类(Date 和 Calendar),非常常用
27 0
|
2月前
|
Java 数据库连接 开发工具
正式开始JAVA学习之旅
正式开始JAVA学习之旅
39 0
|
1月前
|
存储 安全 Java
24、使用 Java 官方教程学习:① 类变量和类方法详解;② 深入介绍 main() 方法
24、使用 Java 官方教程学习:① 类变量和类方法详解;② 深入介绍 main() 方法
38 1
|
15天前
|
设计模式 前端开发 安全
Java是一种广泛使用的编程语言,其学习路径可以大致分为以下几个阶段
【4月更文挑战第9天】Java是一种广泛使用的编程语言,其学习路径可以大致分为以下几个阶段
15 1
|
1月前
|
Java 索引
Java中String方法学习总结_kaic
Java中String方法学习总结_kaic
|
5天前
|
JavaScript Java 测试技术
基于Java的驾考自主学习预约平台的设计与实现(源码+lw+部署文档+讲解等)
基于Java的驾考自主学习预约平台的设计与实现(源码+lw+部署文档+讲解等)
16 0
|
6天前
|
JavaScript Java 测试技术
基于Java的精品课程在线学习系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的精品课程在线学习系统的设计与实现(源码+lw+部署文档+讲解等)
25 1
|
6天前
|
JavaScript Java 测试技术
基于Java的中文学习系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的中文学习系统的设计与实现(源码+lw+部署文档+讲解等)
22 0