610 likes | 737 Views
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 Example uses Jboss 2.4 with Tomcat 3.2.3 A good editor. What is a EJB?. JavaBean? Java Classes? GUI? Are EJBs part of the J2EE?
E N D
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 • Example uses Jboss 2.4 with Tomcat 3.2.3 • A good editor
What is a EJB? • JavaBean? • Java Classes? • GUI? • Are EJBs part of the J2EE? • What is J2EE?
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
Why EJB? (Distributed Computing) • Enterprise Applications • Development Costs • Deployment Costs • Maintenance Costs • Service More Clients • More Bang for the Buck!
J2EE Architecture This is an illustration of the architecture set forth by Sun. See http://java.sun.com/blueprints/
Containers and Services • EJB Containers provide additional services for the (EJBs). • Life-Cycle Management, Transaction Management, Security, Persistence, Resource Management.
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.
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
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
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.
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.
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.
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.
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.
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
Conceptual Model Application Server EJB Container Home Bean Class Client Remote Home Bean Class Remote
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.
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
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.
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.
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
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
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
Scenario 1 – Session Bean Example Data Store Session Bean Client Session Bean
Scenario 2 – Entity Bean Example Session Bean Client Data Store Entity Bean Entity Bean Session Bean
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.
Where to begin? • First, download Jboss – http://www.jboss.org or some other app server. • 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…
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
StockBalanceBean Home Interface package edu.fullcoll.exampleEJB; import java.rmi.RemoteException; public interface StockBalanceHome extends EJBHome { StockBalance create() throws CreateException, EJBException, RemoteException; }
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
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; }
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.
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; } }
StockBalanceBean • Implements SessionBean interface public void ejbActivate() { } public void ejbPassivate() { } public void setSessionContext(SessionContext ctx) {} public void ejbRemove() {} public void ejbCreate() {}
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.
EJBs and RMI App Server Client Network Skeleton Stub EJB Object
Final Steps • Home, Remote and Bean class are created. • Create a deployment descriptor • Package the EJBs • Deploy the beans to the App Server
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…
ejb-jar.xml • Let’s take a look…
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
Creating a client - local • Let’s take a look…
Creating a client - Remote • Let’s take a look…
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.
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…
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
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
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.
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
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.