What is EJB, and why we should use it? Please explain in simple language. Thank you.
The EJB or Enterprise Java Beans are plain java classes (since version 3.0) with annotations that enable to you write the business logic of your applications and later deploy it (or install) on a Java Enterprise Edition Server.
You must consider use EJB if you wish to take advantage of the following services provided by the Java Enterprise Edition (Java EE) server:
- Clustering. You can deploy your EJB on a cluster environment (dependent of Java EE Application Server), this provides to you Fault Tolerance and High Availability.
- Concurrency without use Threads. All EJBs are instantiated through a pool of objects then your application gains on performance and without Thread complexity.
- Transactionality through JTA. All EJBs can benefit from Transactionality management for different resources, the most important Databases, using annotations is easy to delimit the frontier of every transaction and manage them.
- Connection Pool to Database. All ejb can access to connection pools defined into the Java EE Application Server, this connection pools provide an abstraction of the database complexity, by example you can use a XA Datasource that enables to you do Two Phased Commit to different databases.
- Security. All ejb can use JAAS for secure the applications. JAAS is configured into the Java EE Application Server and lets you to Authenticate and Authorize the methods of your EJB through different providers just with configuration (By example using Active Directory, LDAP or Database).
- Schedule service. All ejb can use the Timer Service that enables to you implement task for further execution or inclusive for repetitive execution.
There is other services and benefits but I think that these are the most importants. If you don't need these benefits my recommendation is that you don't use EJB (not all applications are Enterprise Applications).
All ejb can use JAAS for secure the applications
- This is not correct. EJBs do NOT use JAAS. Secured beans (via e.g. @RolesAllowed
) require the container to authenticate the user/caller but the spec does not describe via which mechanism this process should take place. Container implementations could base their code on JAAS or could use something else entirely. See these for some more details: arjan-tijms.blogspot.com/2014/02/… and raymondkng.sys-con.com/node/171477 –
Muimuir EJB beans are specifically designed to implement the business logic of your application. As such they provide services that are often needed when implementing such logic, such as transactions, injecting of the entity manager (used for JPA, the Java Persistence API) and pooling of beans.
See this for a more elaborate answer and even more references: What use are EJBs
In the most basic wording possible; "EJB beans make it a lot easier to work with a database via JPA".
Using JPA outside an EJB requires a lot of verbose and error prone code for obtaining an entity manager, starting a transaction and committing it or rolling it back.
There are a ton of other reasons to use EJB, but from experience I think that is the number one reason.
The Enterprise JavaBeans architecture or EJB for short is an architecture for the development and deployment of component-based robust, highly scalable business applications. These Applications are scalable, transactional, and multi-user secure. You can develop the application once and then deploy on any one of the Java EE 5 compliant application server. There are many application servers available, both free and commercial. You can choose the server for development and deployment to suit your requirement and budget.
Benefits of EJB
EJB simplifies the development of small and large enterprise applications. The EJB container provides system-level services to enterprise beans, the bean developer can just concentrate on developing logic to solve business problems.
see in wiki for more detail to getting stated
There is a newer Java EE tutorial here: http://download.oracle.com/javaee/6/tutorial/doc/. Well worth the time and effort.
And while Wikipedia may be the trite place for answers, this article is a pretty good overview of what EJBs are: http://en.wikipedia.org/wiki/Enterprise_JavaBean.
Transactions, Remoteness, etc. Maybe buy a good EJB book and read it, or do some googling. Start here Java EE6 EJB . It probably answers all your initial queries.
© 2022 - 2024 — McMap. All rights reserved.