Spring RMI Example

来源:http://www.cnblogs.com/rocky-fang/archive/2016/05/17/5502903.html
-Advertisement-
Play Games

一: 提供服務的遠程一端 1-1. applicationContext.xml 1-2. 介面 1-3. javabean 1-4. 實現類 1-5. ServerTest類 二: 本地調用一端 2-1. applicationContext-client 2-2. ClientTest類 ...


一: 提供服務的遠程一端

1-1. applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="  
    http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="userRmiServiceImpl" class="com.goodfan.rmi.service.impl.UserRmiServiceImpl" />
    <bean id="userRmi" class="org.springframework.remoting.rmi.RmiServiceExporter">
        <property name="service" ref="userRmiServiceImpl" />
        <property name="serviceName" value="userRmi" />
        <property name="serviceInterface" value="com.goodfan.rmi.service.UserRmiService" />
        <property name="registryPort" value="9999" />
    </bean>
</beans>  

1-2. 介面

package com.goodfan.rmi.service;

public interface UserRmiService {
     public String sayHello(User user);
}

1-3. javabean

package com.goodfan.rmi.service;

import java.io.Serializable;  

public class User implements Serializable{  
  
    private static final long serialVersionUID = 8550373205815267923L;  
    private String userName;  
  
    public String getUserName() {  
        return userName;  
    }  
  
    public void setUserName(String userName) {  
        this.userName = userName;  
    }  
  
} 

1-4. 實現類

package com.goodfan.rmi.service.impl;

import com.goodfan.rmi.service.User;
import com.goodfan.rmi.service.UserRmiService;

public class UserRmiServiceImpl implements UserRmiService {

    @Override
    public String sayHello(User user) {
         return "Hello, " + user.getUserName();
    }

}

1-5. ServerTest類

package com.goodfan.rmi.service;

import org.springframework.context.support.ClassPathXmlApplicationContext;  

public class ServerTest {  
  
    public static void main(String[] args) {  
        System.setProperty("java.rmi.hostname", "10.7.3.12");   
        new ClassPathXmlApplicationContext("applicationContext.xml");  
        System.out.println("server start......");  
    }  
}

 

二: 本地調用一端

2-1. applicationContext-client

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="  
http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  
    <bean id="rmiProxy" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  
        <property name="serviceUrl" value="rmi://10.7.3.12:9999/userRmi"/>  
        <property name="ServiceInterface" value="com.goodfan.rmi.service.UserRmiService" />  
    </bean>  
</beans> c

2-2. ClientTest類

package com.goodfan.rmi.service;

import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
public class ClientTest {  
  
    public static void main(String[] args) {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");  
        UserRmiService ums = (UserRmiService) ctx.getBean("rmiProxy");  
        User user = new User();  
        user.setUserName("RMI");  
        System.out.println(ums.sayHello(user));  
    }  
} 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 代碼結構: 1、ThymeleafController 1 package com.xxx.firstboot.web; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.Mod ...
  • Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 暴力 KMP 以後寫。。 ...
  • 對於三目運算符(ternary operator),python可以用conditional expressions來替代 如對於x<5?1:0可以用下麵的方式來實現 1if x<5else 0 註: conditional expressions是在python 2.5之前引入的,所以以上代碼僅適 ...
  • 思維導圖 點擊下圖,可以看具體內容! 介紹 正則表達式,大家在開發中應該是經常用到,現在很多開發語言都有正則表達式的應用,比如javascript,java,.net,php等等,我今天就把我對正則表達式的理解跟大家嘮嘮,不當之處,請多多指教! 需要知道的術語——下麵的術語你知道多少? 需要知道的術 ...
  • 程式計數器 程式計數器是一塊較小的記憶體,它是線程私有的,可以看作是當前線程執行位元組碼的計數器。在虛擬機的概念模型中,位元組碼解釋器就是通過這個計數器來找到下一個將要執行的指令。java中分支語句,迴圈,異常處理以及線程恢復都是通過程式計數器來實現的。 由於JVM在執行線程的時候是通過CPU輪流執行各個 ...
  • 0. XXXX "is not a class or namespace"錯誤 最詭異的錯誤,提示意思很明顯,說你寫的名字既不是一個類也不是一個命名空間,雖然我C++水平不是很高,但再愚笨也不至於連類的格式class MyClass{....};也寫不明白吧,報此錯誤原因顯然跟它沒關係,那又是怎麼回 ...
  • 對於每個Java程式員來說,HelloWorld是一個再熟悉不過的程式。它很簡單,但是這段簡單的代碼能指引我們去深入理解一些複雜的概念。這篇文章,我將探索我們能從這段簡單的代碼中學到什麼。如果你對HelloWorld有獨到的理解,請留下你的評論。 HelloWorld.java 為什麼所有東西都是從 ...
  • 前言 Verilog是一種硬體描述語言(HDL),該語言在Windows上有集成開發環境可以使用,如ModelSim,但ModelSim的編輯器不太好用因此筆者萌生了用Sublime Text3來編寫Verilog的想法。下麵我們將圍繞著Sublime Text3搭建起一個簡易的IDE, 我將儘量把 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...