SlideShare a Scribd company logo
Enterprise Java Beans(EJB) With EJBs, you can develop building blocks ‘ejb components ’ – that u and someone else can assemble and reassemble into different applications. For example , you might create a customer bean that represents a customer in database. You can use that Customer bean in an accouting program, an e-commerce shopping cart and a tech support application. One beauty of EJBs is that you take code reuse to a whole new level, instead of just code it reuses whole functionality and allows you modify that way bean behaves at runtime with touching its java code. 06/17/10 JavaTruths by Vikram Singh
Enterprise Java Beans: What really EJBs gives you EJB lets you focus on the business logic for your business, and leave the underlying services (transaction,networking, security etc.) to the EJB server vender. EJB let’s you customize and configure reusable components at deploy time with out touching the source code. EJBs are portable not just to different JVM’s but to different EJB servers. 06/17/10 JavaTruths by Vikram Singh
How does it all works? Beans run under the control of ejb server. The server steps into the middle of every method call from a client to a bean and inserts the “services ” like security, transactions and persistence. 06/17/10 JavaTruths by Vikram Singh
Type of Beans Entity Bean  - Use an entity bean to represent thing in persistent store. That almost always means something in a database, where an instance of an entity bean represents a row in a table. Message driven bean- use a message driven bean only when you need a JMS Consumer. In other words, a bean that can listen for message from a JMS messaging service. Session – use a session bean for … everything else. Where an entity bean represents a thing, a session bean typically represents a process.  06/17/10 JavaTruths by Vikram Singh
Session Bean can be stateless or stateful  Session bean can be marked (at deployment time) as either stateless or stateful. A stateful bean can remember conversational state between method calls, while stateless bean won’t remember anything about the client between method invocations. Stateful session beans are less-scalable. 06/17/10 JavaTruths by Vikram Singh
FIVE THING YOU DO TO BUILD A BEAN Code the bean class with all of the business methods, Code two interfaces for the bean: home and component. Create an XML deployment descriptor that tells the server what your bean is and how it should be managed. You must name it as ejb-jar.xml Put the bean, the interfaces and the DD into an ejb-jar file. Deploy the bean into the server, using the tools provided by the server vendor. 06/17/10 JavaTruths by Vikram Singh
Write the Bean Class Bean class provides the implementation of the business methods declared in the component inteface. There are three types of beans, Session, Entity and Message – Driven. Before making a bean, you must decide what type you need becoz your bean class must implement one of three interfaces, depending on the type you choose. 06/17/10 JavaTruths by Vikram Singh
AdviceGuy Bean Advice Guy gives back an advice string when you invoke the getAdvice() method. We’ve chosen a Session bean here becoz its perfect for AdviceGuy application. So, our bean class implements the SessionBean interface and SessionBean has methods your bean class must implement. The methods you implement from SessionBean interface are known as container callbacks, becoz the container uses then to notify you of important milestone in the bean’s life cycle. 06/17/10 JavaTruths by Vikram Singh
BEAN CLASS import javax.ejb.*; public class AdviceBean implements SessionBean{ private String[] adstring=“advice string goes here”; public void ejbActivate(){} public void ejbPassivate(){} public void ejbRemove(){} public void setSessionContext(SesssionContext sc){ } public String getAdvice(){ return adstring; } public void ejbCreate(){} } You need this package These four methos are from SessionBean interface, so you have to put them here,for now worry about what these are for! You must have an ejbCreate() method. Its an ejb rule we’ll learn about later. But it doesn’t come from SessionBean interface. You must implemet one of three bean type interfaces: 06/17/10 JavaTruths by Vikram Singh
Write two interfaces for the Bean These are the two interfaces that client sees; COMPONENT interface: This is whre all the business methods are declared. In other words,it’s where yo put all the methods the client wants to call. import javax.ejb.*; import java.rmi.RemoteException; public interface Advice extends EJBObject{ public String  getAdvice()  throws RemoteException; } It must extend either the EJBObject or EJBLocalObject. This is the actual business method. It MUST  correspond to a method in the bean class. 06/17/10 JavaTruths by Vikram Singh
Write two interfaces for the Bean These are the two interfaces that client sees; COMPONENT interface: This is whre all the business methods are declared. In other words,it’s where yo put all the methods the client wants to call. import javax.ejb.*; import java.rmi.RemoteException; public interface Advice extends EJBObject{ public String  getAdvice()  throws RemoteException; } It must extend either the EJBObject or EJBLocalObject. This is the actual business method. It MUST  correspond to a method in the bean class. 06/17/10 JavaTruths by Vikram Singh
Home Interface The client uses the home interface to ask for a reference to the component interface. The home is the client’s starting point for getting hold of a reference to a bean.  import javax.ejb.*; import java.rmi.RemoteException; public interface AdviceHome extends EJBHome{ public Advice create() throws CreateException,RemoteException; } The create() method must return your component interface type!! The home must extend either the EJBHome interface or EJBLocalHome  06/17/10 JavaTruths by Vikram Singh
Create an XML DD  <?xml version=&quot;1.0&quot;?> <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN' 'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'>  <ejb-jar>  <enterprise-beans>  <session>  <ejb-name>AdviceBean</ejb-name>  <home>AdviceHome</home>  <remote>Advice</remote>  <ejb-class>AdviceBean</ejb-class>  <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> <security-identity> <description></description> <use-caller-identity></use-caller-identity> </security-identity> </session> </enterprise-bean> </ejb-jar> Specify home interface name Remote interface 06/17/10 JavaTruths by Vikram Singh
Put the Bean, the interface and the DD into ejb-jar file META-INF EJB-JAR.XML MYPACKAGE AdvcieBean.class AdviceHome.class Advice.class MYEJB.JAR ejb-jar.xml must be in a directory named META-INF 06/17/10 JavaTruths by Vikram Singh
Deploy the bean into the server,using the tools provided by the server vendor 06/17/10 JavaTruths by Vikram Singh
How to call a bean from Client Side? Get a reference to a JNDI InitialContext  Use the InitialContext to do a lookup on the home interface of the Bean(that we named “Advisor” when we deployed) Narrow and Cast the thing we get back from the lookup.(that thing is something that implements the AdviceHome interface). Call Create on the home interface to get back a reference to the component interface. Call getAdvice()(“the business method”) on the component interface and print the result. 06/17/10 JavaTruths by Vikram Singh
The Client Code(AdviceClient.java) import javax.naming.*; Import java.rmi.*; Import javax.rmi.*; Import javax.ejb.*; Public class AdviceClient{ public static void main(String[ ] args){ new AdviceClient().go(); } public void go(){ try{ Context ic=new InitialContext(); Object o=ic.lookup(“Advisor”); AdviceHome home=(AdviceHome)PortableRemoteObject.narrow(o,AdviceHome.class); Advice advisor=home.create(); System.out.println(advisor.getAdvice()); }catch(Exception ex){ ex.printStackTrace();} } } Finally we get to the actual business methods. Lookup the AdviseBean using the JNDI name we gave during deployment. Initialcontext is our entrypoint into JNDI directory 06/17/10 JavaTruths by Vikram Singh
EJB ARCHITECTURE 06/17/10 JavaTruths by Vikram Singh
EJB uses RMI 06/17/10 JavaTruths by Vikram Singh
How EJB uses RMI 06/17/10 JavaTruths by Vikram Singh
EJB uses RMI In EJB, the Remote object(EJB Object) is the bean’s bodyguard. The Bean sits back, protected from all client invocations, while EJBObject implements the Remote interface and takes the remote calls.  Once the call gets to the EJBObject, the server jumps in with all the services. The EJBObject implemens the Remote business interface, so the remote calls from the client come to the EJBObject. But it’s still the bean that has the business logic, even though the bean doesn’t implement the Remote interface. 06/17/10 JavaTruths by Vikram Singh
*** Both the Remote object and the stub implement the same interface – the business interface(called a component interface) – but without the real business behavior. The Bean class does NOT implement the business interface but the bean has the real business logic functionality. 06/17/10 JavaTruths by Vikram Singh
*** In EJB, the business interface is called the component interface. This is where you expose your business to the client.  A Bean may have multiple business interfaces according to the client’s need. A component inteface extends EJBObject interface that extends java.rmi.Remote interface. 06/17/10 JavaTruths by Vikram Singh
Tricky Questions  Who writes the class that really DOES implement the component interface? In other words, who makes the EJBObject Class? The Container creates four things: The EJBObject class(implements your component interface) The EJBObject stub class (implements component interface and knows how to talk to the EJBObject) The Home class (implements the home interface) The Home stub class(implements your home interface and knows how to talk to the home) 06/17/10 JavaTruths by Vikram Singh
How EJB really Works? 06/17/10 JavaTruths by Vikram Singh
How EJB really Works? 06/17/10 JavaTruths by Vikram Singh
How EJB really Works? 06/17/10 JavaTruths by Vikram Singh

