Introduction to Session beans

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.
Development and Deployment Roles Venugopal Pakanati.
Enterprise Java Beans Adam, Engels, Josh, Marc, Tim.
Application Server Lecture Paulo Barroso Kris Carver Todd Kitterman Eric Silva.
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)
EJB Fundamentals Celsina Bignoli
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.
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)
Advanced Java Session 7 New York University School of Continuing and Professional Studies.
Entity Java Beans Jorg Janke Open Source ERP & CRM.
Enterprise JavaBeans Umer Farooq CS6704: Design Patterns & Component FrameworksFebruary 25, 2002.
1 Stateful Session Beans Stateless Session Beans Michael Brockway Sajjad Shami Northumbria University School of Computing, Engineering & Information Sciences.
Enterprise Java Bean Matt. 2 J2EE 3 J2EE Overview.
The Triad of Beans I Oleh: Dini Addiati ( ) Fahrurrozi Rahman ( Y) Irfan Hilmy ( ) Salman Azis A ( ) Aziiz Surahman.
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.
Enterprise JavaBeans. Lesson 1: Introduction to Server-Side Component Software.
Introduction to J2EE Architecture Portions by Kunal Mehta.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
Message-Driven Beans and EJB Security Lesson 4B / Slide 1 of 37 J2EE Server Components Objectives In this lesson, you will learn about: Identify features.
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 入门 Introduction What is J2EE ?
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.
Enterprise JavaBeans Understanding EJB Components Version 0.1 Kamal Wickramanayake
1 Session Beans and Business Logic Presented By: Sethu Ram Kettimuthu.
© jGuru.com Enterprise JavaBeans Fundamentals.
EJB Overview: Constraint Types and Enforcement in JBoss Elissa Newman Fluid Meeting 6/3/04.
Enterprise JavaBeans Session Beans. Session beans are a type of Enterprise JavaBean component designed to implement business logic responsible for managing.
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.
Session Beans Objectives Introduction to Session Beans Local and Remote Session Beans Stateless and Stateful Session Beans Session Bean Lifecycle Accessing.
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.
Copyright © 2002 ProsoftTraining. All rights reserved. Enterprise JavaBeans.
13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.
©NIIT Introducing Enterprise JavaBeans (EJB) Lesson 1A / Slide 1 of 43J2EE Server Components Objectives In this lesson, you will learn about: The features.
Enterprise JavaBeans: Fundamentals. EJB Fundamentals(c)CDAC(Formerly NCST)2 Contents Introduction Technology Overview EJB Architecture EJB Specification.
Enterprise JavaBeans. Lesson 1: Introduction to Server-Side Component Software.
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.
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.
14 Copyright © 2004, Oracle. All rights reserved. Achieving State Management in the Business Tier.
DEVELOPING ENTERPRISE APPLICATIONS USING EJB
©NIIT Session Beans Lesson 1B/ Slide 1 of 37J2EE Server Components Objectives In this lesson, you will learn to: Describe the characteristics of session.
Enterprise Java Beans. Contents  Understanding EJBs  Practice Section.
J2EE Lecture 10: EJB 3.0 Overview
EJB Architecture and Design
Introduction to J2EE Architecture
Structure of Enterprise Java Beans
J2EE Application Development
Understanding and Designing with EJB
Luca Simone Software Engineering 2 a.a. 2001/2002
Objectives In this lesson, you will learn to:
Understanding and Designing with EJB
Enterprise Java Beans Bina Ramamurthy 1/13/2019 B.Ramamurthy.
Component-based Applications
Understanding and Designing with EJB
Enterprise Java Beans Bina Ramamurthy 4/5/2019 B.Ramamurthy.
Enterprise Java Beans: an introduction
Enterprise Java Beans.
Knowledge Byte In this section, you will learn about:
Enterprise Java Beans.
Presentation transcript:

Introduction to Session beans EJB

Architecture

Stateless session Beans A stateless session bean does not maintain a conversational state for a particular client. When a client invokes the method of a stateless bean, the bean's instance variables may contain a state, but only for the duration of the invocation. When the method is finished, the state is no longer retained.

Stateless vs. stateful session Beans All instances of a stateless bean are equivalent, allowing the EJB container to assign an instance to any client. => Stateless session beans can support multiple clients, and offer better scalability for applications that require large numbers of clients. Typically, an application requires fewer stateless session beans than stateful session beans to support the same number of clients.

EJB ingredients Interfaces: The remote and home interfaces are required for remote access. For local access, the local and local home interfaces are required. Enterprise bean class: Implements the methods defined in the interfaces. Helper classes: Other classes needed by the enterprise bean class, such as exception and utility classes. Deployment descriptor: see later

Remote Interface Must throw RemoteException /** * This is the HelloBean remote interface. * * This interface is what clients operate on when * they interact with EJB objects. The container * vendor will implement this interface; the * implemented object is the EJB object, which * delegates invocations to the actual bean. */ public interface Hello extends javax.ejb.EJBObject { * The one method - hello - returns a greeting to the client. public String hello() throws java.rmi.RemoteException; } Must throw RemoteException

