400 likes | 545 Views
Advanced Java Session 7. New York University School of Continuing and Professional Studies. Objectives. RMI Overview Example CORBA Overview Using CORBA in Java EJB What is an EJB Entity/Session Beans Services. Remote Method Invocation.
E N D
Advanced JavaSession 7 New York University School of Continuing and Professional Studies
Objectives • RMI • Overview • Example • CORBA • Overview • Using CORBA in Java • EJB • What is an EJB • Entity/Session Beans • Services
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)
Goal of RMI • Make it easier to write distributed application • Provide an interface that’s transparent • Easy to understand • Easy to use
Calling a Method on Remote Object Client Server Method call
Role of RMI Client stub rcvr Server
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
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.
Stub Upon Return • Unmarshalls the return values returned from the remote object • Returns it back to the client
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
Setting up RMI • Create and compile the classes • Use “rmic” to generate the stub and the receiver “glue” • Run “rmiregistry” service to provide registration/lookup services • Launch the server – register the object • Run the client – accesses remote method
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.
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
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
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
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
N-Tier Architecture Presentation logic Presentation layer Pricing component billing component driver Business logic layer database Data Layer
EJB Servers and Containers Client (servlet, applet, app) EJB Server EJB Container EJB Container EJB EJB EJB EJB
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
Enterprise Bean Model Client code Get ref to Home Object EJB Server/container create Home Object Method call Home interface EJB Object delegate EJB Remote interface
Services Provided by Container Client (servlet, applet, app) EJB Server/Container invokes • Resource management • Lifecycle management • State management • Transactions • Security • Persistence delegates EJB
Resource Management EJB Container is responsible for coordinating the entire effort of resource management for resources such as • Threads • Socket Connections • Database Connections
Lifecycle Management Controls the lifecycle of the Bean Creates or destroys beans in response to client requests Provides “instance pooling”
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
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
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
Persistence • Containers can provide transparent persistence for Entity Beans • EJBs can manage their own persistence if they prefer
Remote AccessibilityLocation 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)
Glue CodeBean 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.
Specialized Container Features • Integration to mainframe systems • COM+ integration • Transparent fail-over • Stateful recovery • Server clustering • Dynamic redeployment • Monitoring support • Visual development environment integration
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
Enterprise Bean Model Client code Get ref to Home Object EJB Server/container create Home Object Method call Home interface EJB Object delegate EJB Remote interface
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
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
SessionBean • setSessionContext • ejbPassivate • ejbActivate • ejbRemove • ejbCreate(…) methods that are not part of the interface • Other business methods
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(…..);
Deployment Descriptor • Bean home name • Enterprise bean class name • Home interface class name • Remote interface class name • Re-entrant • Stateful or stateless • Session timeout
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
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