Presentation is loading. Please wait.

Presentation is loading. Please wait.

Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container.

Similar presentations


Presentation on theme: "Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container."— Presentation transcript:

1 Brad Rippe Fullerton College

2 What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container Example uses Jboss 2.4 with Tomcat 3.2.3 A good editor

3 What is a EJB? JavaBean? Java Classes? GUI? Are EJBs part of the J2EE? What is J2EE?

4 What is a EJB? Enterprise JavaBeans (EJBs) are distributed object that are hosted in Enterprise JavaBean Containers and provide remote services for clients distributed throughout the network. This components encapsulate business logic

5 Why EJB? (Distributed Computing) Enterprise Applications Development Costs Deployment Costs Maintenance Costs Service More Clients More Bang for the Buck!

6 J2EE Architecture This is an illustration of the architecture set forth by Sun. See http://java.sun.com/blueprints/

7 Containers and Services EJB Containers provide additional services for the (EJBs). Life-Cycle Management, Transaction Management, Security, Persistence, Resource Management.

8 EJB Container Similar to the Web Container EJBs require a container to function The contain isolates the EJB from direct access from client application. Manages remote access to EJBs. Provided by an Application Server. JBoss, WebLogic, JRun, Borland App Server, etc.

9 EJB Advantages Productivity Container services are provided automatically. Developer can focus on the business logic without Infrastructure Container management is inherently robust. Supports scalability Portability EJB Spec provides a well-defined contract for EJB Containers

10 How does the EJB get the services? EJBs can access container services through one of three ways Callback methods The EJBContext interface Java Naming and Directory Interface

11 CallBack Methods Each EJB is required to implement a subtype of EnterpriseBean interface which defines callback methods. Each callback method provides a way for the container to notify the EJB about an event in the bean’s lifecycle, i.e. removing a bean from memory. The callback methods give the EJB a chance to do some internal housework before or after an event occurs. These are the bean’s event handlers.

12 EJBContext Every EJB obtains an EJBContext object which is a reference directly to the EJB Container. The EJBContext interface provides methods for interacting with the container so that the EJB can request information about its environment.

13 Java Naming and Directory Interface (JNDI) JNDI is the standard extension to the Java platform for accessing naming systems like LDAP, NetWare, NDS, file systems. Every EJB automatically has access to a special naming system called the Environmental Naming Context (ENC). The ENC is managed by the container. It allows an EJB to access resources like JDBC connection, other EJBs, and its own properties.

14 Roles of Application Builders Bean Provider an application developer and, often, a domain expert – builds reusable components without focusing on the framework. Assembler Combines finished EJBs into modules and combines those and other J2EE building blocks into applications, making container neutral- decisions. Deployer Deploys J2EE applications in a specific environment, and make container-specific decisions.

15 Two Main Types of Beans Session Beans Stateless calculating sales tax, or processing and order Stateful Common Shopping cart component Entity Beans CMP (Container-Managed Persistence) BMP (Bean-Managed Persistence) Message-Driven Beans (Introduced in 2.0) This is a JMS bean. Designed for sending and receiving JMS messages.

16 Creating EJBs All ejbs implement a subtype of EnterpriseBean. Either SessionBean, EntityBean, or MessageDrivenBean. Each of the subInterfaces declares callback methods for the container. To create an EJB a developer provides: A home interface defines the life-cycle methods of the bean A remote interface defines the business methods of the bean class. A bean class business logic, the meat is here

17 Conceptual Model Bean Class Home Remote Client Home Remote Application Server EJB Container

18 Home Interface Extends javax.ejb.EJBHome This interface declares create and find methods. EJB Container implements this interface Clients use JNDI to locate the vendors home class.

19 Remote Interface Extends javax.ejb.EJBObject Client view and get access to the EJB through the bean’s remote interface. Methods a client can call are declared here. The actually implementation of those business methods is located in the bean class. Gives a client a handle to the EJB

20 How the do interfaces work? The container creates a class that implements the Home interface and makes it available to JNDI. The container creates a class that implements the Remote interface which acts like a middleman between the bean and the client.

21 Bean Class Implements javax.ejb.EntityBean if it is an EntityBean Implements javax.ejb.SessionBean if it is an SessionBean This class defines the methods declared in the Home and Remote interfaces. Defines finder, create and business methods of the EJB.

22 Enity Beans versus Session Beans Entity Beans Persistent Part of permanent storage Should communicate with Session Beans Should not communicate with clients Read and write access to the data store Session Beans Not persistent Does not survive server crash Can access the database for queries Communicates with client via interfaces

