<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(258) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

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

    導航

    統計

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 一级做a爰片性色毛片免费网站 | 国产精品永久免费10000| 亚洲国产成人久久综合一区77 | 亚洲AV无码乱码麻豆精品国产| 好紧我太爽了视频免费国产| 亚洲精品视频在线观看免费| 黄网址在线永久免费观看 | 麻豆亚洲AV永久无码精品久久 | 伊人久久大香线蕉免费视频| 亚洲色无码一区二区三区| 中文字幕乱码系列免费| 好爽…又高潮了免费毛片| 亚洲经典千人经典日产| 国产男女猛烈无遮挡免费网站| 国产精品亚洲综合一区在线观看 | 一级特黄色毛片免费看| AV在线亚洲男人的天堂| 亚洲色精品三区二区一区| 国产免费一区二区三区不卡| 亚洲AV综合色一区二区三区| 爱情岛论坛亚洲品质自拍视频网站| 四虎永久在线精品免费影视 | 亚洲AⅤ男人的天堂在线观看| 最近中文字幕无免费| 亚洲成a人片在线看| 视频免费在线观看| 97se亚洲综合在线| 美女露100%胸无遮挡免费观看| 亚洲午夜精品久久久久久浪潮 | 岛国岛国免费V片在线观看| 亚洲人成亚洲精品| EEUSS影院WWW在线观看免费| 国产99视频精品免费视频7| 亚洲免费在线观看| 亚洲精品国产福利片| 91精品免费不卡在线观看| 亚洲s码欧洲m码吹潮| 国产AV无码专区亚洲AVJULIA| 欧美在线看片A免费观看| 九九久久精品国产免费看小说 | 日本一卡精品视频免费 |