Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Java Session 7 New York University School of Continuing and Professional Studies.

Similar presentations


Presentation on theme: "Advanced Java Session 7 New York University School of Continuing and Professional Studies."— Presentation transcript:

1

2 Advanced Java Session 7 New York University School of Continuing and Professional Studies

3 2 Objectives RMI –Overview –Example CORBA –Overview –Using CORBA in Java EJB –What is an EJB –Entity/Session Beans –Services

4 3 Remote Method Invocation A mechanism to invoke methods on a “remote” object and receive return values The object that makes the call is the “client” and the object that responds is the “server” Same object could be client for one call and server for another Heart of EJB and Distributed Applications Modern replacement for CORBA (and RPC)

5 4 Goal of RMI Make it easier to write distributed application Provide an interface that’s transparent Easy to understand Easy to use

6 5 Calling a Method on Remote Object ClientServer Method call

7 6 Role of RMI ClientServerstubrcvr

8 7 Stub Method Builds an information block consisting of –An identifier of the remote object to be used –A description of the method to be called –Marshalled parameters Sends this information to the server

9 8 Receiver Object Unmarshalls the parameters Locates the object to be called Calls the desired method Captures and marshals the return value or exception of the call Sends the package containing marshalled return value back to the stub on the client.

10 9 Stub Upon Return Unmarshalls the return values returned from the remote object Returns it back to the client

11 10 Classes and Interfaces for RMI Java.rmi package Interface that will be used by the client Implementation class – that must extend UnicastRemoteObject *and* implement the interface defined for remote clients

12 11 Parameter Marshalling RMI passes all parameters “by value” – it serializes the object and passes it However, if the Object extends “Remote” – a “stub” that represents it is passed Consider what happens when a Bank (server) returns an Account object (acct) to an ATM – and ATM updates the balance by calling acct.withdraw() All parameters to an RMI call must be Serializable or must extend Remote All parameters to an RMI call must be Serializable or must extend Remote

13 12 Parameter Marshalling All parameters to the remote method are marshalled by the client (stub) and unmarshalled by the server (rcvr) All return values are marshalled by the server (rcvr) and unmarshalled by the client (stub) Any exceptions thrown by the server are caught by the rcvr and marshalled back to the client stub which then throws them to the caller.

14 13 Setting up RMI Create and compile the interfaces and classes Use “rmic” to generate the stub and the receiver “glue” – no longer needed with JDK 5 Run “rmiregistry” service to provide registration/lookup services Launch the server – register the object Run the client – accesses remote method

15 14 RMI Examples MOTD with ShowMOTD.jsp RMIQuote with ShowQuotes.jsp Product Warehouse

16 15 Common Object Request Broker Architecture Proposed and maintained by OMG – Object Management Group Uses an IDL (Interface Definition Language) to define interfaces Uses an ORB (Object Request Broker) as the mediator between clients and servers.

17 16 CORBA advantages/disadvantages For accessing legacy applications Works across multiple programming language CORBA language bindings for scripting languages like Perl, Python Too complex Difficult to use

18 17 Writing CORBA clients/servers Write the interface that specifies how the object works using IDL Using the IDL compilers for the target language (idlj for java), generate the needed stub and helper classes Add the implementation code Write a server program that creates and registers the server object Write a client program that locates the server object and invokes the method Start the naming service (similar to rmiregistry), run the server program, and then run the client

19 18 Java and CORBA Packages Package org.omg.CORBA Package org.omg.CORBA_2_3 Classes org.omg.CORBA.ORB org.omg.CosNaming.NamingContext org.omg.CosNaming.NameComponent

20 19 Enterprise Java Beans Server-side component architecture Enables and simplifies the process of building enterprise-class distributed object applications that are scalable, reliable, and secure Analogous to the hardware components model

21 20 N-Tier Architecture Presentation logic Pricing component billing component database driver Presentation layer Business logic layer Data Layer

22 21 EJB Servers and Containers EJB EJB Container EJB Server Client (servlet, applet, app)

23 22 Enterprise Beans Session Beans –Typically represent business processes (like authorize credit card) –Last only through the duration of the client –Stateless Session Beans –Stateful Session Beans Entity Beans –Typically models permanent data objects – such as credit card, account, customer –Are long lasting, or persistent objects –Bean managed persistence –Container managed persistence