More Related Content

What's hot (17)

PDF
Enterprise JavaBeans(EJB)
Armen Arzumanyan
 
PDF
Enterprise Java Beans - EJB
Peter R. Egli
 
PPTX
Enterprise Java Beans 3 - Business Logic
Emprovise
 
PPTX
Skillwise EJB3.0 training
Skillwise Group
 
PPTX
Introduction to EJB
Return on Intelligence
 
PDF
EJB 3.0 - Yet Another Introduction
Kelum Senanayake
 
PDF
Ejb3 Presentation
Saurabh Raisinghani
 
PDF
EJB 3.1 by Bert Ertman
Stephan Janssen
 
PPTX
EJB3 Advance Features
Emprovise
 
PDF
EJB Interview Questions
guest346cb1
 
PPTX
Java EE EJB Applications
DevelopIntelligence
 
PPT
Spring talk111204
ealio
 
PDF
J2EE jsp_01
Biswabrata Banerjee
 
PPT
EJB 3.0 Java Persistence with Oracle TopLink
Bill Lyons
 
PDF
Ejb - september 2006
achraf_ing
 
PPT
Spring talk111204
s4al_com
 
PPT
Spring
s4al_com
 
Enterprise JavaBeans(EJB)
Armen Arzumanyan
 
Enterprise Java Beans - EJB
Peter R. Egli
 
