“Controlling your application” (the business logic)

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

An architecture for webb applications, J2EE
1 Softsmith Open Learning - EJB 21-Nov Enterprise Java Beans Introduction –Application ServerApplication Server –Java 2 Enterprise EditionJava.
1 James Lynn Hewlett-Packard Middleware Division EJB Architecture Design Strategies and Performance Optimizations.
Overview of The Java Platform Solution for E-Business Applications : JSP, Servlet and EJB.
Enterprise Java Beans Welcome to the world of “Distributed System” Presented By: Sameer Nanda Date: 12/17/03.
Application Server Lecture Paulo Barroso Kris Carver Todd Kitterman Eric Silva.
Copyright W. Howden1 Lecture 19: Intro to O/O Components.
Structure of a web application1 Dr Jim Briggs. MVC Structure of a web application2.
Copyright  2001 Urbancode Software Development, Inc. All Rights Reserved. Optimizing J2EE Applications A Comparison of J2EE Design Idioms and their Performance.
Emmanuel Cecchet et al.  Performance Scalability of J2EE application servers.  Test effect of: ◦ Application Implementation Methods ◦ Container Design.
Chapter 10 EJB Concepts of EJB Three Components in Creating an EJB Starting/Stopping J2EE Server and Deployment Tool Installation and Configuration of.
Java Frameworks Indy Java Users Group January 29, 2003.
EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.
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.
Enterprise Java Beans CS-422. Application Servers In the late 1980s and though the mid 1990s a number of corporations (Broadvision, Netscape…) marketed.
1 Session Bean Chuyên đề Lập trình Java & J2EE Chương 14 Biên soạn: Th.S Nguyễn văn Lành.
J2EE Part 2: Enterprise JavaBeans CSCI 4300 Images and code samples from jGuru EJB tutorial,
Writing Enterprise Applications with J2EE (Second lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
EJB Entity Beans “Modeling your data”.
Session Beans Overview EJB container Remote Invocation Represent client's access to app Local / Remote speed / flexibility trade-off when to choose local.
Entity Beans BMP Celsina Bignoli
Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.
Lecture 8 Advanced Topics in Enterprise JavaBeans.
© D. Wong  Indexes  JDBC  JDBC in J2EE (Java 2 Enterprise Edition)
Entity Java Beans Jorg Janke Open Source ERP & CRM.
Container-Managed Persistence (CMP) Entity Beans Lesson 3A / Slide 1 of 42J2EE Server Components Objectives In this lesson, you will learn to: Identify.
Enterprise Java Bean Matt. 2 J2EE 3 J2EE Overview.
CD Database Server (CDDB) Zachary Carter Alan Savage Diana Abbas Kyle Dorman.
Enterprise JavaBeans. What is EJB? l An EJB is a specialized, non-visual JavaBean that runs on a server. l EJB technology supports application development.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
Enterprise Java Bean Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
J2EE Structure & Definitions Catie Welsh CSE 432
Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.
© jGuru.com Enterprise JavaBeans Fundamentals.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
Enterprise Java Beans. Model 1 J2EE – Web View Model/View/Controller Model View Control Data base Web Server Model One Architecture HTTP Request HTTP.
Java EE Patterns Dan Bugariu.  What is Java EE ?  What is a Pattern ?
EJB Overview: Constraint Types and Enforcement in JBoss Elissa Newman Fluid Meeting 6/3/04.
Entity Beans & Persistence Chris Alexander CS 486 Spring 2001.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Session Beans Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
Introduction to EJB. What is an EJB ?  An enterprise java bean is a server-side component that encapsulates the business logic of an application. By.
Java Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
Enterprise Java Beans N.V.RAJASEKHAR REDDY. Definition of EJB EJBs are the components that are the set of classes and interfaces deployed within a container.
13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.
Enterprise JavaBeans: Fundamentals. EJB Fundamentals(c)CDAC(Formerly NCST)2 Contents Introduction Technology Overview EJB Architecture EJB Specification.
Middleware Technology (J2EE/EJB) EJB Fundamentals.
EJB Enterprise Java Beans JAVA Enterprise Edition
EJB. Introduction Enterprise Java Beans is a specification for creating server- side scalable, transactional, multi-user secure enterprise-level applications.
©NIIT Session Beans Lesson 1B/ Slide 1 of 37J2EE Server Components Objectives In this lesson, you will learn to: Describe the characteristics of session.
Structure of a web application
Java Servlets By: Tejashri Udavant..
EJB (Enterprise Java Beans)
Introduction to J2EE Architecture
Enterprise Application Architecture
Design and Maintenance of Web Applications in J2EE
Distributed System Using Java 2 Enterprise Edition (J2EE)
Lecture 1: Multi-tier Architecture Overview
Luca Simone Software Engineering 2 a.a. 2001/2002
Objectives In this lesson, you will learn to:
EJB Types Alessio Bechini June 2002.
Understanding and Designing with EJB
Entity Beans B.Ramamurthy 2/17/2019 BR.
Understanding and Designing with EJB
Enterprise Java Beans Bina Ramamurthy 4/5/2019 B.Ramamurthy.
Enterprise Java Beans.
Knowledge Byte In this section, you will learn about:
Enterprise Java Beans.
Presentation transcript:

“Controlling your application” (the business logic) EJB Session Beans “Controlling your application” (the business logic)

Model 2 with J2EE EJB’s Model Two Architecture Control Model Web Container EJB Container View Control Model Web Server HTTP Request Servlet (request handler) Entity EJB Entity EJB Session EJB Session EJB Session Bean Java Bean Java Bean Java Bean <<creates>> <<forward>> HTTP Response The Model 2 architecture breaks up the Model-View-Controller pattern such that each part of the model can run in a different container on a different server. What is the purpose of breaking the pattern in this way? Adaptability Flexibility Scalability Thus far we have implemented the View and the Control. To implement the Model we will need to develop a seaparate tier of code to handle all of the dataaccess. One way to do this is to create your own custom code (A Data Access Object) Create your own custom code to access the database Access the database or flat file Manage persistence of data System dependent Database/file dependent JSP (view builder)

Advantages Automatic Transaction management Resource management Security Modularity Simple programming interface Advantages Remember that one of the advantages of Session Beans is that the Application Server stores the EJB in memory in the “bean pool”. This allows the bean to remain in memory and shared over large number of clients. Entity Java Beans work the same way but for data objects. Much simpler programming interface for client to access that data Greater portability, maintainability, reliability, code re-use Automatically handles transaction processing for you

Clients View of Session EJB Remote Interface Client Program Bean (Implementation) Local Interface Structure of Entity Java Beans Similar to Session Beans. There is a Client view and a Server View The client view – represents the client programmers interface view to the bean. These are the methods that you as a programmer have access to. This is the public interface to the bean. It acts as a façade to hide the complexity of the bean implementation from the developer. The bean class – represents the detailed implementation of the bean. All of the methods in the interface and the all other required methods by the container are implemented in this class Client Interface Implementation

Session EJB Developers View <<EJB Session Bean>> OrderControl <<Realization>> CustomerOrderEJB +startOrder() +addItem() +deleteItem() +checkout() +cancelOrder() +checkOrder() <<Remote Interface>> CustomerOrderRemote +startOrder() +addItem() +deleteItem() +checkout() +cancelOrder() +checkOrder() The actual implementation of the Home and Remote interface methods is done by creating the EJB Entity class. This class is some times called the “implementation” or “bean” class. The implementation class contains class variables that refer the Context object that EJB is running in and the different data elements represented in the Entity bean. It contains default constructor method and methods that implement the required methods of the home interface and the remote interface. There are also other methods that are used to manage the Entity bean such as ejbPostCreate(), ejbActivate(), ejbPassivate(), ejbLoad(), ejbStore(), setEntityContext(), and getEntityContext(). Client Interface Implementation

Remote vs. Local Entity Beans Please Use Whenever Possible!!! Remote Session EJB Anywhere on the network Pass by value Local EJB are faster but not as flexible, scalable and adaptable On same server as Session Beans No network traffic Pass by reference

Stateful vs. Stateless Transactions Please Use Whenever Possible!!! A stateless transactions is One call to server Does not the save transaction state data No memory is allocated in the pool for each session with a client EJB is never passivated or activated Stateful transaction Used for multi-step transactions Saves state data for each client session Memory is allocated in pool for each client session EJB is passivated and activated

Stateless Session Bean LifeCycle Does not exist timeout, @PreDestroy, Class.newInstance(), @PostConstruct Ready in pool Business method

Stateful EJB Session Bean Lifecycle System exception Does not exist Timeout, @PreDestroy Class.newInstance(), @PostConstruct() Timeout, @PreDestroy @PrePassivate() Ready in pool Passive business methods @PostActivate ()

Guidelines for use Coarse-Grained better than Finely-Grained Use stateless whenever possible Use the remote interface for flexibility Guidelines for use Coarse-Grained Business Objects vs. Finely-Grained Business Objects There is some extra overhead associated with using entity beans (locating the object and accessing it) and there over use can effect performance. Entity beans should seldom be used to represent individual table is a database but a broader end users logical view of the data. For example, A Customer Order and all the information contained in the customer order (order information, and line items purchased on order and totals). You would not create an entity bean for each database table involved in the order. Probably not best for doing simple data access like performing simple queries. Entity Java Beans are more complex to create than the data access code to perform a query. Recommendation – Use for logical data objects that are involved in transactions (update, delete or inserting). Do not use for fine grained simple data access, unless, there is some overriding design requirement that would deem only the use of EJBs. For example, the code must be designed to support multiple types of databases such that the databases are interchangeable.

Locating an EJB Must use a directory service and DNS to locate network objects JNDI (Java Naming Directory Interface) Programming interface to the directory services to locate any object in a network (files, EJBs, web services, etc.) EJB’s can be remote anywhere on the network Must use a directory service and DNS to locate network objects JNDI (Java Naming Directory Interface) - java programming interface to DNS It makes easy to access and use the directory services to locate any object in a network files, EJBs, web services, etc.)

