Definition of EJB

Asked

Viewed 5,197 times

5

Forgive me for the general subject, but I have been researching for some time in various places and I have found nothing that would explain satisfactorily what an EJB is and what it is for.

I am adept at using real examples at all for a better understanding and found few adherents of this style in the tutorials I’ve seen. Usually they say that EJB is essentially a managed component that is created, controlled and destroyed by the J2EE container manager that is running. This tells me nothing or very little. I actually left the text with more doubts than before.

Looking further, I was able to understand that Ejbs are standalone modules that receive requests and return responses, as any method, with the advantage that they can be invoked from external applications. Is that correct?

A simple and practical example would be an EJB that receives a customer’s CPF query whether he is approved or not to make a check purchase. Within this EJB there would be several business rules (account time, occurrences in the credit protection agencies, etc.), even queries to other Ejbs, with the objective of "pulling the record" of the client and finally returning a YES or NO to the one who invoked it. This EJB could be used by any client: a mobile, web or desktop application.

Does anyone have anything else to add?

However, I have some doubts: what is the difference between an EJB and a Webservice? (the latter yes I know what it is and I’ve used it several times).

What is the relationship between JPA and EJB? I’ve done several applications using Hibernate, which is a JPA framework. This means I used EJB?

Thank you!

  • 2

    I will be happy if that question has answers that are not limited to "Ejbs have the characteristics A, B, C" but also include "Feature A serves for such a thing, B has such utility", finally, not only define but clarify why in an application with Ejbs they are considered indispensable, compared to an application without the same.

3 answers

8


About EJB

Quick Setting EJB is: "The guy to take care of the business rule".

It was created to control the transaction, messages, project security, etc.

The characteristics of the EJB are:

  • Allow injection: they can be injected or have other components injected into them
  • Security control: just note your EJB it will be protected from improper access
  • Transaction Control: You can control how the transaction should work, both automatically and programmatically

There are some types of EJB that we can quickly define:

  • Stateles: They only respond to one call and then can be used for other calls from any customer. The server creates a pool of this guy in case the demand increases/decreases the server can control the amount of active instances. They can also be used as Webservices, just put an annotation and ready. [=
  • Stateful: This guy works like Httpsession as long as the reference stays alive. Usually this guy is placed inside the user’s Httpsession, so when Httpsession dies he will also die. Honestly never used and never saw much use for it
  • Singleton: an EJB that will only have one instance for the entire project. Ideal to be used in the DAO layer, for example.
  • MDB: Message service. Almost equal to a Stateles being the difference that it cannot be called directly, but only when a message arrives by a messaging provider.

EJB is a specification, to be able to use it you need a server like Jboss, Glassfish, Tomee...

Before version 3.1 you needed to pack your EJB inside a JAR and attach the jar along with your WAR inside an EAR. If you didn’t pack everyone inside an EAR your Ejbs would have to be the Remote type. A remote EJB uses RMI for connection which has an impact on performance. When an EJB was inside an EAR it could be used as a Local, there improves performance and other aspects.

Webservice + EJB

Webservice in pouclas words is: expose a service to the web and nothing else. By using only Webservice you can receive a Request from anywhere in the world. An EJB can expose a method to the web as a Webservice and still have all its benefits. Without the EJB you will need to use another means to control transaction, security, timer, etc.

Since EJB is a JEE implementation it already comes on servers and are quite easy to start using (I mean the newer versions, the older ones are complicated).

Finalizing

He’s not a Webservice, but he may have exposed services like Webservices. They control the transaction and with it becomes the ideal place to put the rules of the business. It has messaging service, timer and others.

  • Good explanation, is becoming clearer, but I still do not understand what the advantage over a webservice. I found this link http://www.universidadejava.com.br/docs/criandoumwebservicecomejb30 that confused me even more. If they are different things, how can you create a web service with ejb?

  • 1

    I added a Webservice + EJB topic to answer your question

  • Could give an example of the implementation of an EJB?

1

Dude, I’m not gonna give you a giant answer, because there are already hundreds of sources:

Wikipedia

Oracle

My answer: "Any class annotated with @Stateless, @Stateful or @Singleton that runs within an Application server, "that’s an EJB.

Because EJB ?

"Transaction support, lifecycle management, dependency injection, security, remote and local access... and more. All this managed by the container". If you know what these concepts are, so far you have already understood what it is and why to use. If you don’t know for sure what they are, or how an Application Server (Jboss, Websphere, Glassfish, etc.) works, I advise you to take a good look at basic web application and/or Enterprise application topics A "MODULE" EJB is a file. jar (which have Pojos, Ejbs, etc.) which may or may not be part of a larger file (.Ear) and runs inside an Application Server.

Hugs

1

What made me really understand was this site:

http://entjavastuff.blogspot.com.br/2011/02/ejb-transaction-management-going-deeper.html

To make the idea more concrete, the article focuses on one aspect of the Ejbs:

  • Transactions

This includes transactions with the database (or with more than one database) and sending messages. And at that point, an EJB transactional method actually gets much simpler to write than trying to handle all the commit and rollback possibilities on the basis of Try/catch/Finally.

Of course there are other features in Ejbs (remote calls, asynchronous, timers, messages, object pooling, dependency injection, interceptors, security), and many materials place an exaggerated emphasis on remote calls, and hence confusion with web services...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.