Enterprise Java Beans 3 - Business Logic
Emprovise
 
Skillwise EJB3.0 training
Skillwise Group
 
Introduction to EJB
Return on Intelligence
 
EJB 3.0 - Yet Another Introduction
Kelum Senanayake
 
Ejb3 Presentation
Saurabh Raisinghani
 
EJB 3.1 by Bert Ertman
Stephan Janssen
 
EJB3 Advance Features
Emprovise
 
EJB Interview Questions
guest346cb1
 
Java EE EJB Applications
DevelopIntelligence
 
Spring talk111204
ealio
 
J2EE jsp_01
Biswabrata Banerjee
 
EJB 3.0 Java Persistence with Oracle TopLink
Bill Lyons
 
Ejb - september 2006
achraf_ing
 
Spring talk111204
s4al_com
 
Spring
s4al_com
 

Similar to Enterprise java beans(ejb) update 2 (20)

PPT
Enterprise java beans(ejb) Update 2
vikram singh
 
PPT
Enterprise java beans(ejb)
vikram singh
 
PPT
Session 3 Tp3
phanleson
 
PPT
ADVANCED JAVA MODULE I & II.ppt
rani marri
 
PPT
EJBDetailsFeb25.ppt
KalsoomTahir2
 
PDF
Ejb intro
MANOJ KUMAR
 
PPTX
The Latest in Enterprise JavaBeans Technology
Simon Ritter
 
PDF
Ejb intro
vantinhkhuc
 
PDF
Ejb notes
Mumbai Academisc
 
PPTX
Java bean
Jafar Nesargi
 
PPT
Aravind vinnakota ejb_architecture
tayab4687
 
PPT
Ch4 ejb
Raghavendra Goud
 
PDF
Introcution to EJB
Tharindu Weerasinghe
 
PPT
Unite5-EJB-2019.ppt
Krishna900061
 
PPT
CommercialSystemsBahman.ppt
KalsoomTahir2
 
DOCX
J2 ee tutorial ejb
Niraj Bharambe
 
PPT
EJB.ppthckhkhohjpfuysfzhxjvkgur6eydgdcjjggjj
rani marri
 
PPT
J2EEFeb11 (1).ppt
rani marri
 
PDF
Lecture 8 Enterprise Java Beans (EJB)
Fahad Golra
 
PDF
EJB et WS (Montreal JUG - 12 mai 2011)
Montreal JUG
 
Enterprise java beans(ejb) Update 2
vikram singh
 
