RMI socket facotry

Source: Internet
Author: User

Due to product security requirements, you need to bind the RMI service port of the old Code to 127.0.0.1. The solution is to set rmisocketfactory when exporting services through spring. When verifying the code, it is found that the self-implemented rmisocketfactory may cause system resource leakage when there are a large number of RMI requests, and thus the local thread cannot be created during the request.

The jconsole shows that a large number of RMI threads are generated during a large number of RMI requests and will not be released in time. This problem is not solved without customizing socketfactory.

After two days of PR, I finally found the reason for various attempts: in fact, the Oracle FAQ has been described as follows:

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

A.5 why does the Java RMI implementation create so far 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 implementhashCodeAndequalsMethods 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 implementshashCodeAndequalsMethods too.

If your socket factory has no instance state, a trivial Implementation ofhashCodeAndequalsMethods are the following:

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


After hashcode and equals are implemented, the problem is solved. I did not mention this in online examples, including spring documents. I am so sorry, no one has found this problem?


Share debugging code for reference by people with the same problem

The debugging code is compiled and debugged using spring2.5.5. You need to import it and the dependent package into classpath during use.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/59/wKioL1P5PlWTq_5CAAF3vxIjH0U271.jpg "Title =" qq20140425091606.png "alt =" wkiol1p5plwtq_5caaf3vxijh0u271.jpg "/>

This article is from the elminstre blog, please be sure to keep this source http://elminstre.blog.51cto.com/4706743/1543967

RMI socket facotry

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.