23 Session Bean  Session Beans should be used for short requests that can be satisfied with one method.  Session Beans require low resource costs  Easy for the container to manage  Promotes fast response back to the client  Can be stateful or stateless  Client receive only one stateful bean for service  Clients share stateless beans  Two type of transaction modes  CMT – Container Managed Transaction  BMT – Bean Managed Transaction

24 Entity Bean Has a direct relation to database row. Database data types are converted into java data types and encapsulated into the Entity Bean. Entity beans must have a defined primary key data type or compound object as its primary key. Require more overhead to maintain state between the database and the EJB object. Entity bean have persistent data. Persistence can be one of two persistence modes: CMP – Container-Managed Persistence BMP – Bean-Managed Persistence

25 Scenario 1 – Session Bean Example Client Session Bean Data Store Session Bean

26 Scenario 2 – Entity Bean Example Client Session Bean Data Store Session Bean Entity Bean

27 Building a Session Bean Example uses one Session Bean, two different clients to access the bean’s business methods Business method – calculateStockPrice( String ticker, int numShares ) Calculates the price of stock for four different companies.

28 Where to begin? First, download Jboss – http://www.jboss.org or some other app server.http://www.jboss.org App Server must support EJBs. Version integrated with Tomcat is preferred! Second, unzip the archive into the directory where it will be located permanently on your server. I chose a directory like “e:\appServer\” Third, on to the code…

29 StockBalanceBean Example Requires a home interface Requires a remote interface Requires the bean class Requires the Deployment Descriptor Some packaging? A client or two Deploy

30 StockBalanceBean Home Interface package edu.fullcoll.exampleEJB; import java.rmi.RemoteException; public interface StockBalanceHome extends EJBHome { StockBalance create() throws CreateException, EJBException, RemoteException; }

31 StockBalanceHome Home Interface Provides lifecycle methods for creating, destorying and locating EJBs Separate from the remote interface because the home interface is not associated with one instance of an EJB Home interface extend javax.ejb.EJBHome interface

32 StockBalance Remote Interface package edu.fullcoll.exampleEJB; import java.rmi.RemoteException; public interface StockBalance extends EJBObject { double calculateStockPrice(String ticker, int numShares) throws RemoteException, EJBException; }

33 StockBalance Remote Interface Declares the business methods available in EJB class. The this is the clients way of communicating with the EJB. The EJB container creates an object that implements this remote interface and returns it to the client. Can be associated with one instance of an EJB.