Enterprise java beans(ejb)
vikram singh
 
Session 3 Tp3
phanleson
 
ADVANCED JAVA MODULE I & II.ppt
rani marri
 
EJBDetailsFeb25.ppt
KalsoomTahir2
 
Ejb intro
MANOJ KUMAR
 
The Latest in Enterprise JavaBeans Technology
Simon Ritter
 
Ejb intro
vantinhkhuc
 
Ejb notes
Mumbai Academisc
 
Java bean
Jafar Nesargi
 
Aravind vinnakota ejb_architecture
tayab4687
 
Introcution to EJB
Tharindu Weerasinghe
 
Unite5-EJB-2019.ppt
Krishna900061
 
CommercialSystemsBahman.ppt
KalsoomTahir2
 
J2 ee tutorial ejb
Niraj Bharambe
 
EJB.ppthckhkhohjpfuysfzhxjvkgur6eydgdcjjggjj
rani marri
 
J2EEFeb11 (1).ppt
rani marri
 
Lecture 8 Enterprise Java Beans (EJB)
Fahad Golra
 
EJB et WS (Montreal JUG - 12 mai 2011)
Montreal JUG
 
Ad

More from vikram singh (20)

PPTX
Agile
vikram singh
 
PDF
Web tech importants
vikram singh
 
PPT
Enterprise java beans(ejb)
vikram singh
 
DOC
2 4 Tree
vikram singh
 
DOC
23 Tree Best Part
vikram singh
 
DOC
JSP Scope variable And Data Sharing
vikram singh
 
PPT
Bean Intro
vikram singh
 
PPT
jdbc
vikram singh
 
PPT
Sax Dom Tutorial
vikram singh
 
PPT
Xml
vikram singh
 
PPT
Dtd
vikram singh
 
PPT
Xml Schema
vikram singh
 
PPT
JSP
vikram singh
 
PPT
Request dispatching in servlet
vikram singh
 
PPT
Servlet Part 2
vikram singh
 
DOC
Tutorial Solution
vikram singh
 
DOC
Java Script Language Tutorial
vikram singh
 
PPT
Web Tech Java Servlet Update1
vikram singh
 
DOC
Sample Report Format
vikram singh
 
PPT
Javascript
vikram singh
 
Web tech importants
vikram singh
 
Enterprise java beans(ejb)
vikram singh
 
2 4 Tree
vikram singh
 
23 Tree Best Part
vikram singh
 
JSP Scope variable And Data Sharing
vikram singh
 
Bean Intro
vikram singh
 
Sax Dom Tutorial
vikram singh
 
Xml Schema
vikram singh
 
Request dispatching in servlet
vikram singh
 
Servlet Part 2
vikram singh
 
Tutorial Solution
vikram singh
 
Java Script Language Tutorial
vikram singh
 
Web Tech Java Servlet Update1
vikram singh
 
Sample Report Format
vikram singh
 
Javascript
vikram singh
 
Ad

Recently uploaded (20)

PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
PDF
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PDF
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PPTX
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
PDF
'' IMPORTANCE OF EXCLUSIVE BREAST FEEDING ''
SHAHEEN SHAIKH
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
How to Manage Promotions in Odoo 18 Sales
Celine George
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
'' IMPORTANCE OF EXCLUSIVE BREAST FEEDING ''
SHAHEEN SHAIKH
 
PPT on the Development of Education in the Victorian England
Beena E S
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
How to Manage Promotions in Odoo 18 Sales
Celine George
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 