Directory Service names Most use X.500 naming standard c (country name) o (organizationName) ou (organizationUnitName) l (localityName) cn (commonName) dc (domainComponent) uid (userid) Each type of directory service has it’s own naming syntax LDAP example cn=Martin Bond, ou=Authors, o=SAMS, c=us Microsoft Active Directory Service cn=Martin Bond/ou=Authors/o=SAMS/c=us

JNDI names JNDI names are not specific to directory services JNDI maps universal name to specific directory service syntax Typical JNDI name “SAMS/authors/Martin Bond”

Outline for using remote EJBs Get initial JNDI naming context Use JNDI to lookup and find EJB Call business function

Defining the JNDI name The JNDI name is best defined in the Session EJB “Bean” class as a constant value. The JNDI name must follow a specific format for the EJB 3.0 architectures. The format is “BeanClassName/remote” where BeanClassName is the name of the class.

Calling business methods try { InitialContext initialContext = new InitialContext(); // get jndi context String jndiName = BusinessRulesBean.RemoteJNDIName; ; // get jndi name // lookup and get remote interface for session bean BusinessRulesRemote businessRulesRemote = (BusinessRulesRemote) initialContext.lookup(jndiName ); // call any business methods defined in the interface Person person = businessRulesRemote.login(username, password); … } catch (NamingException ne) { throw new MyAppException( “Session EJB not found, jndiname=” + jndiName)); catch (Exception e) { throw new MyAppException(ee.getMessage());

Calling business methods try { InitialContext initialContext = new InitialContext(); // get jndi context String jndiName = BusinessRulesBean.RemoteJNDIName; ; // get jndi name // lookup and get remote interface for session bean BusinessRulesRemote businessRulesRemote = (BusinessRulesRemote) initialContext.lookup(jndiName ); // call any business methods defined in the interface Person person = businessRulesRemote.login(username, password); … } catch (NamingException ne) { throw new MyAppException( “Session EJB not found, jndiname=” + jndiName)); catch (Exception e) { throw new MyAppException(ee.getMessage());

Calling business methods try { InitialContext initialContext = new InitialContext(); // get jndi context String jndiName = BusinessRulesBean.RemoteJNDIName; ; // get jndi name // lookup and get remote interface for session bean BusinessRulesRemote businessRulesRemote = (BusinessRulesRemote) initialContext.lookup(jndiName ); // call any business methods defined in the interface Person person = businessRulesRemote.login(username, password); … } catch (NamingException ne) { throw new MyAppException( “Session EJB not found, jndiname=” + jndiName)); catch (Exception e) { throw new MyAppException(ee.getMessage());

Calling business methods try { InitialContext initialContext = new InitialContext(); // get jndi context String jndiName = BusinessRulesBean.RemoteJNDIName; ; // get jndi name // lookup and get remote interface for session bean BusinessRulesRemote businessRulesRemote = (BusinessRulesRemote) initialContext.lookup(jndiName ); // call any business methods defined in the interface Person person = businessRulesRemote.login(username, password); … } catch (NamingException ne) { throw new MyAppException( “Session EJB not found, jndiname=” + jndiName)); catch (Exception e) { throw new MyAppException(ee.getMessage());

Calling business methods try { InitialContext initialContext = new InitialContext(); // get jndi context String jndiName = BusinessRulesBean.RemoteJNDIName; ; // get jndi name // lookup and get remote interface for session bean BusinessRulesRemote businessRulesRemote = (BusinessRulesRemote) initialContext.lookup(jndiName ); // call any business methods defined in the interface Person person = businessRulesRemote.login(username, password); … } catch (NamingException ne) { throw new MyAppException( “Session EJB not found, jndiname=” + jndiName)); catch (Exception e) { throw new MyAppException(e.getMessage());

Insert object into Datastore public void addBranch() { // create a branch java bean Branch branch = new Branch(); branch.setName(“Bank of Nauvoo”); branch.setPhone(“203-356-1426”); // inserts a branch into the database entityManager.persist(branch); }

Update Object in Datastore public void renameBranch(String branchid, String newName) { // get branch by its Primary Key from datastore Branch branch = (Branch) entityManager.find(Branch.class, branchid); // update the branch branch.setBranchname(newName); entityManager.merge(branch); }

Delete Object from Datastore public void deleteBranch(String branchid) { // get branch by its Primary Key from datastore Branch branch = (Branch) entityManager.find(Branch.class, branchid); // Delete the branch entityManager.remove(branch); }

Query Object/s from Datastore public Collection<Branch> getBranches(String name){ // Define query prepared statement String ejbql = "SELECT b FROM Branch b WHERE b.branchname LIKE :branchname”; // Create query object Query query = entityManager.createQuery(ejbql); // Substitute value to search for in prepared statement query.setParameter("branchname", searchValue); // Execute query to get list of branches List<Branch>branches = query.getResultList(); return branches; }

Entity Bean query language (ejb-ql) select_clause from_clause [where_clause] SELECT j FROM Job AS j WHERE j.jobType = ‘web development’ From Examples: SELECT j.* FROM Job AS j WHERE j.jobType = ‘web development’; SQL equivalent:

Entity Bean query language (ejb-ql) select_clause from_clause [where_clause] SELECT s FROM Job AS j, INNER JOIN j.Skills AS s From Examples: SELECT s.* FROM Job AS j INNER JOIN JobSkill AS s ON j.FK_skillID = s.skillID SQL equivalent:

Entity Bean query language (ejb-ql) Inner Joins of Entities Where Examples: SELECT OBJECT o FROM Customer AS c, INNER JOIN c.orders AS o, INNER JOIN o.items AS I WHERE c.lastName = ‘FLINTSTONE” SELECT C.*, O.*, I.*   FROM Customer C INNER JOIN Orders O INNER JOIN Items I ON C.customerId = O.FK_CustomerId ON O.FK_ItemId = I.ItemId WHERE C.lastName = ‘FLINSTONE’; SQL equivalent: