What is EJB

        Recently, I came into contact with EJB in the project, but I don’t know much about EJB, so I found some information on the Internet. The following is a clearer description of my personal feeling, so I reprinted it for reference. The article is transferred from https://www.cnblogs.com /strugglion/p/6027318.html.

        

What exactly is EJB?

 

1. We can't help but ask, what is a "service cluster"? What is "enterprise development"? 


Since it is said that EJB is for "service cluster" and "enterprise-level development", we have to talk about what is called "service cluster" and "enterprise-level development"! This question is actually quite critical, because there is no clear explanation in J2EE, and there are no specific indicators or examples to tell the majority of programmers when to use EJB and when not to use it. So everyone has some associations, thinking that EJB "distributed computing" refers to "load balancing" to improve the operating efficiency of the system. However, it is estimated that many people are mistaken, this "service cluster" and "distributed computing" does not fundamentally solve the problem of running load, especially for database application systems.
Why?

Let's put the EJB back to its original shape for everyone to analyze slowly.


2. Break the EJB apart and smash it into pieces 


Let's take a good look at the concept of EJB and see what clues can be found.


3.1 Anatomy of EJB Concepts


Let's take a look first, the official explanation of EJB:
the core part of business software is its business logic. Business logic abstracts the flow of the entire business process and implements them using computer language.
...
J2EE's approach to this problem is to extract the business logic from the client software and encapsulate it in a component. This component runs on an independent server, and the client software invokes the services provided by the component through the network to implement business logic, while the client software is only responsible for sending call requests and displaying processing results. In J2EE, this component that runs on an independent server and encapsulates business logic is the EJB (Enterprise  Java Bean) component. Among them, we mainly focus on the following points, let's analyze them one by one:

Analysis 1: The so-called: "business logic" 
We noticed that the main thing mentioned in the concept of EJB is the encapsulation of "business logic", and what is this business logic? It's so daunting to say that, in fact, this so-called "business logic" can be understood as a "class" that performs specific tasks.


Analysis 2: The so-called: "Extract business logic from client software, encapsulate it in components...run on a server"
Since we know that the concept of "business logic" is a "class" that performs specific tasks, then, What is "extracted from client software"? In fact, this is to take the "class" that was originally placed on the client, take it out and not put it on the client, put it into a component, and put this component on a server to run.


3.2 Turn the concept of EJB into vernacular 


It becomes a vernacular, "put the classes in the software you write that need to perform the specified tasks, not on the client software, but package them and put them on a server".


3.3 Found a problem 


不管是用"八股文"说,还是用大白话说这个EJB 概念都提到了一个词--"客户端软件"。
"客户端软件"?难道EJB 的概念中说的是C/S 软件?
是的,没错!
EJB 就是将那些"类"放到一个服务器上,用C/S 形式的软件客户端对服务器上的"类"进行调用。
快崩溃了吧!
EJB 和JSP 有什么关系?EJB 和JSP 有关系,但是关系还真不怎么大,至多是在JSP 的服务器端调用远端服务上的EJB 类,仅此而已。


4 .1 EJB 的最底层究竟是什么 


我们揭开了EJB"八股"概念的真谛,那么,再来分析EJB 的底层实现技术,通过底层实
现技术来分析EJB 的工作方式。


4.2 EJB 的实现技术


EJB 是运行在独立服务器上的组件,客户端是通过网络对EJB 对象进行调用的。在Java中,能够实现远程对象调用的技术是RMI,而EJB 技术基础正是RMI。通过RMI 技术,J2EE将EJB 组件创建为远程对象,客户端就可以通过网络调用EJB 对象了。


4.3 看看RMI 是什么东东 


在说RMI 之前,需要理解两个名词:
对象的序列化
分布式计算与RPC


名词1:对象的序列化 
对象的序列化概念:对象的序列化过程就是将对象状态转换成字节流和从字节流恢复对象。将对象状态转换成字节流之后,可以用java.io 包中的各种字节流类将其保存到文件中,或者通过网络连接将对象数据发送到另一个主机。
上面的说法有点"八股",我们不妨再用白话解释一下:对象的序列化就是将你程序中实例化的某个类的对象,比如,你自定一个类MyClass,或者任何一个类的对象,将它转换成字节数组,也就是说可以放到一个byte 数组中,这时候,你既然已经把一个对象放到了byte数组中,那么你当然就可以随便处置了它了,用得最多的就是把他发送到网络上远程的计算机上了。如图2 11所示。

 
名词2:分布式计算与RPC 
RPC 并不是一个纯粹的Java 概念,因为在Java 诞生之前就已经有了RPC 的这个概念,RPC是"Remote Procedure Call"的缩写,也就是"远程过程调用"。在Java 之前的大多数编程语言,如,Fortran、C、COBOL 等等,都是过程性的语言,而不是面向对象的。所以,这些编程语言很自然地用过程表示工作,如,函数或子程序,让其在网络上另一台机器上执行。说白了,就是本地计算机调用远程计算机上的一个函数。如图2 12所示。

 
名词3:二者结合就是RMI 
RMI 英文全称是"Remote Method Invocation",它的中文名称是"远程方法调用",它就是利用Java 对象序列化的机制实现分布式计算,实现远程类对象的实例化以及调用的方法。说的更清楚些,就是利用对象序列化来实现远程调用,也就是上面两个概念的结合体,利用这个方法来调用远程的类的时候,就不需要编写Socket 程序了,也不需要把对象进行序列化操作,直接调用就行了非常方便。
远程方法调用是一种计算机之间对象互相调用对方函数,启动对方进程的一种机制,使用这种机制,某一台计算机上的对象在调用另外一台计算机上的方法时,使用的程序语法规则和在本地机上对象间的方法调用的语法规则一样。如图2 13所示。

