What exactly is EJB, really so mysterious??

Source: Internet
Author: User
Tags object serialization

What exactly is EJB, really so mysterious?? Category: Java Learning-Basic 2010-08-02 21:26 37965 people read reviews (127) Favorites report EJB Server database server network distributed computing database

1. We cannot help but ask what is a "service cluster"? What is "enterprise-level development"?
Since EJB is meant for "service clusters" and "enterprise-level development", it must be said that what is called "service
Cluster "and" Enterprise-level development "!
This question is actually very important, because there is no white in the Java EE, there is no specific indicators or examples to tell
When do the vast numbers of programmers use EJBS? So we all have some associations that the EJB "distributed operation
"Load balancing" increases the operational efficiency of the system. However, it is estimated that a lot of people are mistaken, this "service cluster"
and "distributed operations" do not solve the problem of running load at all, especially the application system for database.
Why?
Let's take the EJB back to the prototype and analyze it slowly.


2.  The EJB to break the  
Let's analyze the EJB concept and see what clues we can find.

Anatomy of the


3.1 EJB concept
Let's take a look at the official explanation of EJB:
The core part of the business software is its business logic. The business logic abstracts the process of the entire business process and implements them using the
computer-counting language.
... The
Java EE approach to this problem is to extract the business logic from the client software and encapsulate it in a group of
pieces. This component runs on a separate server, and the client software provides real
current business logic through the services provided by the network invoke component, and the client software is simply responsible for sending the call request and displaying the processing results. In the Java EE,
the component that runs on a separate server and encapsulates the business logic is the EJB (Enterprise Java
Bean) component.
Here we mainly focus on these points, we will analyze each:
Anatomy 1: The so-called: "Business logic"  
We note that the main reference in the EJB concept is the encapsulation of "business logic", and what exactly is the business logic of
? So poised, in fact, this so-called "business logic" we can fully understand to perform a specific task "class
".
Anatomy 2: The so-called: "Extract the business logic from the client software, encapsulated in the component ... Run on a
server "
since we know that the concept of" business logic "is the" class "that performs a particular task, what is called" extracting from the client
Software "? In fact, this is the original put on the client's "class", take it out to the client, put
into a component, and put this component on a server to run.
3.2 Turns the concept of EJB into plain English  
becomes plain English, "the class of the software you write that needs to be executed is not placed on the client soft
, but it is placed on a server with a packet."
3.3 found a problem  
Whether it's "stereotyped writing" or "plain English," The EJB concept mentions a word-"client software".
"Client Software"? Does the concept of EJB say C/s software?
Yes, that's right!
EJB is to put those "classes" on a server, in the form of C/S software client to the "class" on the server to the "Incoming
line" call. The
is going to crash!
What is the relationship between EJB and JSP? EJB and JSP are related, but the relationship is not very large, at most the
server side of the JSP call the remote service EJB class, that's all.


4.1 What is the bottom of EJB?
We uncover the true meaning of the EJB "stereotyped" concept, then analyze the EJB's underlying implementation technology, through the underlying real
Techniques to analyze how EJBS work.


Implementation technology of 4.2 EJB
An EJB is a component that runs on a stand-alone server, and the client invokes the EJB object over the network. In the Java
, the technology that enables remote object invocation is RMI, and the EJB Technology Foundation is RMI. With RMI technology, the Java EE
By creating the EJB component as a remote object, the client can invoke the EJB object over the network.
4.3 See what RMI is.
Before you say RMI, you need to understand two nouns:
Serialization of objects
Distributed Computing and RPC
Noun 1: Serialization of objects
Serialization of objects: the serialization of an object is the process of converting an object's state into a byte stream and recovering from a byte stream
Like. After you convert the object state to a byte stream, you can save it to a file using the various byte stream classes in the Java.io package.
Or send the object data to another host over a network connection.
The above statement is a bit "stereotyped", we may as well explain in the vernacular: the serialization of objects is to put your program in the real
Instantiate an object of a class, for example, you customize a class MyClass, or any object of a class, to convert it to
Byte array, that is, it can be placed in a byte array, when you have placed an object in byte
Array, then of course you can dispose of it, the most used is to send him to the network remote computing
On the machine. shown in 2 11.


Noun 2: Distributed computing and RPC
RPC is not a purely Java concept because the concept of RPC has been introduced before Java was born, and RPC
is the abbreviation for "Remote Procedure call", which is called "remoting procedure calls". Most of the programming languages before Java
Words, such as Fortran, C, COBOL, and so on, are procedural languages, not object-oriented. So, these compilation
Process language is naturally used to represent work, such as a function or subroutine, to be executed on another machine on the network. Said
White, the local computer calls a function on the remote computer.
Shown in 2 12.


Noun 3: The combination of the two is RMI
The English name of RMI is "remote Method invocation", which is called "long-distance methods call" and it
Is the use of Java object serialization mechanism to implement distributed computing, the realization of the remote class object instantiation and the method of invocation.
More clearly, the use of object serialization to achieve remote invocation, which is the combination of the above two concepts, using
This method to invoke the remote class, there is no need to write the socket program, and do not need to sequence the object
The direct call on the line is very convenient.
A remote method call is a mechanism by which objects between computers invoke each other's functions, starting the other process, using this
mechanism, the program syntax rules used by an object on one computer when calling a method on another computer, and
The syntax rules for method calls between objects on the local machine are the same.
shown in 2 13.

