A talk on Java Learning (VI.)

Source: Internet
Author: User

The RMI mechanism in 1.Java

RMI's full name is a remote method call, I believe many friends have heard that the basic idea can be explained by a classical analogy: a computer to calculate a two-digit addition, but a can not do, so call another computer B to help, B has the function of calculating addition, A calling it is just as handy as calling this feature to be its own. This is called a remote method call.

Remote method invocation is the backbone of EJB implementation, and establishes the core idea of distributed application. This is well understood, and then take the above example of computational addition, a only know the way to call Computer B, they do not have the functions of B, so a computer side can not see B to perform this function of the process and code, because it can not see, so neither the opportunity to steal nor the opportunity to change the method code. The EJB is formally based on this idea to accomplish its task. As simple additions become complex database operations and E-commerce transaction applications, such security and the convenience of distributed applications show an advantage.

Well, back to the details, how do you implement a remote method call? I hope that when you learn any technology can try to rely on their own subconscious judgment, as long as your idea is reasonable and robust, then it is likely that it is actually doing so, after all, the truth is contained in the ordinary life details. So as long as you take some weak Java foundation to think about RMI, actually can come up with a probably.

A requires a server role that has a true functional code approach. For example B, it provides addition service B) if you want to use B's function remotely, you need to know B's IP address C if you want to use the function of B remotely, you need to know the name of that particular service in B.

It is natural for us to think of these, though imperfect, but already very close to the right approach. In fact, RMI is also proud of a very important feature of Java, that is, the Java reflection mechanism. We need to know the name of the service, but also have to hide the implementation of the code, how to do it? The answer is: interface!

For example: public interface Man () {public void SayHello ();}

public class Personimpla implements person{public Personimpla () {}

public void SayHello () {System.out.println ("hello! ");} }

public class PERSONIMPLB implements person{public personimplb () {}

public void SayHello () {System.out.println () (' Nice to meet you! ");} }

Client: Person p = naming.lookup ("Personservice");p. SayHello ();

It's a few pieces of code that contains almost all of the implementation technology, do you believe it? The client requests a say Hello service, which receives the request at runtime and returns an object using the Java reflection Mechanism's class.newinstance (), but the client does not know whether the server is returning Impla or IMPLB, It accepts that the parameter signature is person, it knows that the object that implements the person interface must have a SayHello () method, which means that the client does not know how the server really implements it, But it is clear by understanding the person interface that the service method name it uses is called SayHello ().

As such, the server only needs to expose its own interface for the client, and all clients can choose the services they need. This is like a restaurant just come up with their own menu to allow customers to choose, you can in the kitchen in the background to make a road, how it does not usually let customers know! (Ancestral recipes, ^_^)

The last point is that I call lookup to find an object called the Personservice name, which is called Jndi (Java Naming and directory interface), as long as the server sees the name and finds the corresponding object name in its own directory (equivalent to the phone book). I believe everyone has heard of it.

Interested friends may wish to do their own RMI application, many predecessors of the blog has a simple example. Tip the Rmi.exe and rmiregistry.exe two commands in the JDK's Bin directory allow you to build a server and provide remote services. Because the example is very easy to find, I do not give an example!

2.JVM Sand Box & Frame

RMI is too much to talk about, it is really trying to make it clear, hope to help everyone. Finally, let's give you a brief look at the JVM framework, which we call the Java sandbox. The basic components of the Java sandbox are as follows: a) class loader structure B) class File Checker C) Security Manager and Java API for Java virtual machines (d)

The class loader works on the Java sandbox in 3 ways: a. It prevents malicious code from interfering with well-meaning code B. It protects the trusted class library boundary C. It includes the code in the protected domain, determining what the code can do

Virtual machines provide different namespaces for classes loaded by different ClassLoader. Namespaces consist of a series of unique names, each loaded class will have a name that is maintained by the Java Virtual machine for each class loader, and they are not even visible to each other.

We often say that the package (package) is the first occurrence of the specification in the Java Virtual Machine version 2nd, which is properly defined as a collection of the same package and multiple types loaded by the same class loader. The mechanism used by the class loader is the parental delegation mode. Specific loader framework I have explained in the Java Chat (i), said at that time the outermost loader is appclassloader, in fact, the network layer of the words Appclassloader can also as parent, There is also a more outer loader urlclassloader. To prevent malicious attacks on class files loaded by URLs we certainly need to have different access namespaces and make the safest loading order, which is simply two points:

A. Start loading from the inner JVM with the class loader outer-layer malicious class with the same name cannot be loaded first and cannot use B. Because of the strict use of the packet to differentiate the access domain, the outer layer of malicious classes through the built-in code can not get access to the inner class, the destruction of the code will naturally not take effect.

Attached: platform-independent of Java, one example can clearly illustrate this feature: In general, the int placeholder width in C or C + + is based on the word length of the target platform, which means that compiling the same C + + program for different platforms will behave differently at run time. However, for the int in Java is a signed integer that is identified by a 32-bit binary complement, float is a 32-bit floating-point number that adheres to the IEEE 754 floating-point standard.

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.