坑爹的RMI Socket Facotry

因为产品安全的需求,需要将之前老代码的RMI服务端口绑定到127.0.0.1。解决办法是通过Spring导出服务时设置自己的RMISocketFactory。在验证代码时发现自己实现的RMISocketFactory在有大量RMI请求时,会将造成系统的资源泄漏,导致请求时无法创建本地线程。

通过jconsole一看,原来在大量RMI请求时,生成了大量的RMI线程并且不会及时释放掉,而不自定义socketfactory则没有此问题。

公关了2天,各种尝试,今天终于找到的了原因:其实Oracle的FAQ早有说明:

http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/faq.html

A.5 Why does the Java RMI implementation create so many sockets when my application uses custom socket factories; or why are stubs (using a custom socket factory) that refer to the same remote object not equal; or why does the Java RMI implementation not reuse server-side ports when I use a custom server socket factory?

The Java RMI implementation attempts to reuse open sockets where possible for remote invocations. When a remote method is invoked on a stub that uses a custom socket factory, the Java RMI implementation will reuse an open connection (if any) as long as that socket was created by an equivalent socket factory. Since client socket factories are serialized to clients, a single client may have several distinct copies of the same logical socket factory. To ensure that the Java RMI implementation will reuse sockets created by custom socket factories, make sure your custom client socket factory classes implement the hashCode and equalsmethods appropriately. If the client socket factory does not implement these methods correctly, another ramification is that stubs (using the client socket factory) that refer to the same remote object will not be equal.

The Java RMI implementation attempts to reuse server-side ports as well. It will only do so if there is an existing server socket for the port created by an equivalent socket factory. Make sure the server socket factory class implements the hashCode and equals methods too.

If your socket factory has no instance state, a trivial implementation of the hashCode and equals methods are the following:

    public int hashCode() { return 57; }
    public boolean equals(Object o) { return this.getClass() == o.getClass(); }

实现了hashCode和equals后,问题解决,网上的例子,包括spring的文档都没说这个,坑爹啊,大家都没发现这个问题?

共享下调试代码,供后面有相同问题的人参考

调试代码使用了spring2.5.5编译调试,使用时需要将其和相应依赖的包导入classpath

时间: 2024-07-28 23:15:16

坑爹的RMI Socket Facotry的相关文章

socket、webService、RMI ?

网络七层协议为:物理层.数据链路层.网络层.传输层.会话层.表示层.应用层 webService > RMI > socket RMI比socket更高一点 socket 只是 java在网络层定义的类,用来实现网络层.上面的各层需要我们自己在程序里实现. 例如端口可以自己定义 .数据包的定义. 数据包的加密解密等 而webService java实现了应用层的工具,他基于的服务为http协议,通过服务器才可以发布出去. 这样内部的端口的定义.数据包的定义和数据包的加密解密都做好了,所以我们就

Java Secure Socket Extension (JSSE) Reference Guide

Skip to Content Oracle Technology Network Software Downloads Documentation Search Java Secure Socket Extension (JSSE) Reference Guide This guide covers the following topics: Skip Navigation Links Introduction Features and Benefits JSSE Standard API S

Webservice与CXF框架快速入门

1. Webservice Webservice是一套远程调用技术规范 远程调用RPC, 实现了系统与系统进程间的远程通信.java领域有很多可实现远程通讯的技术,如:RMI(Socket + 序列化).Binary-RPC(Http+二进制, 代表Hessian).XML-RPC(Http+XML, 代表Burlap, WebService用的SOAP).JMS(使用消息机制).Mina(使用NIO)等, 底层都是基于http/socket和网络IO来实现的.从效率上来讲, RMI > Hes

RPC是什么?科普一下

RPC概念及分类 RPC全称为Remote Procedure Call,翻译过来为“远程过程调用”.目前,主流的平台中都支持各种远程调用技术,以满足分布式系统架构中不同的系统之间的远程通信和相互调用.远程调用的应用场景极其广泛,实现的方式也各式各样. 从通信协议的层面,大致可以分为: 基于HTTP协议的(例如基于文本的SOAP(XML).Rest(JSON),基于二进制Hessian(Binary)) 基于TCP协议的(通常会借助Mina.Netty等高性能网络框架) 从不同的开发语言和平台层

sooket数据成传输压缩

样压缩不以文件为基础的数据 Q: 回答了两个使用Java进行数据压缩的问题. 第一个问题是: 我怎样才能压缩那些不在文件中的数据. 第二个问题是: 我以极大的热情阅读了Todd Sundsted的"压缩你的数据,从而提高你的网络应用程序的性能",但是读完后我却有点失望.当我读到文章标题时我很高兴.我想我总算找到了解决问题的办法了. 在我们的公司,我们试图提高一个组织数据的RMI应用程序的性能.服务器端进行了绝大部分的处理和优化.我们花了一年半的时间去提高性能,但是现在看来瓶颈在于数据的

阿里首席架构师科普RPC框架是什么

RPC概念及分类 RPC全称为Remote Procedure Call,翻译过来为"远程过程调用".目前,主流的平台中都支持各种远程调用技术,以满足分布式系统架构中不同的系统之间的远程通信和相互调用.远程调用的应用场景极其广泛,实现的方式也各式各样. 从通信协议的层面,大致可以分为: 基于HTTP协议的(例如基于文本的SOAP(XML).Rest(JSON),基于二进制Hessian(Binary)) 基于TCP协议的(通常会借助Mina.Netty等高性能网络框架) 从不同的开发语

RPC和Socket,RMI和RPC之间的关系

远程通信机制RPC与RMI的关系 http://blog.csdn.net/zolalad/article/details/25161133       1.RPC RPC(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC不依赖于具体的网络传输协议,tcp.udp等都可以.由于存在各式各样的变换和细节差异,相应的rpc也派生出了各式远程过程通信协议.RPC是跨语言的通信标准,SUN和

RMI RPC socket

  1.RPC RPC(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC不依赖于具体的网络传输协议,tcp.udp等都可以.由于存在各式各样的变换和细节差异,相应的rpc也派生出了各式远程过程通信协议.RPC是跨语言的通信标准,SUN和微软都有其实现,比如RMI可以被看作SUN对RPC的Java版本( 实现),而微软的DCOM就是建立在ORPC协议之上.一言以蔽之,RPC是协议,而无

JAVA与网络开发(TCP:Socket、ServerSocket;UDP:DatagramSocket、DatagramPacket;多线程的C/S通讯、RMI开发概述)

通过TCP建立可靠通讯信道 1)为了对应TCP协议里的客户端和服务器端,Socket包提供了Socket类和ServerSocket类. 2)Socket类构造函数及相关方法 Public Socket(); public Socket(InetAddress address,int port);//本机IP和端口 public Socket(Striing host,int port);//本机IP和端口 public void connect(SocketAddress endpoint);