qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          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 on 2014-11-21 10:56 順其自然EVO 閱讀(255) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 金门县| 南丹县| 四会市| 定远县| 教育| 永川市| 新巴尔虎左旗| 栖霞市| 佛坪县| 南汇区| 衡阳县| 梁河县| 耿马| 平定县| 图木舒克市| 榆社县| 南皮县| 洮南市| 望奎县| 新营市| 白水县| 习水县| 周宁县| 玛多县| 大荔县| 钟祥市| 四子王旗| 呼图壁县| 河源市| 宝坻区| 安溪县| 榆林市| 深泽县| 通江县| 西城区| 高碑店市| 崇仁县| 齐河县| 东源县| 策勒县| 高雄市|