Java 2 Enterprise Edition _J2EE_ by zhangyun

VIEWS: 6 PAGES: 42

									               Java 2 Enterprise Edition (J2EE)


        • Portable Standard
        • Implements (more-or-less) most of the stuff
            we have described so far


        • [ACKM04] Ch 4
        • http://java.sun.com   aka www.javasoft.com




CS530 S05
              J2EE Application Server Architecture

                                JavaServer
            Servlets
                                Pages (JSP)
     Java API for XML
                                 JavaMail         support for communication
     Processing (JAXP)
                                                  and presentation
       Java Authentication and Authorization
                  Service (JAAS)


      Enterprise Java        Java transaction
        Beans (EJB)             API (JTA)         support for the
      Java Message        Java Naming/Directory   application integration
      Service (JMS)         Interface (JNDI)



      Java DataBase         Java 2 Connector      support for access to
    Connectivity (JDBC)    Architecture (J2CA)
                                                  resource managers




CS530 S05
              J2EE Application Server Architecture

                                JavaServer
            Servlets
                                Pages (JSP)
     Java API for XML
                                 JavaMail         support for communication
     Processing (JAXP)
                                                  and presentation
       Java Authentication and Authorization
                  Service (JAAS)


      Enterprise Java        Java transaction
        Beans (EJB)             API (JTA)         support for the
      Java Message        Java Naming/Directory   application integration
      Service (JMS)         Interface (JNDI)



      Java DataBase         Java 2 Connector      support for access to
    Connectivity (JDBC)    Architecture (J2CA)
                                                  resource managers




CS530 S05
                      Servlets


                       Web server
            Java server process

                  Java thread
            Servlet




CS530 S05
                          Servlets

                     Web server




                           Servlet JVM
                            Java thread
                      Servlet




      • JVM can be separate from Web Server
      • Still avoids process creation overhead
CS530 S05
                      Servlet Life Cycle
                      Web Server    Servlet JVM


      Request for A                  Servlet A

      Request for B
                                     Servlet B
      Request for A




        • First reference creates servlet object
        • Subsequent references just invoke methods
          • request and response parameters
CS530 S05
                     Servlet Life Cycle

        • Servlet notified when created and destroyed
        • Can save state between calls
     public class HitCountServlet extends HttpServlet {
       int timesAccessed;
       public void init(ServletConfig conf) ... {
         timesAccessed = 0;
       }
       public void doGet(
             HttpServletRequest req, HttpServletResponse resp) ... {
         ... timesAccessed++; ...
       }
     }
CS530 S05
                     Servlet Life Cycle

     It is more complicated than that.
     Servlet may be destroyed by container anytime,
       it must save state to stable storage:

       public class HitCountServlet extends HttpServlet {
           ...
         public void destroy() {
             ...
           outfile.writeInt(timesAccessed);
             ...
         }
       }



CS530 S05
                     Servlet Life Cycle




       • Real examples: serially reusable resources
            that are expensive to create
            • Database connection pools




CS530 S05
               Concurrency - Thread Safety
                      Web Server         Servlet JVM


      Request for A

                                              Servlet A

      Request for A




      • Each call has its own req and resp parameter
      • But servlet code -- and all code called by the
            servlet -- must be thread-safe!
CS530 S05
                     Session Context

         ...
        HTTPSession theSession = req.getSession(true);
         ...



      • Server will create session context
        • assign unique ID
        • track session by cookies or URL encoding
          or ... (not part of spec)
      • Servlet can store (anything) in session
        context, it will persist between calls
        • even if servlet is destroyed/recreated
CS530 S05
                                  JSP

         • A template engine:

      Database or     Template
                                        Web server   Browser
     other resource   processor



      Template and
      instructions




CS530 S05
                                 JSP

        • A JSP template is HTML with snippets of
            Java embedded in it
        •   Here is a really simple one ...

            <html>
            <body>
             Hello, visitor. It is now
               <%= new java.util.Date().toString() %>
            </body>
            </html>



CS530 S05
                                JSP

        • Here is one with control flow!
        <html>
        <body>
        <% java.util.Date theDate = new java.util.Date(); %>
        <% if (theDate.getHours() < 12) { %>
        Good morning,
        <% } else { %>
        Good afternoon,
        <% } %>
        visitor. It is now <%= theDate.toString() %>
        </body>
        </html>

