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少了,自己注意添加。

第一次写博客,有不对的地方请多多指出。

 

posted @ 2014-10-31 19:58  泛泛而談  阅读(7161)  评论(1编辑  收藏  举报