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

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

Embed Size (px)

Citation preview

Advanced JavaSession 7

New York University

School of Continuing and Professional Studies

2

Objectives

• RMI– Overview

– Example

• CORBA– Overview

– Using CORBA in Java

• EJB– What is an EJB

– Entity/Session Beans

– Services

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)

4

Goal of RMI

• Make it easier to write distributed application

• Provide an interface that’s transparent

• Easy to understand

• Easy to use

5

Calling a Method on Remote Object

Client ServerMethod call

6

Role of RMI

Client Serverstub rcvr

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

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.

9

Stub Upon Return

• Unmarshalls the return values returned from the remote object

• Returns it back to the client

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

11

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

12

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.

13

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

14

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

15

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

16

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

17

N-Tier ArchitecturePresentation

logic

Pricing component

billing component

database

driver

Presentation layer

Business logic layer

Data Layer

18

EJB Servers and Containers

EJB

EJB

EJB

EJB

EJB Container EJB Container

EJB Server

Client (servlet, applet, app)

19

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

20

Enterprise Bean Model

EJB

EJB Server/container

Client code

Home Object

EJB Object

Get ref to Home Object

create

Method call

delegate

Homeinterface

Remoteinterface

21

Services Provided by Container

EJB

EJB Server/Container

Client (servlet, applet, app)

invokes

delegates•Resource management

•Lifecycle management

•State management

•Transactions

•Security

•Persistence

22

Resource ManagementEJB Container is responsible for coordinating the entire effort of resource management for resources such as

– Threads

– Socket Connections

– Database Connections

23

Lifecycle ManagementControls the lifecycle of the Bean

Creates or destroys beans in response to client requests

Provides “instance pooling”

24

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

25

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

26

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

27

Persistence• Containers can provide transparent persistence for

Entity Beans• EJBs can manage their own persistence if they

prefer

28

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)

29

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.

30

Specialized Container Features

• Integration to mainframe systems• COM+ integration• Transparent fail-over• Stateful recovery• Server clustering• Dynamic redeployment• Monitoring support• Visual development environment integration

31

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

32

Enterprise Bean Model

EJB

EJB Server/container

Client code

Home Object

EJB Object

Get ref to Home Object

create

Method call

delegate

Homeinterface

Remoteinterface

33

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

34

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

35

SessionBean

• setSessionContext

• ejbPassivate

• ejbActivate

• ejbRemove

• ejbCreate(…) methods that are not part of the interface

• Other business methods

36

Bean client code

• Locate the Bean’s Home interface using JNDIContext 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(…..);

37

Deployment Descriptor

• Bean home name

• Enterprise bean class name

• Home interface class name

• Remote interface class name

• Re-entrant

• Stateful or stateless

• Session timeout

38

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

39

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

40

Thank you