session7 - Advanced Java
Download
Report
Transcript session7 - Advanced Java
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
2
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)
3
Goal of RMI
• Make it easier to write distributed
application
• Provide an interface that’s transparent
• Easy to understand
• Easy to use
4
Calling a Method on Remote
Object
Client
Server
Method call
5
Role of RMI
Client
stub
rcvr
Server
6
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
7
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.
8
Stub Upon Return
• Unmarshalls the return values returned from
the remote object
• Returns it back to the client
9
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
10
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
11
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.
12
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
13
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
14
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
15
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
16
N-Tier Architecture
Presentation
logic
Presentation layer
Pricing
component
billing
component
driver
database
Business logic layer
Data Layer
17
EJB Servers and Containers
Client (servlet, applet, app)
EJB Server
EJB Container
EJB Container
EJB
EJB
EJB
EJB
18
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
19
Enterprise Bean Model
Client
code
EJB Server/container
create
Home Object
Method call
Home
interface
EJB Object
Remote
interface
delegate
EJB
20
Services Provided by Container
Client (servlet, applet, app)
EJB Server/Container
invokes
•Resource management
•Lifecycle management
delegates
EJB
•State management
•Transactions
•Security
•Persistence
21
Resource Management
EJB Container is responsible for coordinating the
entire effort of resource management for resources
such as
– Threads
– Socket Connections
– Database Connections
22
Lifecycle Management
Controls the lifecycle of the Bean
Creates or destroys beans in response to client
requests
Provides “instance pooling”
23
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
24
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
25
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
26
Persistence
• Containers can provide transparent persistence for
Entity Beans
• EJBs can manage their own persistence if they
prefer
27
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)
28
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.
29
Specialized Container Features
•
•
•
•
•
•
•
•
Integration to mainframe systems
COM+ integration
Transparent fail-over
Stateful recovery
Server clustering
Dynamic redeployment
Monitoring support
Visual development environment integration
30
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
31
Enterprise Bean Model
Client
code
EJB Server/container
create
Home Object
Method call
Home
interface
EJB Object
Remote
interface
delegate
EJB
32
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
33
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
34
SessionBean
• setSessionContext
• ejbPassivate
• ejbActivate
• ejbRemove
• ejbCreate(…) methods that are not part of the
interface
• Other business methods
35
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(…..);
36
Deployment Descriptor
• Bean home name
• Enterprise bean class name
• Home interface class name
• Remote interface class name
• Re-entrant
• Stateful or stateless
• Session timeout
37
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
38
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
39
Thank you
40