• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What exactly is an EJB?

 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly is an EJB? Why was it invented? I've tried to go through some Sun tutorials but I wasn't convinced. I have used JSP/Servlets and it seems that it can do the same job (corrections very welcome), along with Struts. This is the only area of J2EE that leaves me bewildered. Is it some way of encapsulating all the business logic into a class? Don't we do that anyway (e.g. ShoppingCart.java for an ecommerce application)? Any simple guides on this?
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the way I understood EJBs.

1. Type of container : EJBs require ejb container and servlets require web server to run.

2. Type of ejb Beans: In ejb you can separate business logic by using session beans. Entity Beans provide for database mapping. You don't need to use database specific sql if using CMP but need to get acquinted with ejb-ql. MDB i.e. Message driven Beans provide for asynchronous messaging. This feature is not available in servlets.

3. All the ejb beans provide for transactional management either thru CMT(Container managed Transactions) or BMT (Bean Managed Transactions.) The Transaction scope can be extended to subsequent beans calls. I don't know how this transaction management is done in servlet.

4. Java Naming & Directory service- which provides for directory service where all different resources like printer,database,ejbs can be referred by unique ID. Hence providing for location transparency.

Ejbs and servlets both can be invoked remotely. Shopping cart example can be implemented by both using jsp and struts. I used servlets long time back so not sure how transaction management is done in servlets. I think the diff. between servlets and ejb lies in location transparency and transaction management.
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Damanjit.

I went through the Sun Tutorial (Lesson 1 and 2) with a fine tooth comb and I am still not convinced. This is why:

The example shown is a simple web application which calculates a bonus. A simple client sends the social security number and multiplier via a HTTP Browser. This is made with both Servlets and EJBs, and I compared both.

Lesson 1 - Servlets
Lesson 2 - Same as Lesson 1 but with EJBs.

Lesson 1
The "business logic" in the servlet is a simple 2-line method "calculateBonus". This is very simple to understand and in my opinion, to maintain.

On compile, you only have one class: BonusCalculationServlet.java

No problem, everything works, the code is simple to read and you can continue to add more business logic as you need to.

Lesson 2

Suddenly, with the EJB variant, all hell breaks loose. You must create SIX different classes. This implements that, an interface for this, a context for that and so on. Remember, the business logic method is 2 lines in the servlet and we finished with one simple class.

On compile with EJB, you get:
-BonusCalculationServlet.java
-BonusCalculator.java
-BonusCalculatorHome.java
-LocalBonusCalculator.java
-LocalBonusCalculatorHome.java
-BonusCalculatorBean.java

So all in all, I know which "method" I would choose. EJB just seems to add an unneccessary tier of more classes and interfaces for very little return. Whats wrong with creating a simple BonusCalculator.java class with "calculate()"? The servlet can then create an instance of this object and invoke the method when it has all the data it needs. Clean simple code that is easy to understand. The business logic is moved outside the servlet, which is what EJBs try to accomplish right?

Now someone convince me that I am wrong.

[ January 30, 2005: Message edited by: Kashif Riaz ]
[ January 30, 2005: Message edited by: Kashif Riaz ]
 
Damanjit Kaur
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not wrong to choose servlet for writing a simple task. Here is a link that compares both servlets and EJB. Though its quite old.

http://www.theserverside.com/articles/article.tss?l=Is-EJB-Appropriate
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kashif,

If you are just going to develop an application for bonus calculation then servlets would be more appropriate. But EJB's are really useful large distributed applications which requires features transactions, security etc.

Primary goal of EJB is to factor out common services needed by all distributed applications and let EJB container to support them. So that container vendors (who are experts in these services) can provide these services and application developer can concentrate on his own business logic.

Here are few advantages of EJB..

Container takes care of trasaction management (Have you heard of ACID properties that are needed for distributed databases..?)

Container takes care of security.. you easily implement entitlement ie specify which user Role can access which resource..

Provides database transparency.. i.e you can easily port your app. from database to another database..also you can easily handle table name or column name change without modifying your code.. (all just by changing config files) . But you should use CMP to get full advantages of this (Think about implications of porting to different database if you use JDBC within servlets)

Supports BUILD ONCE AND DEPLOY ANYWHERE (one step further to build once and run anywhere principle of JAVA)..so once the application is build you can deploy it in or port it to any container..

Everything is specified declaratively using xml (or any kind of) config files.. you can easily change the behaviour at run-time.

Container can support many optimizations like managing database connection pools..supporting pooling for your stateless session beans etc..

It also supports more advanced features like JMS, JCA (to integrate with any legacy system) , etc..

and more...

Check out book for more info..

Thanks,
Raja
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, those are excellent posts/links. Another reason why I value this forum so much.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic