Unformatted text preview:

CS6320 EJB L Grewe Enterprise JavaBeans Components Enterprise applications are modeled as a collection of components or enterprise beans in which each component is a reusable software unit that implements a specific part of the business functionality and has a well specified interface Used in Business Data layers Scalable container can create pool of servable EJBs Transactional EJBs can manage transactions Concurrency safe EJBs can have state protection from concurrent access EJBs can be tied with database entries A Java Architecture Java Technologies External Application EJB lives in a container Deployed on EJB servers and are hosted within containers EJB depend on the container for certain services that can be declaratively customized at deployment time Example J2EE server Types of EJBs There are three types of EJBs Session beans Model task or process oriented aspects of a business application Entity beans Model persistent enterprise data note Entity beans being replaced by Java Persistence API Message beans Model asynchronous consuming of messages EJB more An EJB is a Java object defining a part of an application Each EJB object implements a remote interface that it advertises The EJB system generates proxy objects stubs and skeletons between the client and the EJB Clients use the RMI IIOP protocol for communication Presence of the proxy objects is transparent to the clients N Tier Architecture Using EJB Presentation Layer Presentation Logic Tier Boundary EJB object EJB object EJB object EJB object Business Logic Layer Application Server JDBC Tier Boundary Database Data Layer 7 How a Client Accesses an EJB 1 2 3 Client locates the EJB container through the JNDI service or you know where it is Client finds a reference to the EJB s interface Now with the interface object the client invokes methods on the EJB interface which the container in turn delegates to the bean itself Acquiring a Bean 3 Create New EJB object Interface Object 5 Return EJB Object Reference Client 6 Invoke Remote Interface Business method 1 retrieve Home Object Reference 2 Return Home Reference 4 Create EJB Object EJB Object Enterprise Beans 7 Delegate request to object JNDI JNDI EJB Server Naming Service 9 The EJB RMI IIOP Common Object Request Broker Architecture CORBA Specifications governed by the Object Management Group OMG CORBA is language neutral using interfaces written in Interface Definition Language IDL CORBA uses Object Request Brokers ORBs for networking and data marshalling EJBs borrow heavily from this technology and from RMI RMI IIOP CORBA services incorporated into EJBs Naming Lifecycle management Security Persistence Distributed structures Session EJBs Encapsulate business logic best practice use when only one client has access to a bean instance and the state does not need to be persistent Think of a session bean as a grouping of related business operations BankTeller session bean does withdrawl checkaccount deposit LoanManager session bean does loan application loan approval Session EJBs 2 kinds Stateless created with no regard for the maintenance of any state between subsequent calls by a client Statefull maintain state for a particular client between alss before some amount of time has expiered EJB 3 0 life made easier MAJOR changes from 2 to make writing EJBs easier Ease of Use Have replaced 2 interfaces and 1 class with now only a business interface and a bean that implements this interface note for local access only need class Get rid of simplify deployment XML files Uses annotations rather than the complex deployment descriptors used in version 2 x EJB 3 The interface class This class defines the business logic as an interface package helloworld Interface of the HelloWorld example author You public interface HelloWorldInterface Hello world void helloWorld EJB 3 The bean class implementing the interface This bean will be a stateless session bean thus the class will be annotated with Stateless annotation package helloword import javax ejb Remote import javax ejb Stateless the interface must be a Business code for the HelloWorld interface remote interface to be author Florent Benoit available for remote clients This is done Stateless by using the Remote Remote HelloWorldInterface class annotation public class HelloWorldBean implements HelloWorldInterface Hello world implementation public void helloWorld System out println Hello world EJB 3 next step of hello world Compile HelloWorldInterface and HelloWorldBean Now you can JAR these files and deploy see your container for details EJB 3 HelloWorld Client Code package helloworld import javax naming Context import javax naming InitialContext Client of the helloworld bean author You public final class Client You need a client Could be a servlet or any kind of Java program to communicate with the EJB Here we make a standalone Console application Class named Client JNDI name of the bean private static final String JNDI NAME helloworld HelloWorldBean HelloWorldInterface class getName Remote hardcoding location of EJB that is in same source package as this client Utility class No public constructor private Client HelloWorld Client Code You need a client continued Could be a servlet Main method param args the arguments not required throws Exception if exception is found public static void main final String args throws Exception grab content of container Context initialContext new InitialContext look up EJB in this container and grab interface object HelloWorldInterface businessItf HelloWorldInterface initialContext lookup JNDI NAME System out println Calling helloWorld method call method of bean remotely through client s interface object businessItf helloWorld or any kind of Java program to communicate with the EJB Here we make a standalone Console application Class named Client Remote and Local Interfaces EJB can have remote or local interface Remote Used for accessing beans outside the JVM managing the bean class Restricts access to pass by value and parameters must be Serializable Local Used by clients that share a JVM with the bean Do not involve networking Use pass by reference semantics Allow association with other EJBs in the local JVM thus reduce communication overheads Do not need interface class can do only with bean class Remote an annotation The Remote line is an annotation It can occur on the previous line or you can use it like a modifier e g Remote public Annotations specify meta data In this case Remote says that the interface


View Full Document

Cal State East Bay CS 6320 - Enterprise JavaBeans

Loading Unlocking...
Login

Join to view Enterprise JavaBeans and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Enterprise JavaBeans and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?