CS530 S05
                     JSP Life Cycle
                 Server                    Servlet JVM

                                            Generated
   Request 1                JSP compiler
                                             servlet




   Request 2

   Request 3

      ...




     • The template is compiled (once) into a servlet
     • Later references re-use existing servlet
CS530 S05
              J2EE Application Server Architecture

                                JavaServer
            Servlets
                                Pages (JSP)
     Java API for XML
                                 JavaMail         support for communication
     Processing (JAXP)
                                                  and presentation
       Java Authentication and Authorization
                  Service (JAAS)


      Enterprise Java        Java transaction
        Beans (EJB)             API (JTA)         support for the
      Java Message        Java Naming/Directory   application integration
      Service (JMS)         Interface (JNDI)



      Java DataBase         Java 2 Connector      support for access to
    Connectivity (JDBC)    Architecture (J2CA)
                                                  resource managers




CS530 S05
                         JavaMail



        • Send and receive mail objects
        • Straightforward




CS530 S05
              J2EE Application Server Architecture

                                JavaServer
            Servlets
                                Pages (JSP)
     Java API for XML
                                 JavaMail         support for communication
     Processing (JAXP)
                                                  and presentation
       Java Authentication and Authorization
                  Service (JAAS)


      Enterprise Java        Java transaction
        Beans (EJB)             API (JTA)         support for the
      Java Message        Java Naming/Directory   application integration
      Service (JMS)         Interface (JNDI)



      Java DataBase         Java 2 Connector      support for access to
    Connectivity (JDBC)    Architecture (J2CA)
                                                  resource managers




CS530 S05
               JNDI Architecture
                      Naming              Directory



            Binding                                Attributes




            Context                                DirContext



                                Initial
                               context
                                                      Application

                                                Service Provider


                      Service provider interface


CS530 S05
               Naming and Directory

        • Naming service
          • context = bindings of names to objects or
             contexts
          • enumerate subcontexts
        • Directory service
          • dircontext = bindings of names to objects
             with attributes or dircontexts
          • enumerate attributes
        • Initial Context = where you start looking
          for objects / services
        • (like X.500 or LDAP)
CS530 S05
              J2EE Application Server Architecture

                                JavaServer
            Servlets
                                Pages (JSP)
     Java API for XML
                                 JavaMail         support for communication
     Processing (JAXP)
                                                  and presentation
       Java Authentication and Authorization
                  Service (JAAS)


      Enterprise Java        Java transaction
        Beans (EJB)             API (JTA)         support for the
      Java Message        Java Naming/Directory   application integration
      Service (JMS)         Interface (JNDI)



      Java DataBase         Java 2 Connector      support for access to
    Connectivity (JDBC)    Architecture (J2CA)
                                                  resource managers




CS530 S05
               JDBC Architecture


            ResultSet      ResultSet         ResultSet



                          Prepared            Callable
            Statement
                          Statement          Statement



                          Connection
                                                Application

                                           Service Provider


                        Service provider


CS530 S05
               JDBC Architecture II
                                      Application

                                  Service Provider
                     Connection



            Oracle   JDBC/ODBC       Sybase
            driver      bridge       driver



                       ODBC
            Oracle     driver        Sybase




                      ODBC DB



CS530 S05
                      Capabilities


        • Connections
          • pooling support
        • SQL queries
          • precompiled queries
          • database stored queries
        • Result sets
          • enumeration
          • scrollable

CS530 S05
                       Capabilities

        • Transaction control
          • auto-commit
          • explicit commit/rollback
          • savepoints
        • Isolation levels
          • READ_(UN)COMMITTED
          • REPEATABLE_READ
          • SERIALIZABLE
        • Distributed transactions
          • if supported by the database and driver