4.4 Advantages
This mechanism brings great convenience to the design and programming of distributed computing system. Just follow the RMI rules design process
The network details under RMI, such as: TCP and sockets, etc. Any two computers
The communication between them is solely the responsibility of RMI. Invoking objects on a remote computer is as convenient as a local object.
RMI can pass complete objects as parameters and return values, not just predefined data types. So
That is, a complex type such as Java Hassi can be passed as a parameter.
4.5 Disadvantages
If it is a simpler method call, the execution efficiency may be much slower than local execution, even if the remote socket
The simple data returned by the mechanism is slower, because the information it needs to communicate between networks is not only
Contains the return value information for the function and also contains the byte content after the object is serialized.
The 4.6 EJB is based on RMI
With RMI technology, the EJB component is created as a remote object by the Java EE, while the EJB uses RMI technology, but only
To define remote interfaces without generating their implementation classes, this masks some of the details of RMI technology.
But anyway, the basis of EJB is still RMI, so if you want to understand the principle of EJB, just put RMI
The principle of the work is clear on the line. You can also figure out when to use EJBS when you don't need ejbs.


5. The so-called "service Cluster" in EJBS
Now that we know, RMI is to put a variety of tasks and functions of the class on different servers, and then through the various services
The call rules established between the services implement distributed operations and understand the concept of EJB's so-called "service Cluster".
is to put a few classes that were originally on a computer and run it on another computer to share the
The CPU and memory resources that these classes need to occupy. At the same time, different software function modules can be put into different
On the server, when you need to modify some features, you can modify the class on these servers directly, modify all the guest
The client software has been modified. shown in 2 14.

6. Is this deployment impeccable?
The "service cluster" shown in Figure 2 14 looks "invulnerable", in fact, it is not a picture of the complete, we come
Complete the picture and see what the problem is.
6.1 Bottlenecks on the database side
After careful observation, it is found that this configuration has bottlenecks, as shown in 2 15.


Let's take a look at the structure of Figure 2 15, and now if you want to implement a query for the same database for each server,
, regardless of how many feature servers you deploy, you need to query for a database server. Other words
No matter how "distributed" Your calculations are, you also need to get data from a single server. Although, it seems that the various functions
Can be distributed across different servers to share the CPU resources of each host computer, however, the real bottleneck is not
Here, instead, the database server is there. The database server will be very busy to handle the queries and operations of each server.
Make a request.
As a result, this structure allows us to understand that EJB does not completely solve the load problem because the bottleneck
Not in the location of the function module, but here in the database server.
6.2 What if we separate the database and share the data
Some readers will certainly think of the following application structure, as shown in 2 16.


is to put a database behind each of the feature servers, so it doesn't solve the problem in the previous section.
It? Yes, it solves the problem of database query load, but there is a new problem, that is, the problem of "data sharing"
It's not easy to solve.
6.3 The network is under great pressure to make your application slow as the old bull
Let's go ahead and look at 2 15. There are two networks in this architecture, one is "a net" and the other is "B
Network ", the two networks are different. "B Network" is often a local area network, the general bandwidth is 10m/100m, faster, so
To be good to say, however, "A network" is often the Internet or is using a telecommunications network interconnection VPN network or called WAN. "A Net"
is characterized by a generally narrow bandwidth, such as ADSL network only has 512k-2m bandwidth, due to the cost of WAN interconnection more
High, so there is generally no higher bandwidth.
And on this network is exactly running the function module and the client software exchange between the data, and this part of the data is precisely
The advantages are very bandwidth-intensive.
As a result, the application architecture can see how slow it is to run. If you don't exaggerate, you'd like to have a little old cow.
It's as slow as pulling a broken car.
A system such as the old cow:
At present, in China, the Internet is a large company operating network management system, one of its early management software is
Using this architecture to do the C/s structure of the application system.
Once, as an evaluator, I evaluated the application system and deployed it to a non-carrier large network
, we have the situation described above, the speed has reached the point of unbearable, open a flow chart,
Sometimes it takes 15 minutes to complete the presentation. However, the system did not find this problem during the development phase,
Why is it? Because they do not take into account the complexity of the actual user-connected network of the application, thus causing the company to be more
Large losses, so that the development structure was eventually abandoned.


7. EJB Ingenious, the Java EE is not required to use EJB
It seems that the system relationship between EJB and the B/s structure of the Web application is not very large, as explained in the above section, in fact
Or else. If we understand the "client program" as a server, it can also be applied, and,
If the server makes EJB calls to each other, there is no question of WAN bandwidth throttling.
However, do not use EJBS as much as possible:
1, simple Web application development, do not need to use EJB.
2. Applications that need to be used in conjunction with other service programs, but the call or return of a custom network protocol can be resolved
Applications that do not require the use of EJBS.
3, more people concurrent access to the C/s structure of the application, try not to use EJB.

Summarize:

A.EJB implementation principle: is to put the original client implementation of the code on the server side, and rely on RMI to communicate.

B.rmi implementation principle: It is through the Java object Serializable mechanism to achieve the distribution of computing.

C. server cluster: It is through RMI communication, connect the server of different function module, in order to realize a complete function.

What exactly is EJB, really so mysterious??

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.