Enterprise java beans(ejb) update 2

  • 1. Enterprise Java Beans(EJB) With EJBs, you can develop building blocks ‘ejb components ’ – that u and someone else can assemble and reassemble into different applications. For example , you might create a customer bean that represents a customer in database. You can use that Customer bean in an accouting program, an e-commerce shopping cart and a tech support application. One beauty of EJBs is that you take code reuse to a whole new level, instead of just code it reuses whole functionality and allows you modify that way bean behaves at runtime with touching its java code. 06/17/10 JavaTruths by Vikram Singh
  • 2. Enterprise Java Beans: What really EJBs gives you EJB lets you focus on the business logic for your business, and leave the underlying services (transaction,networking, security etc.) to the EJB server vender. EJB let’s you customize and configure reusable components at deploy time with out touching the source code. EJBs are portable not just to different JVM’s but to different EJB servers. 06/17/10 JavaTruths by Vikram Singh
  • 3. How does it all works? Beans run under the control of ejb server. The server steps into the middle of every method call from a client to a bean and inserts the “services ” like security, transactions and persistence. 06/17/10 JavaTruths by Vikram Singh
  • 4. Type of Beans Entity Bean - Use an entity bean to represent thing in persistent store. That almost always means something in a database, where an instance of an entity bean represents a row in a table. Message driven bean- use a message driven bean only when you need a JMS Consumer. In other words, a bean that can listen for message from a JMS messaging service. Session – use a session bean for … everything else. Where an entity bean represents a thing, a session bean typically represents a process. 06/17/10 JavaTruths by Vikram Singh
  • 5. Session Bean can be stateless or stateful Session bean can be marked (at deployment time) as either stateless or stateful. A stateful bean can remember conversational state between method calls, while stateless bean won’t remember anything about the client between method invocations. Stateful session beans are less-scalable. 06/17/10 JavaTruths by Vikram Singh
  • 6. FIVE THING YOU DO TO BUILD A BEAN Code the bean class with all of the business methods, Code two interfaces for the bean: home and component. Create an XML deployment descriptor that tells the server what your bean is and how it should be managed. You must name it as ejb-jar.xml Put the bean, the interfaces and the DD into an ejb-jar file. Deploy the bean into the server, using the tools provided by the server vendor. 06/17/10 JavaTruths by Vikram Singh
  • 7. Write the Bean Class Bean class provides the implementation of the business methods declared in the component inteface. There are three types of beans, Session, Entity and Message – Driven. Before making a bean, you must decide what type you need becoz your bean class must implement one of three interfaces, depending on the type you choose. 06/17/10 JavaTruths by Vikram Singh
  • 8. AdviceGuy Bean Advice Guy gives back an advice string when you invoke the getAdvice() method. We’ve chosen a Session bean here becoz its perfect for AdviceGuy application. So, our bean class implements the SessionBean interface and SessionBean has methods your bean class must implement. The methods you implement from SessionBean interface are known as container callbacks, becoz the container uses then to notify you of important milestone in the bean’s life cycle. 06/17/10 JavaTruths by Vikram Singh
  • 9. BEAN CLASS import javax.ejb.*; public class AdviceBean implements SessionBean{ private String[] adstring=“advice string goes here”; public void ejbActivate(){} public void ejbPassivate(){} public void ejbRemove(){} public void setSessionContext(SesssionContext sc){ } public String getAdvice(){ return adstring; } public void ejbCreate(){} } You need this package These four methos are from SessionBean interface, so you have to put them here,for now worry about what these are for! You must have an ejbCreate() method. Its an ejb rule we’ll learn about later. But it doesn’t come from SessionBean interface. You must implemet one of three bean type interfaces: 06/17/10 JavaTruths by Vikram Singh
  • 10. Write two interfaces for the Bean These are the two interfaces that client sees; COMPONENT interface: This is whre all the business methods are declared. In other words,it’s where yo put all the methods the client wants to call. import javax.ejb.*; import java.rmi.RemoteException; public interface Advice extends EJBObject{ public String getAdvice() throws RemoteException; } It must extend either the EJBObject or EJBLocalObject. This is the actual business method. It MUST correspond to a method in the bean class. 06/17/10 JavaTruths by Vikram Singh
  • 11. Write two interfaces for the Bean These are the two interfaces that client sees; COMPONENT interface: This is whre all the business methods are declared. In other words,it’s where yo put all the methods the client wants to call. import javax.ejb.*; import java.rmi.RemoteException; public interface Advice extends EJBObject{ public String getAdvice() throws RemoteException; } It must extend either the EJBObject or EJBLocalObject. This is the actual business method. It MUST correspond to a method in the bean class. 06/17/10 JavaTruths by Vikram Singh
  • 12. Home Interface The client uses the home interface to ask for a reference to the component interface. The home is the client’s starting point for getting hold of a reference to a bean. import javax.ejb.*; import java.rmi.RemoteException; public interface AdviceHome extends EJBHome{ public Advice create() throws CreateException,RemoteException; } The create() method must return your component interface type!! The home must extend either the EJBHome interface or EJBLocalHome 06/17/10 JavaTruths by Vikram Singh
  • 13. Create an XML DD <?xml version=&quot;1.0&quot;?> <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN' 'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'> <ejb-jar> <enterprise-beans> <session> <ejb-name>AdviceBean</ejb-name> <home>AdviceHome</home> <remote>Advice</remote> <ejb-class>AdviceBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> <security-identity> <description></description> <use-caller-identity></use-caller-identity> </security-identity> </session> </enterprise-bean> </ejb-jar> Specify home interface name Remote interface 06/17/10 JavaTruths by Vikram Singh
  • 14. Put the Bean, the interface and the DD into ejb-jar file META-INF EJB-JAR.XML MYPACKAGE AdvcieBean.class AdviceHome.class Advice.class MYEJB.JAR ejb-jar.xml must be in a directory named META-INF 06/17/10 JavaTruths by Vikram Singh
  • 15. Deploy the bean into the server,using the tools provided by the server vendor 06/17/10 JavaTruths by Vikram Singh
  • 16. How to call a bean from Client Side? Get a reference to a JNDI InitialContext Use the InitialContext to do a lookup on the home interface of the Bean(that we named “Advisor” when we deployed) Narrow and Cast the thing we get back from the lookup.(that thing is something that implements the AdviceHome interface). Call Create on the home interface to get back a reference to the component interface. Call getAdvice()(“the business method”) on the component interface and print the result. 06/17/10 JavaTruths by Vikram Singh
  • 17. The Client Code(AdviceClient.java) import javax.naming.*; Import java.rmi.*; Import javax.rmi.*; Import javax.ejb.*; Public class AdviceClient{ public static void main(String[ ] args){ new AdviceClient().go(); } public void go(){ try{ Context ic=new InitialContext(); Object o=ic.lookup(“Advisor”); AdviceHome home=(AdviceHome)PortableRemoteObject.narrow(o,AdviceHome.class); Advice advisor=home.create(); System.out.println(advisor.getAdvice()); }catch(Exception ex){ ex.printStackTrace();} } } Finally we get to the actual business methods. Lookup the AdviseBean using the JNDI name we gave during deployment. Initialcontext is our entrypoint into JNDI directory 06/17/10 JavaTruths by Vikram Singh
  • 18. EJB ARCHITECTURE 06/17/10 JavaTruths by Vikram Singh
  • 19. EJB uses RMI 06/17/10 JavaTruths by Vikram Singh
  • 20. How EJB uses RMI 06/17/10 JavaTruths by Vikram Singh
  • 21. EJB uses RMI In EJB, the Remote object(EJB Object) is the bean’s bodyguard. The Bean sits back, protected from all client invocations, while EJBObject implements the Remote interface and takes the remote calls. Once the call gets to the EJBObject, the server jumps in with all the services. The EJBObject implemens the Remote business interface, so the remote calls from the client come to the EJBObject. But it’s still the bean that has the business logic, even though the bean doesn’t implement the Remote interface. 06/17/10 JavaTruths by Vikram Singh
  • 22. *** Both the Remote object and the stub implement the same interface – the business interface(called a component interface) – but without the real business behavior. The Bean class does NOT implement the business interface but the bean has the real business logic functionality. 06/17/10 JavaTruths by Vikram Singh
  • 23. *** In EJB, the business interface is called the component interface. This is where you expose your business to the client. A Bean may have multiple business interfaces according to the client’s need. A component inteface extends EJBObject interface that extends java.rmi.Remote interface. 06/17/10 JavaTruths by Vikram Singh
  • 24. Tricky Questions Who writes the class that really DOES implement the component interface? In other words, who makes the EJBObject Class? The Container creates four things: The EJBObject class(implements your component interface) The EJBObject stub class (implements component interface and knows how to talk to the EJBObject) The Home class (implements the home interface) The Home stub class(implements your home interface and knows how to talk to the home) 06/17/10 JavaTruths by Vikram Singh
  • 25. How EJB really Works? 06/17/10 JavaTruths by Vikram Singh
  • 26. How EJB really Works? 06/17/10 JavaTruths by Vikram Singh
  • 27. How EJB really Works? 06/17/10 JavaTruths by Vikram Singh

Editor's Notes