qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請(qǐng)?jiān)L問(wèn) http://qaseven.github.io/

          Java學(xué)習(xí)之路-RMI學(xué)習(xí)

           Java遠(yuǎn)程方法調(diào)用,即Java RMI(Java Remote Method Invocation)是Java編程語(yǔ)言里,一種用于實(shí)現(xiàn)遠(yuǎn)程過(guò)程調(diào)用的應(yīng)用程序編程接口。它使客戶機(jī)上運(yùn)行的程序可以調(diào)用遠(yuǎn)程服務(wù)器上的對(duì)象。遠(yuǎn)程方法調(diào)用特性使Java編程人員能夠在網(wǎng)絡(luò)環(huán)境中分布操作。RMI全部的宗旨就是盡可能簡(jiǎn)化遠(yuǎn)程接口對(duì)象的使用。
            一、創(chuàng)建RMI程序的4個(gè)步驟
            1、定義一個(gè)遠(yuǎn)程接口的接口,該接口中的每一個(gè)方法必須聲明它將產(chǎn)生一個(gè)RemoteException異常。
            2、定義一個(gè)實(shí)現(xiàn)該接口的類。
            3、創(chuàng)建一個(gè)服務(wù),用于發(fā)布2中定義的類。
            4、創(chuàng)建一個(gè)客戶程序進(jìn)行RMI調(diào)用。
            二、程序的詳細(xì)實(shí)現(xiàn)
            1.首先我們先創(chuàng)建一個(gè)實(shí)體類,這個(gè)類需要實(shí)現(xiàn)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.定義一個(gè)接口,這個(gè)接口需要繼承Remote接口,這個(gè)接口中的方法必須聲明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.創(chuàng)建一個(gè)類,并實(shí)現(xiàn)步驟2中的接口,但還需要繼承UnicastRemoteObject類和顯示寫(xiě)出無(wú)參的構(gòu)造函數(shù)。
          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.創(chuàng)建服務(wù)并啟動(dòng)服務(wù)
          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);//定義端口號(hào)
          14             Naming.rebind("rmi://127.0.0.1:5008/StudentService", studentService);
          16             System.out.println("服務(wù)已啟動(dòng)");
          18         } catch (Exception e) {
          20             e.printStackTrace();
          22         }
          24     }
          26 }
            5. 創(chuàng)建一個(gè)客戶程序進(jìn)行RMI調(diào)用。
          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.控制臺(tái)顯示結(jié)果
            =============控制臺(tái)============
            姓名:張三,年齡:15
            姓名:李四,年齡:20
            ===============================
            在Spring中配置Rmi服務(wù)
            將Rmi和Spring結(jié)合起來(lái)用的話,比上面實(shí)現(xiàn)Rmi服務(wù)要方便的多。
            1.首先我們定義接口,此時(shí)定義的接口不需要繼承其他接口,只是一個(gè)普通的接口
            1 package service;
            3 import java.util.List;
            5 public interface StudentService {
            7      List<Student> getList();
            9 }
            2.定義一個(gè)類,實(shí)現(xiàn)這個(gè)接口,這個(gè)類也只需實(shí)現(xiàn)步驟一定義的接口,不需要額外的操作
          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.接一下來(lái)在applicationContext.xml配置需要的信息
            a.首先定義服務(wù)bean
            <bean id="studentService" class="service.StudentServiceImpl"></bean>
            b.定義導(dǎo)出服務(wù)
            <bean class="org.springframework.remoting.rmi.RmiServiceExporter"
            p:service-ref="studentService"
            p:serviceInterface="service.StudentService"
            p:serviceName="StudentService"
            p:registryPort="5008"
            />
            也可以增加p:registryHost屬性設(shè)置主機(jī)
            c.在客戶端的applicationContext.xml中定義得到服務(wù)的bean(這里的例子是把導(dǎo)出服務(wù)bean和客戶端的bean放在一個(gè)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.配置的東西就這么多,是不是比上面的現(xiàn)實(shí)要方便的多呀!現(xiàn)在我們來(lái)測(cè)試一下
          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 }
            =============控制臺(tái)============
            姓名:張三,年齡:15
            姓名:李四,年齡:20
            =============================
            上面的mian方法運(yùn)行可能會(huì)報(bào)錯(cuò),應(yīng)該是spring的jar少了,自己注意添加。

          posted on 2014-11-21 10:56 順其自然EVO 閱讀(258) 評(píng)論(0)  編輯  收藏 所屬分類: 測(cè)試學(xué)習(xí)專欄

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

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 阜宁县| 通城县| 泰来县| 贵南县| 涿州市| 静海县| 拉孜县| 法库县| 犍为县| 巴楚县| 南乐县| 那曲县| 河东区| 宝山区| 浦城县| 南城县| 亚东县| 万源市| 宣恩县| 永城市| 来凤县| 东乡县| 建昌县| 响水县| 宁蒗| 察隅县| 茌平县| 闸北区| 罗平县| 巩义市| 隆德县| 沅陵县| 常州市| 余庆县| 禄劝| 平谷区| 建湖县| 宁化县| 凤翔县| 固阳县| 鹰潭市|