The basis of the EJB technology is the other two technologies: RMI-IIOP and JNDI.
To understand EJB, you must first understand the RMI-IIOP and JNDI. Therefore, we first understand these two technologies before introducing the details of EJB. Our introduction is basic, so most organizations only need to understand this.
Java RMI-IIOP
Java RMI-IIOP (Java Remote Method Invocation over the Internet Inter-ORB Protocol) is the network mechanism of J2EE. Java RMI-IIOP allows you to write distributed objects so that the communication scope of objects can be in memory, across Java virtual machines, across physical devices.
Remote Method Invocation
Remote procedure call is a process in which one machine's process calls another machine. Remote method invocation is more advanced than RPC, allowing communication between distributed objects. The RMI-IIOP allows you to call methods of remote objects, not just processes. This facilitates object-oriented programming. Java RMI (Remote Method Invocation Remote Method call) is implemented in Java in JDK1.1, which greatly enhances Java's ability to develop distributed applications. Java, as a popular network development language, has great power in its powerful ability to develop distributed network applications, RMI is one of the core solutions for developing a network distributed application system with hundreds of thousands of pure Java. In fact, it can be regarded as the Java version of RPC. However, traditional RPC cannot be well applied to distributed object systems. Java RMI supports communication between Program-level objects stored in different address spaces to achieve seamless remote calls between remote objects.
Remote method invocation is never simple. Several issues need to be considered:
Variables alling and unmarshalling. variables (including Java basic types and objects) are transmitted across different machines over the network. What should I do if the target machine represents data differently from the original machine? For example, the binary database is different. Therefore, implicit alling and unmarshalling are the processes for passing variables.
Variable transfer method. There are two ways to pass a variable: pass-by-value and pass-by-reference.
For the former method, you only need to use a copy of the target method, but for the latter, any modifications to the variables of the remote method will affect the source data.
Network and machine instability. A mechanism is required to ensure that the normal operation of the system is not affected after a JVM crash.
In the Java distributed object model, remote object is an object that can be called from other Java virtual machines (possibly on different hosts. Objects of this type are described by one or more remote interfaces (which is a Java interface for declaring remote object methods. Remote method call (RMI) is the action of calling a remote interface on a remote object. More importantly, the method call Syntax of a remote object is the same as that of a local object.
Remote Interface
RMI-IIOP follows the principle of interface and implementation. All the network code you write is applied to interfaces instead of implementations. In fact, you have to use the example in the RMI-IIOP and there is no other choice. It is impossible to execute a remote call directly on your object implementation. You can perform this operation only on the object class interface.
So when using RMI-IIOP, you must create a client interface called remote interface. This Remote interface should be extended java. rmi. remote interface.
Remote Object Implementation
The physical location of remote objects and clients is not very important. It can run in the same address space or across the Internet.
To make the object a remote object, perform the following steps:
Inheriting javax. rmi. PortableRemoteObject. PortableRemoteObject is the base class for remote calls. When you call the constructor for a remote object, the constructor of the PortableRemoteObject object will also be called automatically.
Does not inherit javax. rmi. portableRemoteObject. if your remote object needs to inherit other classes, and Java does not allow multiple inheritance, you cannot inherit PortableRemoteObject. in this case, you need to manually call javax. rmi. portableRemoteObject. exportObject ().
Stub and Skeletons
Let's look at the hidden network architecture behind RMI-IIOP. One benefit of RMI-IIOP is that you don't have to worry about whether the object you want to call is local or remote. This is called local/remote transparency.
RMI applications generally include two independent programs: server programs and client programs. A typical server application creates multiple remote objects so that these remote objects can be referenced and waits for the client to call the methods of these remote objects. A typical client program obtains one or more remote object references from the server, and then calls the remote object method. RMI provides a mechanism for communication and information transmission between servers and clients.
During communication with a remote object, RMI uses the standard mechanism: stub and skeleton. stub of the remote object act as the local representative or agent of the remote object.
The caller will call the local stub method, while the local stub will be responsible for executing the method call to the remote object.
In RMI, the remote object's stub is the same as the remote interface set implemented by the remote object.
When the stub method is called, the following operations are performed:
(1) Initiate a connection to a remote virtual machine that contains a remote object;
(2) Grouping (writing and transmitting) parameters of remote virtual machines );
(3) Wait for the method call result;
(4) uncompile (read) the return value or return an exception;
(5) return the value to the caller.
To show the caller a simple call mechanism, stub hides details such as parameter serialization and network-level communication. In a remote virtual machine, each remote object can have a corresponding skeleton (skeleton is not required in the JDK1.2 environment ).
Skeleton is responsible for allocating calls to actual remote objects.
It performs the following operations when receiving method calls:
(1) parameters of the remote method for decoding (reading;
(2) Call the methods on the actual remote object implementation;
(3) group (write and transmit) the result (return value or exception) to the caller.
Stub and skeleton are generated by the rmic compiler.
Local/remote transparency is not that easy to implement. To shield you from calling objects on a remote host, the RMI-IIOP needs to simulate a local object for you to call. This local object is called stub. It is responsible for accepting local method call requests and entrusting these requests to objects that actually implement them (which can be located through the network ). This makes the remote call look the same as the local call.
To use RMI to write distributed object applications, you need to do the following:
(1) locate a remote object. Applications can use one of the two mechanisms to get reference to remote objects. Rmiregistry, a simple naming tool for RMI, can be used to register its remote objects, and remote object references can be passed and returned as part of common operations.
(2) Communication with remote objects. The details of remote communication between objects are handled by RMI. For programmers, remote communication looks like a standard Java method call.
(3) Load class bytecode for objects passed as parameters or return values. Because RMI allows calling programs to send pure Java objects to remote objects, RMI provides the necessary mechanism to load object code and transmit object data.
When the RMI distributed application is running, the server calls the registration service program to associate the name with a remote object. The client registers the service program on the server to search for the remote object with the name of the remote object, and then calls its method.
Locate a remote object. Applications can use one of the two mechanisms to get reference to remote objects. Rmiregistry, a simple naming tool for RMI, can be used to register its remote objects. Remote Object references can also be passed and returned as part of common operations.
Communicate with remote objects. RMI processes the details of communication between remote objects. For programmers, remote communication looks like a standard Java method call. Load class bytecode for objects passed as parameters or return values. Because RMI allows calling programs to pass pure Java objects to remote objects, RMI provides the necessary mechanism, it can load object code and transmit object data. The server calls the registration service program to associate the name with a remote object. In the Server Registration Service Program, the client searches for the remote object with the name of the remote object and then calls its method. RMI can load class bytecode with any URL protocol (such as HTTP, FTP, and file) supported by the Java system.
Stub only solves half of the problem. We also hope that remote objects do not need to consider network issues. Therefore, the remote object also needs a local skeleton to accept the call. Skeleton accepts network calls and delegates calls to remote objects for implementation.
Your J2EE server should provide a method to generate necessary stub and skeleton, so as to reduce your burden on network problems. A typical example is to use a command line tool. For example, sun's J2EE reference implementation package uses a tool named rmic (RMI compiler) to generate stub and skeleton classes. You should deploy stub on the client and deploy skeleton on the server.
Object serialization and variable transmission
In RMI distributed application systems, Java objects transmitted between servers and clients must be serializable objects. Non-serializable objects cannot be transmitted in object streams. Object serialization extends the core Java input/output class and supports objects. Object serialization supports converting object encoding and object encoding that can be accessed through them into byte streams. It also supports the complementary reconstruction of object images in the stream. Serialization is used for lightweight persistence and communication by means of socket or remote method call (RMI. Serialization now includes an API (Application Programming Interface, Application Interface) that allows serialization data of specified objects independent of the class domain, it also allows existing protocols to write serialized data domains to or read from the stream to ensure compatibility with the default read/write mechanism.
Write Applications