24 23 Enterprise Bean Model EJB EJB Server/container Client code Home Object EJB Object Get ref to Home Object create Method call delegate Home interface Remote interface

25 24 Services Provided by Container EJB EJB Server/Container Client (servlet, applet, app) invokes delegates Resource management Lifecycle management State management Transactions Security Persistence

26 25 Resource Management EJB Container is responsible for coordinating the entire effort of resource management for resources such as –Threads –Socket Connections –Database Connections

27 26 Lifecycle Management Controls the lifecycle of the Bean Creates or destroys beans in response to client requests Provides “instance pooling”

28 27 State Management Stateful beans or Entity Beans need to maintain a “state” for each clients The container provides services to maintain the state The same state may be passed on to another bean if necessary before invoking a method that a client requested

29 28 Transactions EJBs may participate in transactions EJB Container handles the underlying transaction operations, coordinating efforts behind the scenes. Java Transaction API is used to implement transactions for beans Variety of transaction management options are available

30 29 Security EJB containers add transparent Security to the Beans Enterprise Beans can automatically run as a certain security identity, or can programmatically ensure that clients are authorized to perform desired operations

31 30 Persistence Containers can provide transparent persistence for Entity Beans EJBs can manage their own persistence if they prefer

32 31 Remote Accessibility Location Transparency Converts a network-naïve component to a networked component Containers use Java RMI to specify remote accessibility Gives sysadmins the ability to upgrade a certain machine while clients are routed to another (better fault tolerance, availability)

33 32 Glue Code Bean Installation Tools Containers provide glue code tools. These tools are meant to integrate beans into the EJB container’s environment Glue code tools (deployment tools) are responsible for transforming an enterprise bean into a fully managed, distributed server-side component.

34 33 Specialized Container Features Integration to mainframe systems COM+ integration Transparent fail-over Stateful recovery Server clustering Dynamic redeployment Monitoring support Visual development environment integration

35 34 Bean classes and interfaces The EnterpriseBean class contains implementation details of your component. It extends Serializable. SessionBean – extends EnterpriseBean EntityBean – extends EnterpriseBean EJBObject class represents the “surrogate” object that is created instead of your Bean object Remote Interfaces for your bean must extend EJBObject EJBHome class represents the factory object Home Interfaces for your bean must extend EJBHome

36 35 Enterprise Bean Model EJB EJB Server/container Client code Home Object EJB Object Get ref to Home Object create Method call delegate Home interface Remote interface

37 36 EJBObject getEJBHome – retrieves a ref to corresponding Home object getPrimaryKey – returns the primary key for this object Remove – destroys this EJB object (and persistent store) getHandle – acquires a “handle” for this EJB obejct isIdentical – tests if two EJB Objects are identical

38 37 EJBHome getEJBMetaData – access info about the bean – e.g. whether it’s a session bean, or entity bean, it supports transactions or not etc. remove – destroys a particular EJB object create(…) methods – that are not part of the Interface EJBHome

39 38 SessionBean setSessionContext ejbPassivate ejbActivate ejbRemove ejbCreate(…) methods that are not part of the interface Other business methods

40 39 Bean client code Locate the Bean’s Home interface using JNDI Context ctx = new InitialContext(); IHome home = ctx.lookup(“MyHome”); Create an instance of the EJB Object IRemote remote = home.create(…); Call a business method using the Remote interface remote.authorizeCreditCard(…..);

41 40 Deployment Descriptor Bean home name Enterprise bean class name Home interface class name Remote interface class name Re-entrant Stateful or stateless Session timeout

42 41 Entity Beans Persistent objects Long-lived Survive failures Typically a view into a database Can be pooled Several Entity Beans may represent the same underlying data Can be created, removed or found

43 42 EntityBean interface setEntityContext unsetEntityContext ejbRemove ejbActivate ejbPassivate ejbLoad ejbStore ejbCreate is optional for entity beans and returns a primaryKey object ejbFindByPrimaryKey and other ejbFind methods

44 43 Thank you


Download ppt "Advanced Java Session 7 New York University School of Continuing and Professional Studies."

Similar presentations


Ads by Google