to ejb or not to ejb 1 (for 网络苍蝇)

发表于:2007-07-01来源:作者:点击数: 标签:
To EJB, or not to EJB? Addressing the issues and decisions that go into adopting an EJB-based solution Summary -- Summary Since J2EE@#s (Java 2 Platform, Enterprise Edition) earliest days, Enterprise JavaBean (EJB) technology has caused mu

To EJB, or not to EJB?

Addressing the issues and decisions that go into adopting an EJB-based solution


Summary
-->
Summary
Since J2EE@#s (Java 2 Platform, Enterprise Edition) earliest days, Enterprise JavaBean (EJB) technology has caused much controversy, with developers and architects constantly struggling to make best use of EJB in their projects. Consequently, our industry has spawned folklore and rules of thumb to guide us how best to use EJB -- some true, some out of date, and some pure fabrication. In this article, Humphrey Sheil explains when and how to use EJB in your J2EE application, as well as how to know when EJBs are not the right solution for you. (3,500 words; December 7, 2001)
-->By Humphrey Sheil




o EJB, or not to EJB: that is the question.
Whether @#tis nobler in the mind, to suffer
The slings and arrows of outrageous licensing;
Or to take arms against a sea of potential overheads and features,
And by opposing end them? To roll your own: to reinvent the wheel;
No more; and by reinvent, to say, we continue
The heart-ache of low-level systems maintained in-house,
and the thousand natural shocks
That flesh is heir to; @#tis a consummation
Devoutly to be avoided.

(For Shakespeare@#s original "To be, or not to be," speech from Hamlet, see the .)
Once you have forgiven me for taking Hamlet apart with a chainsaw, you will realize that the lines above sum up the quandary every J2EE (Java 2 Platform, Enterprise Edition) architect faces: Are you over engineering your solution by employing EJB (Enterprise JavaBean) technology when the signs point to a different approach, indeed, perhaps a different technology? On the other hand, if you decide not to specify an EJB-based solution, have you subsequently found that a sizeable chunk of your code (that must be maintained) handles tasks best addressed by a container or a server?
This article shares with you how I methodically answer these questions.
Before I begin, let me set your expectations and explain my motivations for writing this article. First up, I do not answer the EJB/not EJB question for you, but I do provide a toolkit with which you can answer that question for yourself. Second, I have no hidden agendas: I do not represent an EJB server vendor, so I won@#t push EJBs for everything. On the other hand, I do not represent a technology or technique competing with EJB, apart from presenting it to you here as an alternative, so I won@#t deride an EJB solution where it makes sense.
Indeed, my motivation for this article stems from reader requests following my last JavaWorld article, "." In that article, I briefly touched on the danger of over-engineering, specifically around EJBs. Many readers requested a more in-depth treatise on the subject -- so here it is!
With that in mind, I propose to proceed as follows: In order to understand if you need EJB, you must understand an EJB solution@#s main advantages and disadvantages. As with all technologies, EJB has its weak points, and knowing these weak points is the first step to avoiding them. Following the discussion on EJB@#s weaknesses, I briefly outline alternatives to an EJB-based solution, both inside and outside the Java/J2EE world.
The final section is a proofing check. When you reach this part of the article, you should be comfortable applying the principles previously outlined to any business scenario. In that section, I present a progressive scenario that requires a solution (I promise not to pick either a pet store or an ATM machine!), make my call on the EJB/no EJB decision, and let you agree or disagree with my reasoning.
EJB advantages and disadvantages
Moving along, let@#s look at EJB@#s strong and weak points. In the disadvantages section, I also give solutions to commonly encountered EJB weaknesses.
EJB advantages
First, let@#s examine the advantages that the EJB architecture and programming model provides:
  1. The underpinning : Funnily enough, EJB@#s main advantage -- its specification -- also represents its biggest disadvantage, as you@#ll see below. The specification stipulates everything for EJB, from the various types, lifecycles, and restrictions, right through to roles and responsibilities. Many vendors@# application servers conform to the specification, allowing you to select a best-of-breed solution.
    EJB@#s universal nature offers application developers well-defined roles (I@#ll write the container and server, you write the business logic aclearcase/" target="_blank" >ccording to my APIs). Moreover, developers enjoy the certainty the spec delivers. Indeed, by choosing an EJB solution, developers benefit from a large skills pool, along with established best practices and open standards. Finally, found a show-stopper bug in your current server? If you have paid attention to portability issues in your design and implementation, with EJB you can circumvent it by moving to another vendor with minimum effort.
  2. Integration with the J2EE platform: Since Sun Microsystems introduced EJB in 1997, a strong enterprise computing environment has grown up around it, including technologies such as servlets, JMS (Java Message Service), JSP (JavaServer Pages), the JCA (Java Connector Architecture), JDBC (Java Database Connectivity), security, and transaction management. Such integration enhances EJB@#s attractiveness as the J2EE platform includes so much other complementary technology.
    For you, the application architect or developer, you can rest easy because no matter how complex your application, the integrated, fully featured J2EE platform can handle your requirements.
  3. Almost transparent scalability: Because you delegate so much to the container, the vendor can scale server-side resources to meet fluctuations in demand. I say "almost transparent" because developers are somewhat affected. In the case of horizontal scaling and entity beans, for example, where application servers are clustered, you usually need to modify deployment descriptors to let individual server instances know they are not the only processes accessing and modifying a persistent store.
  4. Free access and usage of complex resources: When you buy a specification-compliant EJB server, you receive important features for free, namely transaction and security management, resource pooling, JNDI (Java Naming and Directory Interface), component lifecycle management, and more -- representing heaps of systems-level code you would otherwise have to design, build, and maintain in-house.
  5. A strong and vibrant industry and community: All the major IT players, with the exception of Microsoft, support EJB and J2EE, which means you are investing in a technology in which many other people have also invested. That investment commitment means the technology will be around for a long time. Additionally, enterprise Java technology continuously improves. For example, the EJB specification is currently at 2.0, having evolved from 1.0 and 1.1; a pattern likely to continue.

原文转自:http://www.ltesting.net