Session Beans & Business Logic CS 486 Sagar Deshpande.

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento.
Advertisements

12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs.
Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009.
EJB Entity Beans. Entity Beans Data versus logic Used to represent an instance rather than a collection of data (depending on underlying storage) Represents.
Development and Deployment Roles Venugopal Pakanati.
Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.
Enterprise JavaBeans 2.0 Wu Lu (Cont’d). EJB2.0 Query Language EJB QL is a declarative language similar in many respects to SQL Is intended to be portable.
Copyright W. Howden1 Lecture 19: Intro to O/O Components.
Writing Enterprise Applications with J2EE (Third lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
How Does Remote Method Invocation Work? –Systems that use RMI for communication typically are divided into two categories: clients and servers. A server.
Chapter 10 EJB Concepts of EJB Three Components in Creating an EJB Starting/Stopping J2EE Server and Deployment Tool Installation and Configuration of.
1 The JNDI ENC and Injection Every EJB container that is deployed in an application server has its own personal internal registry called the Enterprise.
EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.
4-Tier Model Client Tier Web Tier Business Tier EIS Tier.
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.
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,
Enterprise Java Beans - (EJB)
Seminar: Enterprise JavaBeans. Agenda Agenda Java™ 2 Platform Java™ 2 Platform Java™ 2 Platform,Enterprise Edition(J2EE) Java™ 2 Platform,Enterprise Edition(J2EE)
Writing Enterprise Applications with J2EE (Second lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
Distributed Object-Oriented Programming (3) – Enterprise JavaBeans SNU iDB Lab. Taewhi Lee May 14th, 2007.
Entity Beans BMP Celsina Bignoli
Lecture 8 Advanced Topics in Enterprise JavaBeans.
Advanced Java Session 7 New York University School of Continuing and Professional Studies.
Container-Managed Persistence (CMP) Entity Beans Lesson 3A / Slide 1 of 42J2EE Server Components Objectives In this lesson, you will learn to: Identify.
Enterprise JavaBeans Umer Farooq CS6704: Design Patterns & Component FrameworksFebruary 25, 2002.
Distributed Systems 1 Master of Information System Management Distributed Systems Persistence.
Presented By Pradeep K Sahu. What will be the Contents of the Seminar ? What is EJB ? EJB Architecture Types of EJB Session Entity Why EJB ? Writing a.
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,
Session Beans Objectives Introduction to Session Beans Local and Remote Session Beans Stateless and Stateful Session Beans Session Bean Lifecycle Accessing.
Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.
A TUTORIAL TO USING EJBs by SHREERAM IYER 09/17/2001.
Professional Java Server Programming J2EE Edition Chapter 22 : Development and Deployment Roles Robert David Cowan CS486 April 9, 2001.
Enterprise JavaBeans Understanding EJB Components Version 0.1 Kamal Wickramanayake
1 Session Beans and Business Logic Presented By: Sethu Ram Kettimuthu.
Enterprise Java v041102EJB QL1 EJB Query Language Source: “Enterprise JavaBeans, 3rd Edition”, Richard Monson-Haefel.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
Li Tak Sing COMPS311F. RMI callbacks In previous example, only the client can initiate a communication with the server. The server can only response to.
 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture.
EJB Overview: Constraint Types and Enforcement in JBoss Elissa Newman Fluid Meeting 6/3/04.
Entity Beans & Persistence Chris Alexander CS 486 Spring 2001.
Session Beans Objectives Introduction to Session Beans Local and Remote Session Beans Stateless and Stateful Session Beans Session Bean Lifecycle Accessing.
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.
Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech.
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.
D Copyright © 2004, Oracle. All rights reserved. BMP Entity EJBs J2EE Connector Architecture.
Middleware Technology (J2EE/EJB) EJB Fundamentals.
Advanced Java Session 7 New York University School of Continuing and Professional Studies.
Enterprise JavaBeans™ Trademark of Sun Microsystems, Inc.
Java Object-Relational Layer Sharon Diskin GUS 3.0 Workshop June 18-21, 2002.
14 Copyright © 2004, Oracle. All rights reserved. Achieving State Management in the Business Tier.
17 Copyright © 2004, Oracle. All rights reserved. Integrating J2EE Components.
JAVA & J2EE UNIT – 8 ENTERPRISE JAVA BEAN - EJB 28-Jun-16 Prepared by: SANTHIYA.M & GANGA V. C. Department: Computer Science & Engineering Course code:
1 EJB 3.0: There’s Something Different About You Martin Jäkle, TSBU Middleware.
©NIIT Session Beans Lesson 1B/ Slide 1 of 37J2EE Server Components Objectives In this lesson, you will learn to: Describe the characteristics of session.
ATS Application Programming: Java Programming
Entity Bean Chuyên đề Lập trình Java & J2EE Chương 15
Chapter 14: Session EJBs and Distributed Transactions
Structure of Enterprise Java Beans
Objectives In this lesson, you will learn to:
Enterprise Java Beans Bina Ramamurthy 1/13/2019 B.Ramamurthy.
Entity Beans B.Ramamurthy 2/17/2019 BR.
Knowledge Byte In this section, you will learn about:
Understanding and Designing with EJB
Introduction to Session beans
Knowledge Byte In this section, you will learn about:
Objectives In this lesson, you will learn about:
Presentation transcript:

Session Beans & Business Logic CS 486 Sagar Deshpande

Requirements 1. The Engineering Dept needs to be able to define a product as series of steps that manufacturing facility will follow to build it 2. Should be able to accept from web as well as from phone operators who get orders from Sales personnel. Notify Manufacturing dept that a particular is placed and must be delivered on a particular due date. The identifier to this order is a unique sales division and a number provided by sales division. Duplicate order numbers exist between sales division. 3. Should be able to cancel orders on which manufacturing has not started, and prevent cancellation of order on which work has started 4. Manufacturing dept should be able to select a order based on due date and time it has to be in inventory before delivering 5. Management should be able retrieve a list of overdue orders 6. Manufacturing dept should notify shipping dept if a shipment is read, indicate carrier appropriate for the size and weight and loading dock where the product is sent.

Two session Beans ManageOrders --stateless Manufacture --stateful Two entity beans Product Order

Implementation ManageOrders, a stateless session bean used to manage orders, which also has a utility function to create sample products Package factory.manage_orders consisting of ManageOrders (remote interface) ManageOrdersHome (Home interface) ManageOrdersEJB DuplicateOrderException NoSuchOrderException NoSuchProductException OpenOrderView OverdueOrderView ProductCreationHelper// helper class

A stateful session bean Manufacture used to control manufacturing process factory.manufacture package consisting of Manufacture(remote interface) ManufactureHome(home interface) ManufactureEJB

Remote Interface for ManageOrders stateless session bean package factory.manage_orders; import javax.ejb.*; import java.util.Date; import java.rmi.RemoteException; import factory.order.OrderNotCancelableException; public interface ManageOrders extends EJBObject{ void placeOrder(int salesDivision, int orderNumber, String product, Date datedue) throws RemoteException, NoSuchProductException, DuplicateOrderException; void cancelOrder(int salesDivision, int orderNumber) throws RemoteException, orderNotCancelableException, NosuchOrderException; OverdueOrderView[] getOverdueOrders() throws RemoteException; OpenOrderView[] getSchedulableOrders() throws RemoteException void createProduct(String id, String name) throws RemoteException void createSampleProducts() throws RemoteException //utitlityfunction void addRoutingInstruction(String id, int sequence, String instruction) throws RemoteException

In the above interface, in a few methods salesDivision and ordernumber form a unique combination to identify any order. Two methods getOverdueorders() and getSchedulabeorders() return arrays of objects The other way to implement this to implement java.sql.Resultset interface or java.sql.RowSet Interface with info provided in rows and columns Here the assumption is result set is manageable,what if not ? Typical to arbitrary search criteria Depends on specifics of application Send a chunk, ask for refinement Get a marker from client, send data in chunks, this is possible because connections to database are pooled,hence no burden of creating connections.

Another issue to be addressed How to transfer data about a single entity from server to client getEachParameter() increases network traffic Use a structure, with serializable parameter and pass it. Problem with this Specific to particular type of client, introducing coupling between client and server Use java.sql.resultset What if data not tabled Use java.util.map-using a Key-value pair

Implementing exception classes package factory.manage_orders; Public class DuplicateOrderException extends Exception{ public DuplicateOrderException() } package factory.manage_orders; public class NoSuchProductException extends Exception{ public NoSuchProductException() }

Implementing OpenOrderView class package factory.manage_orders; import java.io.Serializable; import java.util.Date; public class OpenOrderView implements Serializable{ public final int salesDivision public final int orderNumber public final String product; public final Date datedue; public OpenOrderView(int salesDivision, int orderNumber,String product, Date datedue){ this.salesDivision=salesDivision; this.orderNumber=orderNumber; this.product=product; this.datedue=datedue; }

Implementing OverdueOrderView class package factory.manage_orders; import java.io.Serializable; import java.util.Date; public class OverdueOrderView implements Serializable{ public final int salesDivision public final int orderNumber public final String product; public final Date datedue; public OverdueOrderView(int salesDivision, int orderNumber,String product, String status, Date datedue){ this.salesDivision=salesDivision; this.orderNumber=orderNumber; this.product=product; this.status=status; this.datedue=datedue; }

Home interface for manage orders // typical to any stateless bean package factory.manage_orders; import javax.ejb.*; public interface ManageOrders extends EJBHome{ ManageOrders create()throws java.rmi.RemoteException, java.ejb.CreateException; }

Implementation of ManageOrdersEJB package factory.manage_orders; public class ManageOrdersEJB implements SessionBean{ void placeOrder(int salesDivision, int orderNumber, String productName, Date datedue) try{//find product ProductHome phome= get ProductHome(); Product p=phome.findByPrimaryKey(productName); //create order OrderHome ohome=getOrderHome(); Ohome.create(int salesDivision, int orderNumber, String productName, Date datedue); }catch (NamingException ne){throw new EJBException(ne) }catchFinderException fe){ If (fe instanceOf objectNotFoundException) throw NoSuchProductException(fe) }catch(CreateException ce){ if (OrderExists(int salesDivision, int orderNumber)){ throw new DuplicateOrderException(ce); else { throw new EJBException(ce)}

placeOrder() method makes use of both entity beans. It uses getProductHome() helper method to get a reference to ProductHome uses it to call getByPrimaryKey(). and getOrderHome() to get reference to OrderHome and Use the reference to call create() method of entitybean Order. Exceptions If finder exception is result of product not being found in database, NosuchProduct exception is thown, if it due to indeterminate cause, a general EJBException is thrown, similary with createException, here it uses helper function orderExists()

void cancelOrder(int salesDivision, int orderNumber) throws RemoteException, orderNotCancelableException, NosuchOrderException{ try{ OrderHome oh= getOrderHome(); OrderPk pk= new OrderPk(int salesDivision, int orderNumber); Order od=oh.findByPrimarykey(pk); od.cancel(); }catch( Exception e){} void createSampleProducts() try{ ProductHome ph=getProductHome(); ProductCreationHelper pch= new ProductCreationHelper(ph); // helper class pch.createAll();}catch (Exception e ){}

Helper class package factory.manage_orders; imports public class ProductCreationHelper{ ProductHome productHome; public ProductCreationHelper(ProductHome ph){this. productHome = productHome;} public void createALL(){ try{ createDesk(); createChair(); createLamp(); }catch(Exception e) throw new EJBException} public void createDesk()throws Exception{RoutingInstruction compress = new RoutingInstruction(5,"Compress the wood."); RoutingInstruction stain = new RoutingInstruction(10, "Stain the wood.");} productHome.create("CHAIR01", "Quality Plastic Chair", routings);}}

OpenOrderView[] getSchedulableOrders() throws RemoteException{} void createProduct(String id, String name) throws RemoteException{} void addRoutingInstruction(String id, int sequence, String instruction) throws RemoteException{} //helper methods private boolean orderExists(int salesDivision int orderNumber){ try{ OrderHome oh =getOrderHome(); OrderPK pk=new OrderPK(salesDivision, orderNumber); Order or=oh.findByPrimaryKey(pk); return true;} catch (Exception e){ return false} }

OverdueOrderView[] getOverdueOrders() { try { LinkedList overdueOrders = new LinkedList(); Date today = new Date(); long todayMillis = today.getTime(); long leadTimeMillis = getLeadTimeDays() *MILLIS_IN_DAYS; OrderHome orderHome = getOrderHome(); Collection uncompletedOrders = orderHome.findUncompletedOrders(); Iterator iterUncompletedOrders = uncompletedOrders.iterator(); while(iterUncompletedOrders.hasNext(){ find all orders which are to be produced or in production and put them in array of type OverdueOrderView }catch(exception e){throw EJBException}

Private int getLeadTimeDays() throws NamingException{ Integer leadTimeDays= (Integer).initial.lookup(“java:comp/env/lead_time”); Return leadTimeDays.intValue(); } private OrderHome getOrderHome()throws NamingException{ InitialContext I=new InitalContext(): OrderHome home=(orderHome)javax.rmi. PortableRemoteObject.narrow(initial.lookup(“java:comp/env/ejb/Order”) Return home;} //lifecycle methods public void ejbCreate(){} public void ejbActivate(){} public void ejbPassivate(){} public void ejbRemove(){} public void setSessionContext(SessionContext ct){}

Helper functions provide functionality to business methods These methods are private The method getLeadTime() retrieves Leadtime from beans JNDI environment This read-only information is specified in deployment descriptor, To change this information edit deployment descriptor and redeploy the bean, better than hardcoding in bean, in which case need to rebuild the bean and redeploy. What type of information Customization information like company’s name, IPaddress, SQL statement to direct database access. Environment entries can be following type String,Boolean,Byte,Short,Integer, Long,Float,Double. They can be specified tool provided by application server vendor Or edited by hand. The names specified will be available “java:comp/env” The method getOrderHome retrieves home interface of entity bean “Order” Bean developer declares logical names of beans that they reference in deployment descriptor

Deployment descriptor lead_time java.lang.Integer 4 ejb/Order Entity factory.order.OrderHome factory.order.Order Orders // optional

Manufacture stateful session bean remote interface package factory.manufacture; import statements... public interface Manufacture extends EJBObject{ OpenOrderView[] getOpenOrders() throws remoteException; void selectForManufacture( int salsdiv,int ordno) extends RemoteException,NosuchOrderException, BadStatusException; Boolean hasNextRouting()throws RemoteException, NoSuchOrderException; String getNextRouting()throws RemoteException, NoSuchOrderException; void ship(String carrier,int loaddock) throws RemoteException, NoSelectionrException; }

Unlike in manageOrder (stateless bean) there is an implied order in which the methods should be called in this interface, Select a product Work through routing(building it) Ship it failing to do will throw noSuchSelection exception

Home interface package factory.manage_orders; import javax.ejb.*; public interface ManufactureHome extends EJBHome{ Manufacture create( String manf_cellid)throws java.rmi.RemoteException, java.ejb.CreateException; }

Stateful session bean implementation package factory.manufacture; import statements public class ManufactureEJB implements session bean{ // properties public string manf_cellid; public List routing instructions; public int currentposition, last position; // framework & lifecycle methods public void ejbcreate(String manf_cellid){ this.manf_ceddid= manf_cellid; } public void ejbActivete(){} public void ejbpassivate(){} public void ejbRemove(){} public void setSessionContext(SessionContext ctx){}

//business methods public OpenOrderView[] getOpenOrders(){ ManageOrdersHome hmo=getManageOrdersHome(); ManageOrders mo= hmo.create(); return mo.getSchedulableOrders(); }catch(Exception e){throw new EJBException e}

Public void selectForManufacture(int SalesDivsion, int orderNumber){ OrderHome ho=getOrderHome(); OrderPK pk=new OrderPK(salesDivision, orderNumber); Order ord=ho.getByPrimaryKey(pk); String ordstat=ord.getStatus(); If(ordstat !=StatusString.OPEN)throw BadStatusException ; ord.beginManufacture() Product pr=ord.getProductOrdered(); RoutingInstructions[] productRouting=pr.getRoutingInstructions(); RoutingInstruction[] productRouting = product.getRoutingInstructions(); routingInstructions = Arrays.asList(productRouting); Currentposition=0; Lastposition= routingInstructions.size()-1; SelectedSalesDivision= SalesDivsion; SelectedOrderNumber= orderNumber; orderSelected=true;}catch(Exception e){}

The SelectforManufacture() verifies that order exists and is eligible for manufacture Set the status so other manufacturing units will realize order is being built. Retrives routing instruction and put them in variable as cache

public void ship(String carrier, int dock){ If (!orderSelected)throw new NoSelectionException(); Connection con=null; try{ con=getConnection(); PreparedStatement st= con.prepareStatement(getShipmentSQLString()); st.setInt(1,selSalesDiv); statement.setInt(2, selectedOrderNumber); statement.setString(3, carrier st.excuteUpdate(); st.close(); con.close(); orderSelected=false; Find the Order And call order.completeManufacture(); } Catch(Exception e){throw new EJBException(e)}

Private String getShipmentSQLString()thows exception InitialContext ic=new InitialContext(); String sql=(String)ic.lookup(“java:/comp/env/ shipmentsSQL”); Return sql} shipmentsSQL java.lang.string insert into shipments (dividion, oredrnumber, carrier, loadingdock,datecompleted, manufactured_by) values(?,?,?,?,?,?)

//helper functions private Connection getConnection()throws Exception{ Context in=new IntialContext(); DataSource dataSource=(DataSource)initial.lookup(“java:comp/env/jdbc/shipDB”); Return dataSource.getConnection} res-ref-name>jdbc/shipDB res-type>javax.sql.DATASource res-auth>Container

Questions ?