Definition of EJB

5

Please forgive me for the generic subject, but I have been researching for a long time in a number of places and have not found anything that would satisfactorily explain what an EJB really is and what it does.

I am adept at using real examples at all for a better understanding and found few followers of this style in the tutorials I saw. Generally, they say that EJB is essentially a managed component that is created, controlled, and destroyed by the generator container of the J2EE that is running . That does not tell me anything or nothing. In fact I left the text with more doubts than before.

Looking for more, I have been able to understand that EJBs are autonomous 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 the CPF from a customer inquiring whether or not it is approved to make a purchase by check. Within this EJB there would be various business rules (account time, credit card instances, etc.), even queries to other EJBs, in order to "pull the chip" from the client and finally return a YES or NO to who summoned him. 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 of an EJB for a WebService? (the latter yes I know what it is and I have used it several times).

What is the relationship between JPA and EJB? I have done several applications using Hibernate, which is a JPA framework. Does that mean I've used EJB?

Thank you!

    
asked by anonymous 09.06.2014 / 22:52

3 answers

6

About EJB

Quickly defining EJB is: "The guy to take care of the business rule".

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

The features of EJB are:

  • Allow injection: they can be injected or have other components injected into them
  • Security control: just annotate your EJB it will be protected from unauthorized 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: respond only to one call and then can be used for other calls from any client. The server creates a pool of this face in case the demand increase / decrease the server can control the number of active instances. They can also be used as WebServices, just put a note and you're done. [=
  • Stateful: this guy functions as HttpSession as long as the reference stays alive. Usually this guy is placed inside the user's HttpSession, that way when the HttpSession dies it will also die. I honestly never used it and never saw much use for it.
  • Singleton: An EJB that will only have one instance for the entire project. Ideal for use in the DAO layer, for example.
  • MDB: Messaging service. Almost equal to a Stateles being the difference that it can not be called directly, but only when a message arrives through 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 had to pack your EJB into a JAR and attach the jar together with your WAR inside an EAR. If you did not pack everyone into an EAR your EJBs would have to be of the Remote type. A remote EJB uses RMI for connection which has performance impact. When an EJB was inside an EAR it could be used as a Local, it improves performance and other aspects.

WebService + EJB

WebService in few words is: expose a service to the web and nothing else. By using only the 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 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 is very easy to start (I mean newer versions, older ones are tricky).

Finishing

It is not a WebService, but may have services exposed as Webservices. They control the transaction and make it the ideal place to put the rules of the deal. It has messaging, timer and other services.

    
10.06.2014 / 19:20
1

Dude, I'm not going to 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", this is an EJB.

Why EJB?

"Transaction support, lifecycle management, dependency injection, security, remote and local access ... and more - all managed by the container". If you know what these concepts are, so far you must have understood what it is and why. If you do not know for sure what they are, or as an Application Server (JBoss, Websphere, Glassfish, etc etc) it works, I advise you to take a good look at basic topics about web application and / or enterprise application An EJB "MODULE" is a .jar file (which have POJOs, EJBs, etc etc) that may or may not be part of a larger (.ear) file and run within an Application Server.

Hugs

    
10.06.2014 / 19:32
1

What really made me understand was this site:

link

To get the idea more concrete, the article focuses on one aspect of EJBs:

  • Transactions

This includes transactions with the database (or with more than one database) and sending messages. And at this point, an EJB transactional method actually gets a lot simpler to write than trying to handle all the commit and rollback possibilities on the try / catch / finally basis.

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

    
30.01.2015 / 01:22