Home Interface /** * This is the home interface for HelloBean. This interface * is implemented by the EJB Server’s tools - the * implemented object is called the Home Object, and serves * as a factory for EJB Objects. * * One create() method is in this Home Interface, which * corresponds to the ejbCreate() method in HelloBean. */ public interface HelloHome extends javax.ejb.EJBHome { /* * This method creates the EJB Object. * @return The newly created EJB Object. Hello create() throws java.rmi.RemoteException, javax.ejb.CreateException; }

Bean Implementation /** * Demonstration stateless session bean. */ public class HelloBean implements javax.ejb.SessionBean { private javax.ejb.SessionContext ctx; // // EJB-required methods public void ejbCreate() { System.out.println(“ejbCreate()”); } public void ejbRemove() { System.out.println(“ejbRemove()”); } public void ejbActivate() { System.out.println(“ejbActivate()”);} public void ejbPassivate() {System.out.println(“ejbPassivate()”);} public void setSessionContext(javax.ejb.SessionContext ctx) { this.ctx = ctx; } // Business methods public String hello() { System.out.println(“hello()”); return “Hello, World!”; }

Client Implementation import javax.naming.Context; import javax.naming.InitialContext; import java.util.Properties; /** * This class is an example of client code that invokes * methods on a simple stateless session bean. */ public class HelloClient { public static void main(String[] args) throws Exception { /* * Setup properties for JNDI initialization. * These properties will be read in from the command line. Properties props = System.getProperties(); * Obtain the JNDI initial context. * The initial context is a starting point for * connecting to a JNDI tree. We choose our JNDI * driver, the network location of the server, etc. * by passing in the environment properties. Context ctx = new InitialContext(props);

Client Implementation /* Get a reference to the home object - the * factory for Hello EJB Objects */ Object obj = ctx.lookup(“HelloHome”); /* Home objects are RMI-IIOP objects, and so they must be cast * into RMI-IIOP objects using a special RMI-IIOP cast. HelloHome home = (HelloHome) javax.rmi.PortableRemoteObject.narrow(obj, HelloHome.class); /* Use the factory to create the Hello EJB Object Hello hello = home.create(); /*Call the hello() method on the EJB object. The * EJB object will delegate the call to the bean, * receive the result, and return it to us. * We then print the result to the screen. System.out.println(hello.hello()); /* * Done with EJB Object, so remove it. * The container will destroy the EJB object. hello.remove(); }

The logical architecture Client Machine NamingService Directory Machine Pool App server (container) Machine HomeInterface Istanza Find the Home interface Find Give me an instance Create or fetch An instance Method()

Deployment Descriptor Deployment descriptor: An XML file that specifies information about the bean such as its transaction attributes. You package the files in the preceding list into an EJB JAR file, the module that stores the enterprise bean. To assemble a J2EE application, you package one or more modules--such as EJB JAR files--into an EAR file, the archive file that holds the application.

ejb-jar.xml <?xml version=”1.0” encoding=”UTF-8”?> <ejb-jar xmlns=”http://java.sun.com/xml/ns/j2ee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd” version=”2.1”> <enterprise-beans> <session> <ejb-name>HelloWorldEJB</ejb-name> <home>examples.ejb21.HelloHome</home> <remote>examples.ejb21.Hello</remote> <local-home>examples.ejb21.HelloLocalHome</local-home> <local>examples.ejb21.HelloLocal</local> <ejb-class>examples.ejb21.HelloBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> ... </ejb-jar>

ejb-jar.xml (continued) <assembly-descriptor> <security-role> <description> This role represents everyone who is allowed full access to the HelloWorldEJB. </description> <role-name>everyone</role-name> </security-role> <method-permission> <method> <ejb-name>HelloWorldEJB</ejb-name> <method-name>*</method-name> </method> </method-permission> <container-transaction> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor>

The file structure Client packages .class Jboss jndi.properties ejb.jar packages .java ejb-jar.xml jboss.xml

Introduction to Session beans LOCAL BEANS

Local Interface May throw EJBException instead of RemoteException /** * This is the HelloBean local interface. * * This interface is what local clients operate * on when they interact with EJB local objects. * The container vendor will implement this * interface; the implemented object is the * EJB local object, which delegates invocations * to the actual bean. */ public interface HelloLocal extends javax.ejb.EJBLocalObject { * The one method - hello - returns a greeting to the client. public String hello(); } May throw EJBException instead of RemoteException

Local Home Interface /** * This is the home interface for HelloBean. This interface * is implemented by the EJB Server’s tools - the * implemented object is called the Home Object, and serves * as a factory for EJB Objects. * * One create() method is in this Home Interface, which * corresponds to the ejbCreate() method in HelloBean. */ public interface HelloLocalHome extends javax.ejb.EJBLocalHome { /* * This method creates the EJB Object. * @return The newly created EJB Object. HelloLocal create() throws javax.ejb.CreateException; }

Local Client Object ref = jndiContext.lookup(“HelloHome"); HelloHome home = (HelloHome) PortableRemoteObject.narrow(ref,HelloHome.class); … HelloHome cabin_1 = home.create(); HelloLocalHome home = (HelloLocalHome ) jndiContext.lookup(“java:comp/env/ejb/ HelloLocalHome "); … HelloLocalHome cabin_1 = home.create(); We looked up a bean in java:comp/env/ejb. This is the JNDI location that the EJB specification recommends (but does not require) you put beans that are referenced from other beans.

Hierarchy of HelloWorld

Introduction to Session beans EJB 3.0

Remote Interface package examples.session.stateless; EJB 2.1 ===================================================== public interface Hello extends javax.ejb.EJBObject { /** * The one method - hello - returns a greeting to the client. */ public String hello() throws java.rmi.RemoteException; } EJB 3.0 ===================================================== package examples.session.stateless; public interface Hello { public String hello(); business interface

Bean Implementation enterprise bean instance EJB 2.1 =============================================================== public class HelloBean implements javax.ejb.SessionBean { private javax.ejb.SessionContext ctx; public void ejbCreate() { System.out.println(“ejbCreate()”); } public void ejbRemove() { System.out.println(“ejbRemove()”); } public void ejbActivate() { System.out.println(“ejbActivate()”);} public void ejbPassivate() {System.out.println(“ejbPassivate()”);} public void setSessionContext(javax.ejb.SessionContext ctx) { this.ctx = ctx; } public String hello() { System.out.println(“hello()”); return “Hello, World!”; } EJB 3.0 ============================================================== package examples.session.stateless; import javax.ejb.Remote; import javax.ejb.Stateless; @Stateless @Remote(Hello.class) public class HelloBean implements Hello { System.out.println(“hello()”); return “Hello, World!”; enterprise bean instance

The remote client – 3.0 package examples.session.stateless; import javax.naming.Context; import javax.naming.InitialContext; public class HelloClient { public static void main(String[] args) throws Exception { /* * Obtain the JNDI initial context. * * The initial context is a starting point for * connecting to a JNDI tree. */ Context ctx = new InitialContext(); Hello hello = (Hello) ctx.lookup(“examples.session.stateless.Hello”); * Call the hello() method on the bean. * We then print the result to the screen. System.out.println(hello.hello()); }

ejb-jar.xml – 3.0 <?xml version=”1.0” encoding=”UTF-8” ?> <ejb-jar xmlns=”http://java.sun.com/xml/ns/j2ee” xmlns:xsi=”http://www.w3.org/2001/XMLSchemainstance” xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_3_0.xsd” version=”3.0”> <enterprise-beans> </enterprise-beans> </ejb-jar>

Keep in mind these terms… The enterprise bean instance is a plain old Java object instance of an enterprise bean class. It contains business method implementations of the methods defined in the remote/local business interface, for session beans. The business interface is a plain old Java interface that enumerates the business methods exposed by the enterprise bean. Depending on the client view supported by the bean, the business interface can be further classified into a local business interface or a remote business interface. The deployment descriptor is an XML file that specifies the middleware requirements for your bean. You use the deployment descriptor to inform the container about the services you need for the bean, such as transaction services, security, and so on. Alternatively, you can specify the middleware requirements using deployment annotations within the bean class as well.

Keep in mind these terms… The Ejb-jar file is the packaging unit for an enterprise bean, consisting of all the above artifacts. An EJB 3.0 Ejb-jar file can also consist of the old-style beans, if your application uses components defined using pre–EJB 3.0 technologies. The vendor-specific deployment descriptor lets you specify your bean’s needs for proprietary container services such as clustering, load balancing, and so on. Avendor can alternatively provide deployment metadata for these services, which, like standard metadata, can be used within the bean class to specify the configuration for these services. The vendor-specific deployment descriptor’s definition changes from vendor to vendor.

3.0 Packaging

3.0 Packaging

3.0 Lifecycle

Passivation

Activation

Managing the lifecycle – 3.0 @Stateful public class MyBean { @PrePassivate public void passivate() { <close socket connections, etc...> } ... @PostActivate public void activate() { <open socket connections, etc...>

Download Netbeans 6.0 beta JBOSS and NetBeans Download Jboss 4.2.1 http://labs.jboss.com/jbossas/downloads/ Download Netbeans 6.0 beta http://www.netbeans.org/community/releases/60/

Setting the JNDI properties private HelloBeanRemote lookupHelloBeanBean() { Properties props= new Properties(); props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); props.setProperty("java.naming.provider.url", "jnp://localhost:1099"); props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); try { Context c = new InitialContext(props); //return (HelloBeanRemote) c.lookup("java:comp/env/HelloBeanBean"); return (HelloBeanRemote) c.lookup("EnterpriseDemo/HelloBeanBean/remote"); } catch (NamingException ne) { … }

JBOSS: see the JNDI names http://localhost:8080/jmx-console/HtmlAdaptor Click on Service=JNDI Choose List