4.4 优点


这种机制给分布计算的系统设计、编程都带来了极大的方便。只要按照RMI 规则设计程序,可以不必再过问在RMI 之下的网络细节了,如:TCP 和Socket 等等。任意两台计算机之间的通讯完全由RMI 负责。调用远程计算机上的对象就像本地对象一样方便。RMI 可将完整的对象作为参数和返回值进行传递,而不仅仅是预定义的数据类型。也就是说,可以将类似Java 哈西表这样的复杂类型作为一个参数进行传递。


4.5 缺点 


如果是较为简单的方法调用,其执行效率也许会比本地执行慢很多,即使和远程Socket机制的简单数据返回的应用相比,也会慢一些,原因是,其在网络间需要传递的信息不仅仅包含该函数的返回值信息,还会包含该对象序列化后的字节内容。


4.6 EJB 是以RMI 为基础的

 

通过RMI 技术,J2EE 将EJB 组件创建为远程对象,EJB 虽然用了RMI 技术,但是却只需要定义远程接口而无需生成他们的实现类,这样就将RMI 技术中的一些细节问题屏蔽了。但不管怎么说,EJB 的基础仍然是RMI,所以,如果你想了解EJB 的原理,只要把RMI的原理搞清楚就行了。你也就弄清楚了什么时候用EJB 什么时候不需要用EJB 了。


5. EJB 中所谓的"服务群集" 


既然已经知道了,RMI 是将各种任务与功能的类放到不同的服务器上,然后通过各个服务器间建立的调用规则实现分布式的运算,也就明白EJB 所谓的"服务群集"的概念。就是将原来在一个计算机上运算的几个类,分别放到其他计算机上去运行,以便分担运行这几个类所需要占用的CPU 和内存资源。同时,也可以将不同的软件功能模块放到不同的服务器上,当需要修改某些功能的时候直接修改这些服务器上的类就行了,修改以后所有客户端的软件都被修改了。如图2 14所示。

 

6. 这种部署难道是无懈可击 


图2 14所示的这个"服务群集"看似"无懈可击",其实是它这个图没有画完整,我们来把这个图画完整,再来看看有什么问题没有。


6.1 瓶颈在数据库端 


仔细观察之后,发现这种配置是有瓶颈的,如图2 15所示。

 
我们看看图2 15的结构图,现在如果想实现各个服务器针对同一个数据库的查询,那么,不管你部署多少个功能服务器,都需要针对一个数据库服务器进行查询操作。也就是说,不管你的"计算"有多么"分布"也同样需要从一台服务器中取得数据。虽然,看起来将各个功能模块分布在不同的服务器上从而分担了各个主计算机的CPU 资源,然而,真正的瓶颈并不在这里,而是,数据库服务器那里。数据库服务器都会非常忙的应付各个服务器的查询及操作请求。
因此,通过这个结构图使我们了解到了EJB 根本不能完全解决负载的问题,因为,瓶颈并不在功能模块的所在位置,而是在数据库服务器这里。


6.2 假如分开数据库,数据共享怎么办 


有的读者一定会想到下面的这个应用结构,如图2 16所示。

 
就是把每一个功能服务器后面都部署一个数据库,这样不就解决了上节所说的问题了吗?是的解决了数据库查询负载的问题,然而又出现了新的问题,就是"数据共享"的问题就又不容易解决了。


6.3 网络面临较大压力,让你的应用慢如老牛

 

我们再向前翻看看如图2 15所示的这种架构中存在两个网络,一个是"A 网"一个是"B网",这两个网络是不同的。"B 网"往往是局域网,一般带宽是10M/100M,速度较快,因此到还好说,然而,"A 网"往往是互联网或者是利用电信网络互联VPN 网或称广域网。"A 网"的特点是带宽一般较窄,如ADSL 的网络仅仅有512K-2M 的带宽,由于广域网互联的成本较高,所以一般不会有较高的带宽。而在这个网络上恰恰跑的是功能模块和客户端软件之间交换的数据,而这部分数据恰恰优势非常占用带宽的。因此,这个应用架构其运行速度可以想见是多么的慢了。说句不夸张的话,有点想老牛拉破车一样的慢。一个如老牛的系统:目前在中国互联网做运营商网络管理系统的一个大公司,它的一个早期的网管软件就是采用了这种架构来做的C/S 结构的应用系统。有一次,我作为评估者来对其应用系统进行评估,将其部署到一个非运营商大型的网络中的时候,便出现了我们上述描述的情况,速度已经到了难以忍受的地步,打开一个流量图,有时候需要用15分钟的时间才能呈现完整。然而,该系统在开发阶段并没有发现这个问题,为什么呢?因为,他们没有考虑到应用的实际用户连接网络的复杂性,从而给该公司造成较大损失,以至于,这个开发架构被最终遗弃。

 

7. EJB 活学活用,J2EE 不是必须使用EJB 


Through the explanation of the above section, it seems that EJB is not related to the system of B/S structure for developing Web applications, but it is not the case. If we understand the "client program" as a certain server, this can also be applied, and if the servers make EJB calls between each other, there is no problem of WAN bandwidth limitation.
However, try not to use EJB in the following situations:
1. For relatively simple pure Web application development, EJB is not required.
2. Applications that need to be used in conjunction with other service programs, but applications that call or return custom network protocols can be resolved, do not need to use EJB.
3. Try not to use EJB for applications with C/S structure that are accessed concurrently by many people.

 

 

Summarize:

a. EJB implementation principle: It is to put the code originally implemented on the client side on the server side, and rely on RMI for communication.

b.RMI implementation principle: It is to realize distributed computing through the Java object serialization mechanism.

c. Server cluster: It is to connect the servers of different functional modules through the communication of RMI to realize a complete function.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326016057&siteId=291194637