- 浏览: 223769 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
itlang01:
推荐一款动态添加日期的插件https://github.com ...
利用mysql触发器记录数据的插入和更新时间 -
daydayupzzc:
好,不错,清楚
Spring MVC的拦截器 -
zi_wu_xian:
不管用POI还是JXL都是第三的操作excel的组件,都有破坏 ...
java操作excel——jxl和poi比较 -
小头猎豹:
支持无私的分享,很有用,十分感谢!!
Maven之使用SVN版本号 -
superich2008:
写的很详细,正在找这个资料,多谢博主分享。
Maven之使用SVN版本号
Java远程方法调用(Java Remote Method Invocation, RMI)支持一个JVM中的对象调用另一个JVM对象的方法,而这两个JVM可以分布在不同的主机上,为Java的分布式应用提供了支持。
1.首先,定义一个远程调用接口,该接口必须实现java.rmi.Remote接口,接口的方法声明要抛出java.rmi.RemoteException
import java.rmi.Remote; import java.rmi.RemoteException; public interface HelloRemote extends Remote { String sayHello(UserRemote user)throws RemoteException; }
import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 3553604278956856032L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
2.实现接口并继承UnicastRemoteObject
import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class HelloRemoteImpl extends UnicastRemoteObject implements HelloRemote { private static final long serialVersionUID = -6209023821784985143L; protected HelloRemoteImpl() throws RemoteException { super(); } @Override public String sayHello(User user) throws RemoteException { return "Hello, " + user.getName(); } }
3.注册该服务并启动
import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; public class RemoteServer { public static void main(String[] args) { try { // System.setProperty("java.rmi.server.hostname", "10.0.0.249"); LocateRegistry.createRegistry(5590); HelloRemoteImpl server = new HelloRemoteImpl(); Naming.rebind("//10.0.0.249:5590/HelloRemote", server); System.out.println("server start……"); } catch (RemoteException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } } }
5.调用RMI服务
package com.lanhuidong.client; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; public class RMIClient { public static void main(String[] args) { String url = "//10.0.0.249:5590/HelloRemote"; try { HelloRemote helloRemote = (HelloRemote) Naming.lookup(url); User user = new User(); user.setName("RMI"); System.out.println(helloRemote.sayHello(user)); } catch (MalformedURLException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } catch (NotBoundException e) { e.printStackTrace(); } } }
在实际项目中,可能项目使用了Spring等框架,下面是spring的rmi服务
1定义接口(不用继承任何其它接口)
public interface UserRmiService { public String sayHello(User user); }
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; } }
2.实现该接口
public class UserRmiServiceImpl implements UserRmiService { @Override public String sayHello(User user) { return "Hello, " + user.getUserName(); } }
3.在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.darcy.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.darcy.rmi.service.UserRmiService" /> <property name="registryPort" value="9999" /> </bean> </beans>
4.启动服务
import org.springframework.context.support.ClassPathXmlApplicationContext; public class ServerTest { public static void main(String[] args) { System.setProperty("java.rmi.hostname", "10.0.0.249"); new ClassPathXmlApplicationContext("applicationContext-Server.xml"); System.out.println("server start......"); } }
5.调用服务
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)); } }
applicationContext-client.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="rmiProxy" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://127.0.0.1:9999/userRmi"/> <property name="ServiceInterface" value="com.darcy.rmi.service.UserRmiService" /> </bean> </beans>
发表评论
-
Java日志系统-logback配置
2013-10-30 19:52 4742logback配置文件:logback.xml < ... -
关于ThreadPoolExecutor的理解
2013-09-03 16:07 853做Java也有4年了,原来对于ThreadPoolExec ... -
Garbage-First Collector
2013-01-30 21:51 1180从JDK7 update4开始完全支持G1垃圾收集器,这是一 ... -
JDK自带工具
2013-01-30 10:21 01.Java Virtual Machine Process ... -
JAVA定时器
2013-01-20 23:14 5465最近有同事提出在项目中使用JDK自带的定时器,之前的任务调度 ... -
开发环境配置
2012-09-11 11:50 53将自己的开发环境安装及配置过程写成文件,供自己或他人参考。 -
异常没有打印栈信息
2012-07-04 21:36 1571最近上的项目bug很多,服务器经常爆java.lang.Nul ... -
java操作excel——jxl和poi比较
2012-06-06 20:48 20501最近需要需要项目开发需要从excel导入数据到数据库,于是就开 ... -
SecurityManager使用
2011-06-15 14:49 2021因为只允许指定IP的服务器访问RMI,因此研究了Securit ... -
关于String连接(+运算符)
2011-03-08 20:54 26131.代码 package lan.jdk.study; ... -
HotSpot VM 内存管理
2011-02-22 21:18 11661.HotSpot VM的内存有三个g ... -
The Java HotSpot VM's GC
2011-02-18 15:04 1066GC的主要特点: 一、精 ... -
Java HotSpot
2011-02-17 17:16 0JDK中包含两种风格的虚拟机:Client VM and Se ... -
maven在eclipse中报错问题解决
2010-07-17 10:03 1079Eclipse is running in a J ... -
Java中的for和for-each效率问题
2010-06-22 11:59 1322今天看到Effective Java中说foreach比for ... -
Integer之==
2010-05-12 20:41 1005package lan; public class T ...
相关推荐
本文档主要讲述的是Java 远程方法调用RMI参数详解;根据RMI参数意义,可以归结为以下几点,我们可以根据这几点通过优化GC, 网络等待,流传输协议(http/rmi special socket)等方面来优化RMI。
三种方式实现java远程调用(rmi) 方式一:原始方式 方式二:spring 方式三:jndi 解压,放到myeclipse上可用
rmi 远程方法调用 客户端
java远程方法调用,可以实现远程调用,仅作参考
Eclipse 插件RMI Plug-in for Eclipse 2.0 博文链接:https://robinjie.iteye.com/blog/34606
rmi java 远程方法调用
JAVA RMI远程调用方法代码 RMI 远程 java
Java RMI远程方法调用详解-例子代码,例子详解在:http://blog.csdn.net/guyuealian/article/details/51992182
JAVA远程调用RMI与应用
java实现远程调用的完整实例,可在linux环境下启动远程服务,window下执行操作。代码有详细说明。
讲述利用java rmi进行分布式应用开发的相关知识
Java RMI 远程方法调用的一个小例子
java rmi 实现远程调用服务的简单的例子。
Java RMI远程方法调用的小Demo。详细:http://blog.csdn.net/u014375869/article/details/39344963
Java远程方法调用是一种分布式系统技术。它允许一个java虚拟机调用运行在网络中另一个地方的JVM上的对象方法. 这种技术对开发大型系统尤为重要,因为它使得在多台机器上分布资源和处理负载的想法成为可能。
java rmi 远程方法调用 服务端
最近在学习代理模式,用到了java rmi远程调用,包含服务端和客户端,之前一直没有接触过,学习了java rmi远程调用,一方面可以了解代理模式,一方面熟悉java低层的远程
Java RMI远程方法调用,简单明了,欢迎大家下载
java jdk1.8; eclipse 开发环境;实现A机器的程序,可以管理(增加、删除、改等)B机器上的某个文件夹或者目录;掌握远程过程调用原理,基于...客户端利用RMI实现远程调用服务。同时,在在两台机器之间验证结果正确。
NULL 博文链接:https://xiongjiajia.iteye.com/blog/1292142