郑州团购网站建设,免费做婚礼邀请函的网站,电话手表网站,网站cms分站系统参考其它rmi#xff08;remote method invocation#xff09;的代码后#xff0c;加入了自己思考。整个工程基于maven构建#xff0c;我觉得maven的模块化可比较直观地演示rmi
目录
项目结构图
模块解读
pom文件
rmi-impl
rmi-common-interface
rmi-server
rmi-cli…参考其它rmiremote method invocation的代码后加入了自己思考。整个工程基于maven构建我觉得maven的模块化可比较直观地演示rmi
目录
项目结构图
模块解读
pom文件
rmi-impl
rmi-common-interface
rmi-server
rmi-client 各模块源码
rmi-common-interface
RemoteInterface.java
rmi-server
RMIServer.java
RMIServerProperties.java
rmi-client RMIClient.java 启动说明重要
运行截图
启动服务是的控制台信息
编辑客户端连接服务端并发送消息
服务端控制台再次打印信息 项目结构图 模块解读
整个工程rmi-impl分为三个子模块
1、rmi-commom-interfacermi-server与rmi-client都依赖的模块用来定义供服务端实现、供客户端调用的公共远程调用接口
2、rmi-server实现远程调用接口并注册服务
3、rmi-client拉去服务并调用远程接口
pom文件
rmi-impl ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdpsn.kiko/groupIdartifactIdrmi-impl/artifactIdpackagingpom/packagingversion1.0-SNAPSHOT/versionmodulesmodulermi-common-interface/modulemodulermi-server/modulemodulermi-client/module/modulespropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/properties/project rmi-common-interface ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdrmi-impl/artifactIdgroupIdpsn.kiko/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdrmi-common-interface/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/properties/project rmi-server ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdrmi-impl/artifactIdgroupIdpsn.kiko/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdrmi-server/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/properties--依赖common模块--dependenciesdependencygroupIdpsn.kiko/groupIdartifactIdrmi-common-interface/artifactIdversion1.0-SNAPSHOT/version/dependency/dependencies/project rmi-client ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdrmi-impl/artifactIdgroupIdpsn.kiko/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdrmi-client/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/properties--依赖common模块--dependenciesdependencygroupIdpsn.kiko/groupIdartifactIdrmi-common-interface/artifactIdversion1.0-SNAPSHOT/version/dependency/dependencies
/project 各模块源码
rmi-common-interface
RemoteInterface.java package psn.kiko.rmi.common;import java.rmi.Remote;
import java.rmi.RemoteException;/*** 声明远程服务接口此接口里声明的方法由服务端实现供客户端调用。* 鉴于此将所有远程接口都放到common模块并在需要此接口的模块引入br* on 2023/2/15 18:03*/
public interface RemoteInterface extends Remote {/*** 此方法将会在服务端打印message:由rmi-server模块实现由rmi-client远程调用* param message* throws RemoteException*/void printMsg(String message) throws RemoteException;
}rmi-server
RMIServer.java package psn.kiko.rmi.server;import psn.kiko.rmi.common.RemoteInterface;import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;/*** rmi服务端br* on 2023/2/15 18:08*/
public class RMIServer extends UnicastRemoteObject implements RemoteInterface {// 运行main注册服务public static void main(String[] args) {try {rmiServerRegistration();} catch (RemoteException | MalformedURLException | AlreadyBoundException e) {e.printStackTrace();}}private RMIServerProperties serverProperties;public void setServerProperties(RMIServerProperties serverProperties) {this.serverProperties serverProperties;}public RMIServerProperties getServerProperties() {return serverProperties;}protected RMIServer() throws RemoteException {}Overridepublic void printMsg(String message) throws RemoteException {System.out.println(this.serverProperties.getSeverName()获得客户端消息 message);}/*** rmi服务注册添加到虚拟机注册表* throws RemoteException* throws AlreadyBoundException* throws MalformedURLException*/private static void rmiServerRegistration() throws RemoteException, AlreadyBoundException, MalformedURLException {RMIServer rmiServer new RMIServer();rmiServer.setServerProperties(new RMIServerProperties(8888, rmi-server));LocateRegistry.createRegistry(rmiServer.getServerProperties().getServerPort());Naming.bind(rmiServer.getServerProperties().getServerUrl(), rmiServer);System.out.println(rmiServer.getServerProperties().getSeverName()已启动);}
}RMIServerProperties.java package psn.kiko.rmi.server;/*** 服务端参数br* on 2023/2/15 18:41*/
public class RMIServerProperties {private String serverUrl; //服务端通信地址private int serverPort;//服务端端口private String severName;//服务名称public RMIServerProperties(int serverPort,String serverName) {this.severNameserverName;this.serverPort serverPort;this.serverUrl rmi://localhost:serverPort/severName;}public String getServerUrl() {return serverUrl;}public int getServerPort() {return serverPort;}public String getSeverName() {return severName;}
}rmi-client RMIClient.java package psn.kiko.client;import psn.kiko.rmi.common.RemoteInterface;import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Scanner;/*** rmi客户端br* on 2023/2/15 18:19*/
public class RMIClient {// 运行main获取远程服务并向其传输messagepublic static void main(String[] args) {try {RemoteInterface rmiServer (RemoteInterface)Naming.lookup(rmi://localhost:8888/rmi-server);Scanner scanner new Scanner(System.in);System.out.println(请输入需要发送给服务端的信息);String message scanner.next();rmiServer.printMsg(message);System.out.println(客户端发送消息完毕);} catch (NotBoundException | MalformedURLException | RemoteException e) {e.printStackTrace();}}
}启动说明重要
先运行RMIServer.main启动后将等待客户端连接,再运行RMIClient.main(可多次单独运行进行多次连接通信)
运行截图
启动服务是的控制台信息
截图中的红色正方形也表示服务程序启动后就一直运行着
客户端连接服务端并发送消息
灰色正方形表示客户端与服务端通信完毕就断开连接 服务端控制台再次打印信息
红色正方形表示服务端与客户端通信完毕后仍在运行 以上就是全部关于RMI的详细基础用法了