RMI summary and several possible anomalies and solutions

Source: Internet
Author: User

A summary of RMI technology

I. Overview

Mainly includes 5 documents: Irmi (interface), Rmiimp, Rmiserver, Rmiclient, rmiimp_stub

Execute at the and CN Unified Directory level: Rmic CN.CNIC.OSG.RMIIMP

II. Security Mechanisms

Http://unmi.cc/think-in-java-rmi-exception

There is a sample RMI code perfecttime in the think of Java, but there is no explanation for some of the execution details, and it can cause a novice to run it out of the ordinary. The following lists the possible exceptions and explains the solution, such as the reader in the execution of other unlisted exceptions, can leave a message, I will do my best.

First, change the code perfecttime and Displayperfecttime//colossus:2005/perfecttime to//localhost:2005/perfecttime, because Colossus is the machine name, So instead of localhost point to the local machine, or can not find the host Colossus.

The Perfecttime_stub.class has been generated with command rmic, and command Rmiregistry 2005 has been executed

1. Execute Java perfecttime exception java.security.AccessControlException:access denied (java.net.SocketPermission 127.0.0.1:2005 connect,resolve)

Cannot parse and connect to port 2005 on 127.0.0.1 because Security manager

(1) The code can be System.setsecuritymanager (new Rmisecuritymanager ()), remove, do not set the security manager
(2) Modify the JRE's security policy file, which requires you to determine which JRE is used for execution, such as using JDK as c:/java/jdk1.5.0_06 in Eclipse, and the corresponding security policy file is C:/java/jdk1.5.0_06/jre/lib /security/java.policy, if the Java program in the applet should be in the JRE directory, such as file C:/java/jre1.5.0_06/lib/security/java.policy. Modify the security policy file and add permission Java.net.SocketPermission "Localhost:2005″, Connect,resolve" to the grant {} and curly braces;
(3) To establish their own policy files, such as C:/mypolicy.policy, the content is:

Grant {
Permission Java.net.SocketPermission "Localhost:2005″", "connect,resolve";
}

The security policy file was specified with the command Java-djava.security.policy=c:/mypolicy.policy Perfecttime when the perfecttime was executed, and I tested it without adding-djava**** to the server side =***mypolicy.policy. This can be added on the client.

(4) Change System.setsecuritymanager (New Rmisecuritymanager ()) To anonymous class implementation, overwriting two methods

System.setsecuritymanager (New Rmisecuritymanager () {
public void Checkconnect (String host, int port) {}
public void Checkconnect (String host, int port, Object context) {}
});

The simplest solution, of course, is the first.

2. The same is the implementation of the exception of Perfecttime
Java.rmi.ServerException:RemoteException occurred in server thread; Nested exception is:
Java.rmi.UnmarshalException:error unmarshalling arguments; Nested exception is:
Java.lang.ClassNotFoundException:PerfectTime_Stub

A lot of people are confused about this problem, because obviously see Perfecttime_stub and perfecttime These two classes are in the same directory, and Classpath also have set the current directory, since can load Perfecttime class execution, can load to Perfecttime_stub Bar, why also hint classnotfound. In fact, the class perfecttime_stub is not directly loaded by the Perfecttime execution line, but perfecttime when registering with RMI, requires rmiregistry to load the Perfecttime_stub class, Understanding this level of meaning will know that perfecttime_stub is actually used for rmiregistry. So the solution is:

(1) Before executing rmiregistry, set Classpath to find Perfecttime_stub class, as in the same DOS window, assuming that the Perfecttime_stub class is in the e:/workspace/testrmi/ Bin directory, the execution process is

C:/Documents and Settings/unmi>set classpath=%classpath%; E:/workspace/testrmi/bin

C:/Documents and Settings/unmi>rmiregistry 2005

(2) or at the command line, first enter the directory where the Perfecttime_stub class resides, and then execute the Rmiregistry (this method is essentially the same as the above, just the appropriate application of the current directory in the Classpath "."), the following process of execution

C:/Documents and Settings/unmi>e:

E:/>CD E:/workspace/testrmi/bin

E:/workspace/testrmi/bin>rmiregistry 2005

See: Rmiregistry is finding the stubs in its CLASSPATH

3. Perform client program Displayperfecttime exception java.security.AccessControlException:access denied (java.net.SocketPermission 127.0.0.1:1276 connect,resolve), while also generating an exception on the server Exception in thread "RMI TCP Connection (6) -127.0.0.1″ Java.security.AccessControlException:access denied (java.net.SocketPermission 127.0.0.1:1296 accept,resolve)

The solution that can be thought of directly is to add the 127.0.0.1:1276,127.0.0.1:1276 parsing connection permission, the method is preferable to the method listed in the 1th kind of exception, but this port is random. To resolve the purpose of these ports, 2005 is a direct specified port for the client to find the registered service object reference, which is fixed, and the above generated on the client and on the server 1276 and 1296 of the port, is random, is the port of data communication between the real client and the server that provides the service (rather than the registered server) at the time of the method call.

In order to satisfy the above port application, can only add permission Java.net.SocketPermission "localhost:*", "Accept,connect,resolve" in the security policy file; Allow acceptance, connection, resolution on all ports. If you want to access a lot of IP, but also written permission Java.net.SocketPermission "*:*", "accept,connect,resolve"; Convenient.

4. Perform client program Displayperfecttime abnormal java.rmi.UnmarshalException:Error unmarshaling return header; Nested exception is:java.io.EOFException, this exception should be relatively rare, where the client has permission to access a port on the service provider, while the service provider does not have permissions on a port or service to that client. The workaround is to change the security policy files for both the client and the server to access any port.

Summary: Above 1, 3, 43 kinds of situations are due to insufficient authority, if the granularity of security control does not require too fine, on the server side and the client can not set the full manager, or the policy file set to be able to accept, connect, resolve any IP and port: permission Java.net.SocketPermission "*:*", "accept,connect,resolve"; or use 1 (4) methods to ignore all IP and port detection.

After writing, I also feel like not how to sort out the problems, I hope not to make readers more confused.

Third, Attention matters:

1 after the modification, the rmiserver needs to be restarted.

Today because I forgot to declare that the object to be transferred is serializable, always throw

java.io.notserializableexception:cn.***.**. Exeresult.

Later, or have a problem, read a lot of posts, said all Exeresult within the class also if realize the serializable, so the Cpuusginfo also serialized or not. Finally, think whether should restart Rmiserver, sure enough to handle. I estimate that Nmserver started, it has loaded the class into the memory space, in time you update the new class file, within the space from the class did not update, so there is the above phenomenon.

2 All classes should be serialized: In order to verify the above sentence, but also deliberately to cpuusginfo, the result is thrown:

Java.io.NotSerializableException:cn.cnic.osg.datadefine.perfmonintor.CpuusgInfo

Very good. I don't cheat on me.

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.