CS530 S05
              J2EE Application Server Architecture

                                JavaServer
            Servlets
                                Pages (JSP)
     Java API for XML
                                 JavaMail         support for communication
     Processing (JAXP)
                                                  and presentation
       Java Authentication and Authorization
                  Service (JAAS)


      Enterprise Java        Java transaction
        Beans (EJB)             API (JTA)         support for the
      Java Message        Java Naming/Directory   application integration
      Service (JMS)         Interface (JNDI)



      Java DataBase         Java 2 Connector      support for access to
    Connectivity (JDBC)    Architecture (J2CA)
                                                  resource managers




CS530 S05
                     Point-to-Point



            sender                    receiver




CS530 S05
                     Topic Based (pub/sub)


                                  receiver

                         ...
            sender                receiver

                         ...
                                  receiver




CS530 S05
                        Capabilities

        • Message filtering (Boolean predicates) at
            receiver
        •   Durable subscriptions if desired
        •   Transactional support
            • JMS queues only
            • or other resource managers as well




CS530 S05
              J2EE Application Server Architecture

                                JavaServer
            Servlets
                                Pages (JSP)
     Java API for XML
                                 JavaMail         support for communication
     Processing (JAXP)
                                                  and presentation
       Java Authentication and Authorization
                  Service (JAAS)


      Enterprise Java        Java transaction
        Beans (EJB)             API (JTA)         support for the
      Java Message        Java Naming/Directory   application integration
      Service (JMS)         Interface (JNDI)



      Java DataBase         Java 2 Connector      support for access to
    Connectivity (JDBC)    Architecture (J2CA)
                                                  resource managers




CS530 S05
                   What is a bean?

        • Java Object
        • Container provides many standard services
          • location / binding
          • life cycle
          • persistence
          • transactions
          • etc


CS530 S05
                   Classes of Beans

        • Session
          • embodies business logic associated with a
            session / business txn
        • Entity
          • represents a real-world object
        • Message-driven
          • asynchronous processing using JMS queues


CS530 S05
                       EJB Classes and Stubs
                                    home
                                  interface                create/find/
                                                             remove       container
                         home                  home
   create/find/remove      stub                  impl                           notify

                                                                            bean
      client                                          business methods/     impl
                                    client                 remove
                                  interface
   business methods/
        remove          client                client i/f
                         stub                   impl


                         client               server



CS530 S05
                     Session Beans


        • embodies business logic associated with a
          session’s / business txn
        • always given a session context
        • not persistent
        • stateful
          • invoked only thru creating session
        • stateless
          • use by concurrent sessions allowed

CS530 S05
                        Entity Beans


        • represents a real-world entity
        • may be shared by multiple sessions
        • persistent beyond session or EJB container
          lifetime
        • persistence may be bean-managed (JDBC/
            JTA) or container-managed




CS530 S05
             Bean-Managed Persistence

        • Container notifies bean on activation/
          passivation
        • Bean implementation responsible for
          serializing state to database
        • Bean implementation responsible for
          transaction behavior (JTA)
          • distributed txns may be supported


CS530 S05
            Container-Managed Persistence


        • Container manages serializing state to
          database and retrieving
        • Container manages transactional behavior
          • declarative



CS530 S05
            Declared Transaction Attributes

        • NotSupported
        • Supports
          • use txn if present
        • Required
          • create txn if none
        • RequiresNew
          • create txn
        • Mandatory
          • error if no txn
        • Never
CS530 S05
                    Managing a Transaction Context
                                                 Required

                           RequiresNew
        Client                                    EJB B
      Application                                           Register
                               EJB A
                                                Required     EJB B


                                                  EJB C
                    Register EJB A
                                       Register EJB C


                                     Transaction Manager




                                       EJB A     Transaction
                                       EJB B      Context
                                       EJB C

CS530 S05
               Message-Driven Beans


        • Communication by queueing (JMI) rather
          than RPC
        • Container manages
          • creation of MDB when message arrives
          • invoke onMessage method with each
            arriving message
          • remove idle beans
        • Equivalent to dequeue-process loop

CS530 S05
                      Message Driven Beans
                         Txn 1     Txn 2


                 MDB A             MDB B




             Entity      Session       Entity
            Bean C       Bean D        Bean E




CS530 S05
                     Deployment


        • XML deployment descriptors
          • identify programmer-provided code
          • dependencies
          • transactional behavior
          • security properties
        • Container generates stubs


CS530 S05

								
To top