Enterprise java beans(ejb) update 2

1,578 views

Published on

Published in: Education, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,578
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
1
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • 06/17/10
  • 06/17/10
  • Enterprise java beans(ejb) update 2

    1. 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. 2. Enterprise Java Beans: What really EJBs gives you <ul><li>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. </li></ul><ul><li>EJB let’s you customize and configure reusable components at deploy time with out touching the source code. </li></ul><ul><li>EJBs are portable not just to different JVM’s but to different EJB servers. </li></ul>06/17/10 JavaTruths by Vikram Singh
    3. 3. How does it all works? <ul><li>Beans run under the control of ejb server. </li></ul><ul><li>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. </li></ul>06/17/10 JavaTruths by Vikram Singh
    4. 4. Type of Beans <ul><li>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. </li></ul><ul><li>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. </li></ul><ul><li>Session – use a session bean for … everything else. Where an entity bean represents a thing, a session bean typically represents a process. </li></ul>06/17/10 JavaTruths by Vikram Singh
    5. 5. Session Bean can be stateless or stateful <ul><li>Session bean can be marked (at deployment time) as either stateless or stateful. </li></ul><ul><li>A stateful bean can remember conversational state between method calls, while stateless bean won’t remember anything about the client between method invocations. </li></ul><ul><li>Stateful session beans are less-scalable. </li></ul>06/17/10 JavaTruths by Vikram Singh
    6. 6. FIVE THING YOU DO TO BUILD A BEAN <ul><li>Code the bean class with all of the business methods, </li></ul><ul><li>Code two interfaces for the bean: home and component. </li></ul><ul><li>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 </li></ul><ul><li>Put the bean, the interfaces and the DD into an ejb-jar file. </li></ul><ul><li>Deploy the bean into the server, using the tools provided by the server vendor. </li></ul>06/17/10 JavaTruths by Vikram Singh
    7. 7. Write the Bean Class <ul><li>Bean class provides the implementation of the business methods declared in the component inteface. </li></ul><ul><li>There are three types of beans, Session, Entity and Message – Driven. </li></ul><ul><li>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. </li></ul>06/17/10 JavaTruths by Vikram Singh
    8. 8. AdviceGuy Bean <ul><li>Advice Guy gives back an advice string when you invoke the getAdvice() method. </li></ul><ul><li>We’ve chosen a Session bean here becoz its perfect for AdviceGuy application. </li></ul><ul><li>So, our bean class implements the SessionBean interface and SessionBean has methods your bean class must implement. </li></ul><ul><li>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. </li></ul>06/17/10 JavaTruths by Vikram Singh
    9. 9. BEAN CLASS <ul><li>import javax.ejb.*; </li></ul><ul><li>public class AdviceBean implements SessionBean{ </li></ul><ul><li>private String[] adstring=“advice string goes here”; </li></ul><ul><li>public void ejbActivate(){} </li></ul><ul><li>public void ejbPassivate(){} </li></ul><ul><li>public void ejbRemove(){} </li></ul><ul><li>public void setSessionContext(SesssionContext sc){ </li></ul><ul><ul><li>} </li></ul></ul><ul><li>public String getAdvice(){ </li></ul><ul><li>return adstring; </li></ul><ul><li>} </li></ul><ul><li>public void ejbCreate(){} </li></ul><ul><li>} </li></ul>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. 10. Write two interfaces for the Bean <ul><li>These are the two interfaces that client sees; </li></ul><ul><li>COMPONENT interface: </li></ul><ul><li>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. </li></ul><ul><li>import javax.ejb.*; </li></ul><ul><li>import java.rmi.RemoteException; </li></ul><ul><li>public interface Advice extends EJBObject{ </li></ul><ul><li>public String getAdvice() throws RemoteException; </li></ul><ul><li>} </li></ul>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. 11. Write two interfaces for the Bean <ul><li>These are the two interfaces that client sees; </li></ul><ul><li>COMPONENT interface: </li></ul><ul><li>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. </li></ul><ul><li>import javax.ejb.*; </li></ul><ul><li>import java.rmi.RemoteException; </li></ul><ul><li>public interface Advice extends EJBObject{ </li></ul><ul><li>public String getAdvice() throws RemoteException; </li></ul><ul><li>} </li></ul>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. 12. Home Interface <ul><li>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. </li></ul><ul><li>import javax.ejb.*; </li></ul><ul><li>import java.rmi.RemoteException; </li></ul><ul><li>public interface AdviceHome extends EJBHome{ </li></ul><ul><li>public Advice create() throws CreateException,RemoteException; </li></ul><ul><li>} </li></ul>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. 13. Create an XML DD <ul><li><?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'> </li></ul><ul><li><ejb-jar> </li></ul><ul><li><enterprise-beans> </li></ul><ul><li><session> </li></ul><ul><li><ejb-name>AdviceBean</ejb-name> </li></ul><ul><li><home>AdviceHome</home> </li></ul><ul><li><remote>Advice</remote> </li></ul><ul><li><ejb-class>AdviceBean</ejb-class> </li></ul><ul><li><session-type>Stateless</session-type> </li></ul><ul><li><transaction-type>Bean</transaction-type> </li></ul><ul><li><security-identity> </li></ul><ul><li><description></description> </li></ul><ul><li><use-caller-identity></use-caller-identity> </li></ul><ul><li></security-identity> </li></ul><ul><li></session> </li></ul><ul><li></enterprise-bean> </li></ul><ul><li></ejb-jar> </li></ul>Specify home interface name Remote interface 06/17/10 JavaTruths by Vikram Singh
    14. 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. 15. Deploy the bean into the server,using the tools provided by the server vendor 06/17/10 JavaTruths by Vikram Singh
    16. 16. How to call a bean from Client Side? <ul><li>Get a reference to a JNDI InitialContext </li></ul><ul><li>Use the InitialContext to do a lookup on the home interface of the Bean(that we named “Advisor” when we deployed) </li></ul><ul><li>Narrow and Cast the thing we get back from the lookup.(that thing is something that implements the AdviceHome interface). </li></ul><ul><li>Call Create on the home interface to get back a reference to the component interface. </li></ul><ul><li>Call getAdvice()(“the business method”) on the component interface and print the result. </li></ul>06/17/10 JavaTruths by Vikram Singh
    17. 17. The Client Code(AdviceClient.java) <ul><li>import javax.naming.*; </li></ul><ul><li>Import java.rmi.*; </li></ul><ul><li>Import javax.rmi.*; </li></ul><ul><li>Import javax.ejb.*; </li></ul><ul><li>Public class AdviceClient{ </li></ul><ul><li>public static void main(String[ ] args){ </li></ul><ul><li>new AdviceClient().go(); </li></ul><ul><li>} </li></ul><ul><li>public void go(){ </li></ul><ul><li>try{ </li></ul><ul><li>Context ic=new InitialContext(); </li></ul><ul><li>Object o=ic.lookup(“Advisor”); </li></ul><ul><li>AdviceHome home=(AdviceHome)PortableRemoteObject.narrow(o,AdviceHome.class); </li></ul><ul><li>Advice advisor=home.create(); </li></ul><ul><li>System.out.println(advisor.getAdvice()); </li></ul><ul><li>}catch(Exception ex){ ex.printStackTrace();} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>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. 18. EJB ARCHITECTURE 06/17/10 JavaTruths by Vikram Singh
    19. 19. EJB uses RMI 06/17/10 JavaTruths by Vikram Singh
    20. 20. How EJB uses RMI 06/17/10 JavaTruths by Vikram Singh
    21. 21. EJB uses RMI <ul><li>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. </li></ul><ul><li>Once the call gets to the EJBObject, the server jumps in with all the services. </li></ul><ul><li>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. </li></ul>06/17/10 JavaTruths by Vikram Singh
    22. 22. *** <ul><li>Both the Remote object and the stub implement the same interface – the business interface(called a component interface) – but without the real business behavior. </li></ul><ul><li>The Bean class does NOT implement the business interface but the bean has the real business logic functionality. </li></ul>06/17/10 JavaTruths by Vikram Singh
    23. 23. *** <ul><li>In EJB, the business interface is called the component interface. This is where you expose your business to the client. </li></ul><ul><li>A Bean may have multiple business interfaces according to the client’s need. </li></ul><ul><li>A component inteface extends EJBObject interface that extends java.rmi.Remote interface. </li></ul>06/17/10 JavaTruths by Vikram Singh
    24. 24. Tricky Questions <ul><li>Who writes the class that really DOES implement the component interface? In other words, who makes the EJBObject Class? </li></ul><ul><li>The Container creates four things: </li></ul><ul><li>The EJBObject class(implements your component interface) </li></ul><ul><li>The EJBObject stub class (implements component interface and knows how to talk to the EJBObject) </li></ul><ul><li>The Home class (implements the home interface) </li></ul><ul><li>The Home stub class(implements your home interface and knows how to talk to the home) </li></ul>06/17/10 JavaTruths by Vikram Singh
    25. 25. How EJB really Works? 06/17/10 JavaTruths by Vikram Singh
    26. 26. How EJB really Works? 06/17/10 JavaTruths by Vikram Singh
    27. 27. How EJB really Works? 06/17/10 JavaTruths by Vikram Singh

    ×