34 StockBalanceBean – Bean Class public class StockBalanceBean implements SessionBean { public double calculateStockPrice(String ticker, int numShares){ if( ticker != null) { ticker = ticker.toUpperCase(); if(ticker.equals( "AAPL" )) { return numShares * 20.42; } else if(ticker.equals( "MSFT" )) { return numShares * 64.84; } else if(ticker.equals( "YHOO" )) { return numShares * 16.70; } else if(ticker.equals( "SUNW" )) { return numShares * 13.84; } return 0.0; }

35 StockBalanceBean Implements SessionBean interface public void ejbActivate() { } public void ejbPassivate() { } public void setSessionContext(SessionContext ctx) {} public void ejbRemove() {} public void ejbCreate() {}

36 EJBs and RMI The remote and home interfaces are types of Java RMI remote interfaces. This means that the EJB, even though instantiated in the EJB container, can have its methods invoked as a result of a request from an outside application. The RMI stub and skeleton hide the communication specifics from the client.

37 EJBs and RMI Stub Skeleton EJB Object EJB Object ClientNetwork App Server

38 Final Steps Home, Remote and Bean class are created. Create a deployment descriptor Package the EJBs Deploy the beans to the App Server

39 ejb-jar.xml Must be stored in the jar’s META-INF directory. XML document. Describes the EJB setup, transaction mode, JNDI name, security, and persistence. Can be created by hand, not recommended. J2EE deploytool Another GUI tool to generate the xml… Recommended…

40 ejb-jar.xml Let’s take a look…

41 Packaging the EJBs Again you can use a tool like Together’s Control Center or the deploytool. Or Create a jar file with your ejb classes in it and the deployment descriptor in the META-INF directory. This file will have a.jar extension. Name the jar an arbitrary name, StockBeanEJBs.jar

42 Creating a client - local  Let’s take a look…

43 Creating a client - Remote Let’s take a look…

44 How does a client lookup a bean? A client needs to know two things: The JNDI name of the Bean The vendor-specific syntax for getting the InitialContext.

45 Deployment To deploy the bean in jboss, simply copy the EJB jar to the “deploy” directory. 2.4 handles hot deploy, so if you update your EJBs you can copy the new jar into the “deploy” directory and the EJBs will be updated…

46 Info about client compilation Your client code must be compiled with the following jars. ejb.jar – standard javax.ejb.* jaas.jar – Java security classes jbosssx-client.jar – JBossSX security classes jboss-client.jar – EJB container proxy and stub classes jnp-client.jar – jboss JNDI provider client classes

47 Entity Beans Provides object representation of data. One entity bean can represent data for multiple clients. Represents a row in the database. Model business objects, nouns, Person, Seat, Room, etc. Container handles persistence, transactions, and access control

48 Two types of Entity Beans Bean-Managed Persistence (BMP) Develop provides all of the code for managing persistence between the object and the database. The container will notify the bean when its necessary to update or read from the database. Container-Managed Persistence (CMP) The EJB Container handles the relationship between the bean and the database. Bean developer can focus on the data and the business process.

49 Using Entity Beans CMP Recommended for beginners Handles simple relationships with the database. (one row) BMP Used for more complex relationships. Beans that represent multiple rows or table joins. This code is implemented by the developer

50 Entity Bean Requirements Home Interface Remote Interface Bean class – implements EntityBean Primary Key – can be a java class or primitive type. Points to a unique record in the database. All Entity beans must have a primary key that is serializable.

51 Creation The entity bean’s home interface declares a method create(). The bean class must define methods ejbCreate() and ejbPostCreate(). ejbCreate() and ejbPostCreate() must have the same parameters as create() from the home interface. create() Inserts a row into the database. ejbPostCreate()- provides a method for accessing the EJB’s remote method (this).

52 Callback Methods setEntityContext() unsetEntityContext() ejbLoad() ejbStore() ejbActivate() ejbPassivate() ejbRemove() CMP - the container decides when to call these methods and their implementation. BMP - the container decides when to call these methods and the developer provide the implementation.

53 Find Methods Find methods in the Home interface are used to query the EJB server for specific entity beans. CMP the find methods are implemented by the container. There isn’t any code in the bean class. Clients can call find methods to obtain a reference to a particular bean’s remote interface. Can return a single reference or an Enumeration or Collection of references. FCStudentBean brad = (FCStudentBean) home.findStudent(00001149);

54 Entity Beans Example - Home package edu.fullcoll.schedule; import java.rmi.RemoteException; Import javax.ejb.EJBHome; public interface FCStudentHome extends EJBHome { public FCStudentRemote create(int pidm) throws CreateException, RemoteException; public FCStudentRemote findByPrimaryKey(int pk) throws FinderException, RemoteException; }

55 Entity Beans Example - Remote package edu.fullcoll.schedule; import java.rmi.RemoteException; Import javax.ejb.EJBObject; public interface FCStudentRemote extends EJBObject { public String getName() throws RemoteException; public void setName(String n) throws RemoteException; }

56 Entity Beans Example – Bean Class public class FCStudent implements EntityBeans { public int pidm; public String name; public int ejbCreate(int pidm) { this.pidm = pidm; return null; } public String getName() { return name; } public void setName(String n) { name = n; } // all callback methods must be defined, but blank for CMP }

57 J2EE Comprised of many different technologies JSP/Servlets JDBC JNDI JTA JMS, jetc, jetc

58 Summary Similar to other technologies, EJBs have there place in software development. EJBs are not a solution for all development problems. They are meant for transactional, secure business applications, reservation systems, student registration, online purchasing. They are highly scalable components meant for use in complex, mission-critical applications.

59 Resources Jboss – http://www.jboss.orghttp://www.jboss.org Tomcat – http://jakarta.apache.org/tomcathttp://jakarta.apache.org/tomcat J2EE Web Site – http://java.sun.com/j2eehttp://java.sun.com/j2ee J2EE Tutorial – http://java.sun.com/j2ee/tutorial http://java.sun.com/j2ee/tutorial EJB Spec – http://java.sun.com/products/ejbhttp://java.sun.com/products/ejb Lecture available at http://staffwww.fullcoll.edu/brippe/cis226 http://staffwww.fullcoll.edu/brippe/cis226

60 Resources J2EE Developer’s Guide – http://java.sun.com/j2ee/j2sdkee/techd ocs/guides/ejb/html/DevGuideTOC.html http://java.sun.com/j2ee/j2sdkee/techd ocs/guides/ejb/html/DevGuideTOC.html J2EE Tutorial – http://java.sun.com/j2ee/tutorial/1_3- fcs/index.html http://java.sun.com/j2ee/tutorial/1_3- fcs/index.html Server Side Programming – http://www.theserverside.com http://www.theserverside.com

61 The End! Thanks for you time!


Download ppt "Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container."

Similar presentations


Ads by Google