Docstoc

WTP 8b

Document Sample
WTP 8b Powered By Docstoc
					    Chapter 8b:
Business Logic Tier:
   Session EJB




    Reference: Eclipse WTP   1
                  Objectives

•   Enterprise Java Beans
•   Distributed Application Using EJBs
•   Types of EJBs
•   Developing Session EJBs
•   Writing EJB Clients

Important: The WTP textbook uses EJB 2.x (J2EE
  1.4). You need to use EJB 3.x (JEE 5) for your
  assignments.
                                               2
            What are EJBs
• Enterprise JavaBeans (EJBs): server-side
  software components that conform to the J2EE
  architecture for development and deployment of
  distributed systems
• J2EE-compliant application servers must provide
  a run-time environment for the EJBs: an EJB
  container




                                                3
Distributed Application Using EJBs

        <<web>>                        Presentation
       League UI                               Tier
                                        (WebApps)




                     Network



       League EJBs      Other EJBs    Business Tier
                                     Services Layer
                                             (EJBs)

                                                      4
  Types of EJBs: J2EE 1.4

• Session beans:
  – can perform any kind of processing
  – models business processes (coarse-
    grained components)
• Entity beans:
  – represent persistent data
  – models fine-grained data objects
• Message-driven beans (MDB):
  – used with messaging software
                                         5
    Types of EJBs: JEE 5

• Session beans:
  – Performs a task for a client
  – May implement a web service
• Message-driven beans (MDB):
  – Acts as a listener for Java Message
    Service API
• Entity beans:
  – Replaced by Java Persistence API
    (JPA) entities
                                          6
Major Components of an EJB 2.x
• Bean class
  – Implements the business methods
• Home interface
  – Defines the methods that allows clients
    to create, find and remove EJB objects
• Remote interface
  – Defines the business methods callable
    by the a remote client


                                              7
MVC Architecture




                   8
   EJB Containers and Services
• The role of the EJB container is to provide the
  following:
  – The distribution infrastructure and a naming service to
    help client code locate and access EJBs.
  – The ability to place EJBs in a scalable architecture
  – Support for concurrent access
  – Resource management, including connection pooling
  – Security services in addition to the secure
    environment that can be configured for applications
    loaded into the application server
  – Transaction managers that interact with JDBC drivers
    and resource adapters
                                                          9
  J2EE Enterprise Application
  Packaging and Deployment
• EJBs are always packaged in J2EE enterprise
  applications
• Files that compose EJBs are packaged in jar
  files
• An EJB jar must contain a deployment
  descriptor file (ejb-jar.xml)




                                            10
J2EE Packaging into Archive Files




                                11
           Session EJBs

• Session EJBs can do general purpose
  processing
• Associated with the client that calls them
• There are two types of session EJBs:
  – stateful
  – stateless
     • EJB Design Pattern: Session Façade (p.327)
        – E.g., LeagueFacade EJB wraps the
          IceHockeyFacade object


                                                    12
   Stateless Session EJBs
• Stateless session EJBs can be shared among
  clients
   – Used in Exercise 3 and Assignment 3




Reference: JEE 5 Tutorial
                                               13
    Stateless Session EJB 2.x
• Methods defined in the javax.ejb.SessionBean
  interface are call-back methods (e.g.,
  ejbCreate()) that control the lifecycle of the
  bean, that are called by the container

public abstract class LeagueFacadeBean implements
LeagueFacade, javax.ejb.SessionBean {
    /**
     * @ejb.create-method view-type="remote"
     */
    public void ejbCreate() {
         leagueFacade = IceHockeyFacade.getLeagueFacade();
    }…
}

                                                             14
Iteration 2: Developing Session EJB 2.x
Reference: Pages 325 -355
• General Steps:
  – Create an EAR and EJB projects
  – Create a Stateless Session Bean
     • Note: Home and Remote Interfaces generated by XDoclet
  – Add business methods to Bean class
     • Note: can be automatic if interface specified in Wizard
  – Add XDoclet annotation to set the view-type
     • Business methods added to Local and Remote Interface
  – Build a Web client
  – Run the Web client
     • Deploy and test the Session Bean

                                                                 15
  EJB 2.x Component Class Diagram
        Reference: page 329
     Renamed to
LeagueServiceManager
      in EJB 3.x




• Assembled into LeaguePlanetEJB.jar
• In the com.leagueplanet.ejb package, only LeagueFacadeBean code
typically changed by developer; the other class/interfaces auto-created by
XDoclet.
                                                                             16
     Sample EJB 3.x Bean Class
• Implement findLeague() method by calling findLeague()
  method of the IceHockeyDOAImpl (service layer).
import com.leagueplanet.dao.LeagueDAO;
…
public class LeagueServiceManager implements
LeagueServiceManagerRemote, LeagueDAO {
                                                      Other options:
    private LeagueDAO leagueDAO;               • IceHockeyJdbcDAOImpl
                                                • IceHockeyJPADAOImpl
    public LeagueServiceManager() {
        this.leagueDAO = IceHockeyDOAImpl.getLeagueDAO();
    }

    public League findLeague(long id) {
        return leagueDAO.findLeague(id);
    }
    …                                                             17
}
   Sample EJB 3.x Remote Interface

• Code skeleton generated by EJB Session Bean Wizard
  in LeagueEJBClient project
package com.leagueplanet.ejbs;
import javax.ejb.Remote;
                                                To distinguish an EJB 3.x
import com.leagueplanet.model.League;           session bean, we append
import com.leagueplanet.model.Schedule;         ServiceManager (instead
                                                 of Facade for EJB 2.x)
@Remote
public interface LeagueServiceManagerRemote {
     public Schedule findSchedule(long id);
     public League findLeague(long id);
     public void save(League newLeague);
}


                                                                            18
     Stateful Session EJBs
• Stateful session EJBs retain conversational state
  between method calls and are used by only one
  client
   – not covered in this course




Reference: JEE 5 Tutorial                             19
                 EJB Clients

• Since EJB 2.0, local
  interfaces were
  added to session
  beans specifically for
  use by co-located
  clients (i.e., located in
  the same JVM)
   – Local and Remote
     options available




                               20
    Writing EJB 2.x Clients
Reference: Pages 349 - 352
• The client calls getHome() method generated
  by XDoclet.
  – getHome() performs a lookup to the JNDI
    namespace using the JNDI_NAME (e.g.,
    LeagueFacade)
• When the client has the EJB home, it uses a
  create() method to get a handle to the remote
  interface
• When the client has the EJB remote object, it
  calls the business methods (e.g.,
  findLeague())
                                                21
        Writing EJB 3.x Clients
Reference: Exercise 3
• Add code to locate and get EJB 3.x bean instance
• Servlet:
      @EJB
      private HelloManagerRemote mgr;
• JSPs:
<%!
      private LeagueServiceManagerRemote mgr = null;
      public void jspInit() { ...
         InitialContext ic = new InitialContext();
         mgr = (LeagueServiceManagerRemote) ic.
            lookup(LeagueServiceManagerRemote.class.getName()); ...} ...
%>

                                                                      22
                   Next Steps
• Read JEE 5 Tutorial
  – Chapter 20: Enterprise Beans
  – URL: http://java.sun.com/javaee/5/docs/tutorial/doc/
• Exercise 3
  – EJB 3.x Session Bean:
     • LeagueServiceManager
  – Replaces Chapter 8: Iteration 2 tutorial (EJB 2.x)
• Assignment 3
  – XXYYSurveyEJB: XXYYSurveyServiceManager
  – XXYYSurveyEJBClient
                                                           23

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:4
posted:4/26/2011
language:English
pages:23