Java rmi遠(yuǎn)程方法調(diào)用基本用法解析
本文主要介紹Java中的rmi的基本使用
1:項(xiàng)目架構(gòu)
api:主要是接口的定義,url地址,端口號(hào)
rmiconsumer:rmi服務(wù)的調(diào)用者
rmiserver:rmi服務(wù)的提供者
2:pom.xnl
api的pom.xml
<artifactId>api</artifactId><groupId>com.api</groupId><version>1.0</version> rmiconsumer和rmiserver的pom.xml<dependency><groupId>com.api</groupId><artifactId>api</artifactId><version>1.0</version></dependency>
該功能主要是將api的引入到服務(wù)端和客戶端
3:代碼
api的代碼
public interface RMIInterface extends Remote { String RMI_URL = 'rmi://127.0.0.1:9080/RMIServer'; int PORT = 9080; Object sayHello(String name) throws RemoteException;}
rmiserver的代碼
public class RMIInterfaceImpl extends UnicastRemoteObject implements RMIInterface { public RMIInterfaceImpl() throws RemoteException { } @Override public Object sayHello(String name) throws RemoteException { return '你好,你連接成功,姓名:'+name; }}
public class RMIServer { public static void main(String[] args) { try { RMIInterface rmi = new RMIInterfaceImpl(); //注冊(cè)通訊端口 LocateRegistry.createRegistry(RMIInterface.PORT); //注冊(cè)通訊路徑 Naming.bind(RMIInterface.RMI_URL,rmi); System.out.println('rmi服務(wù)端啟動(dòng)成功'); }catch (Exception e){ e.printStackTrace(); } }}
rmiconsumer
public class RMIConsumer { public static void main(String[] args) { //遠(yuǎn)程調(diào)用RMI RMIInterface rmiInterface =null; try{ rmiInterface =(RMIInterface) Naming.lookup(RMIInterface.RMI_URL); Object ret = rmiInterface.sayHello('張先生'); System.out.println('測(cè)試遠(yuǎn)程調(diào)用成功,返回結(jié)果:'+ret); }catch (Exception e){ e.printStackTrace(); } }}
4:總結(jié)
接口必須繼承 Remote
接口的實(shí)現(xiàn)類必須繼承 UnicastRemoteObject
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析2. 解決啟動(dòng)django,瀏覽器顯示“服務(wù)器拒絕訪問(wèn)”的問(wèn)題3. JavaMail 1.4 發(fā)布4. vue使用webSocket更新實(shí)時(shí)天氣的方法5. 淺談python出錯(cuò)時(shí)traceback的解讀6. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼7. Yii2.0引入CSS,JS文件方法8. Nginx+php配置文件及原理解析9. 關(guān)于HTML5的img標(biāo)簽10. 如何使用CSS3畫(huà)出一個(gè)叮當(dāng)貓
