An EJB is a server-side component that executes specific business logic. EJBs run on a server and are invoked by local or remote clients. A standardized contract exists between an application server and its EJB components that enables a component to run within any application server. The application server provides clearly-defined services while the components adhere to a standard interface. EJBs are not GUI components; rather, they sit behind the GUIs and perform all the business logic, e.g. database CRUD operations. GUIs such as rich clients, web-based clients, and web services are the clients that can make connections to EJBs.The original JavaBeans specification is based on the java.beans package which is a standard package in the JDK. Components built in the JavaBeans specification are intraprocess components that stay in one address space and are typically used for Graphical User Interface (GUI) visual widgets such as buttons, tables, HTML viewers, etc. On the other hand, the EJB specification is based on the javax.ejb package, which is a standard extension package. EJB is one of several specifications grouped into Sun Microsystem’s Java 2 Enterprise Edition (J2EE) specification. Components built in the EJB specification are interprocess components that stay in multiple address spaces as distributed objects. These components are used as transactional business objects that are accessed as remote objects.
So we have:
EJB is a heavyweight framework. Deciding to implement EJB on your next project should only be done after weighting their pros and cons versus more lightweight solutions, such as Hibernate, JDO, or straight SQL. If you need a distributed solution, consider adding RMI-IIOP or CORBA options to the mix. Following are some things to consider when making the decision of whether or not to use EJB:
Some rules of thumb for when not to use EJB include:
To have an EJB deployment up and running, one needs more than an application server and components. There are six more parties that are involved:
There are three different types of beans in EJB: session, entity, and message-driven. Each has a specific purpose:
Session Beans are actions that can be executed by client code, such as making a reservation or charging a credit card. When a client wants to perform any of these actions, a session bean should be used. The session bean decides what data to modify. Typically, the session bean uses an entity bean to access or modify data, although it can use any technology that makes sense, for example, JDO, JDBC, or direct file access. Session beans represent business processes to be performed. They implement business logic, business rules, algorithms, and work flows. Session beans are relatively short-lived components. Their lifetime is equivalent to a client session. For example, if the client code invokes a session bean to perform credit card validation, the EJB container creates an instance of that session bean. After performing the business logic, if the client disconnects or terminates the session, the application server may destroy the session bean. The length of the client’s session generally determines how long a session bean is in use. The EJB container may destroy a session bean if its client times out. Typically, session beans neither survive application server crashes nor machine crashes. Session beans are not persistent, i.e. they are not saved to permanent storage such as a database. A session bean can perform database operations, but session beans themselves are not persistent.
Based on the possible types of conversations between a client and a bean, there are two types of session beans:
Entity beans deal with data. They typically represent nouns, such as a frequent flier account, customer, or payment. Plain old Java objects come into existence when they are created in a program. When the program terminates, the object is lost. But an entity bean stays around until it is deleted. A program can create an entity bean and then the program can be stopped and restarted–but the entity bean will continue to exist. After being restarted, the program can again find the entity bean it was working with and continue using it.
Plain old Java objects are used only by one program. An entity bean, on the other hand, can be used by any program on the network. Client programs just need to find the entity bean via JNDI in order to use it. Entity beans must have a unique primary key that is used to find the specific entity bean they want to manipulate. For example, an “employee” entity bean may use the employee’s social security number as its primary key. Methods of an entity bean run on a “server” machine. When a client program calls an entity bean’s method, the client program’s thread stops executing and control passes over to the server. When the method returns from the server, the local thread resumes execution.
Message-Driven Beans are enterprise beans that allow applications to process messages asynchronously. They act as a JMS message listener, which is similar to an event listener, except that it receives messages instead of events. The messages may be sent by any J2EE component–an application client, another enterprise bean, a Web component–or by a JMS application or system that does not use J2EE technology. The most visible difference between message-driven beans and session or entity beans is that clients do not access message-driven beans through interfaces. Unlike a session or entity bean, a message-driven bean has only a bean class.
In several respects, a message-driven bean resembles a stateless session bean:
The instance variables of a message-driven bean can contain some state across the handling of client messages–for example, a JMS API connection, an open database connection, or an object reference to an enterprise bean. Session beans and entity beans allow you to send JMS messages and to receive them synchronously, but not asynchronously. To avoid tying up server resources, you may prefer to not use blocking synchronous receives in a server-side component. To receive messages asynchronously, instead, use a message-driven bean.