Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on...

35
Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI

Transcript of Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on...

Page 1: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

Middleware Technology (J2EE/EJB)RMI-IIOP/JNDI

Page 2: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

2

Distributed Objects

• EJB components are based on distributed objects. A distributed object can be called from an in-process client, an out-of-process client, or a client located elsewhere on the network.

• Three different technologies OMG’s CORBA (Common Object Request Broker Architecture)

Microsoft’s DCOM Sun’s Java RMI-IIOP

Page 3: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

3

Distributed Object

Page 4: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

4

Client

Object Reference

Stub

Servant

Skeleton

Server

network

Client and server are collocated in different address space.

Page 5: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

5

Client and Server

Object Reference

Proxy

Servant

Client and server are collocated in the same address space.

Location Transparency

No changes to the source code are necessary in either client or server.

Page 6: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

6

Distributed Objects and Middleware

• Explicit Middleware (transaction, security, etc.)

Difficult to writeDifficult to maintainDifficult to support

Page 7: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

7

Explicit Middleware (gained through API)

Page 8: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

8

Distributed Objects and Middleware

• Implicit Middleware (Recommended) JTS

Instead of writing any code to middleware APIs, you declare what you need in a simple text file. The request interceptor provide the middleware logic for you transparently

Page 9: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

9

Implicit Middleware (gained through declaration)

Page 10: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

10

RMI-IIOP and JNDI

• EJB technology depends on Java RMI-IIOP and Java Naming and Directory Interface (JNDI)

• Your J2EE server implementation should ship with RMI-IIOP and JNDI implementations. It is generally a bad idea to mix and match implementations, like combining Sun’s RMI-IIOP packages with BEA’s JNDI implementations.

• RMI-IIOP (RMI over Internet Inter-ORB Protocol) allows you to write distributed objects in Java, enabling objects to communicate in memory, across JVM, and across physical devices.

RMI java.rmi package

RMI-IIOP java.rmi, javax.rmi package

Page 11: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

11

Remote Method Invocations

• Compared to RPC (Remote Procedure Call), RMI-IIOP yields the benefits of an object-oriented programming, such as inheritance, encapsulation and polymorphism.PRC: Inter-Process CommunicationRMI-IIOP: Distributed Object Communication, platform-

independent

• Remote Method Invocation Marshalling and Unmarshalling Parameter passing conventions (pass-by-value or pass-by-referenc

e) Network or machine instability

A crash of a single JVM should not cause the distributed system to grind to a halt. (RemoteException)

(See also: Figure A.1 page 494)

Page 12: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

12

Page 13: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

13

The Remote Interface

• In RMI-IIOP, all networking code you write is applied to interfaces, not implementations. You can operate solely on the interface to that object’s class.

import java.rmi.Remote;import java.rmi.RemoteException;

public interface IPKGenerator extends Remote { public long generate() throws RemoteException;}

interface f1 {} class c1 implements f1 {} f1 ff = new c1(); //upcasting, safety

Page 14: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

14

The Remote Object Implementation

• To make your object a remote object available to be invoked on by remote hosts, you remote class must perform one of the following steps: Extend the class javax.rmi.PortableRemoteObject (In the sample, we use RM

I, so the super class is changed to java.rmi.UnicastRemoteObject) Don’t extend javax.rmi.PortableRemoteObject. To export your object, call ja

vax.rmi.PortableRemoteObject.exportObject(). (*)

• Running the sample IPKGeneratorRMI_PKGenerator/readme.txt

Page 15: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

15

Page 16: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

16

Object Serialization and Parameter Passing

• Pass-by-valueWhen invoking a method using RMI-IIOP, all parameters are copied from client to server.If you are trying to pass an object over the network and that object contains reference to other objects, how are those references resolved on the target machine? *

• Object SerializationRMI-IIOP uses object serialization to send parameters over the network. Serialization is the conversion of a Java object into a bit-blob representation of that object.

writeObject: SerializationreadObject: Deserialization (bit-blob back into a object)

You can provide your own custom serialization by implementing the methods above, such as some sort of compression

Page 17: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

17

Rules for serialization

• The rules are effective to member variables held in serialized objects

Page 18: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

18

Object Serialization

• When you pass a object as a parameter, if the object contains non-transient sub-objects, the object serialization process will be repeated recursively for every object until the entire reference graph of objects is serialized.

Page 19: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

19

Why Transient

• The object is very large, which may not be suitable for serialization. Object serialization is a heavyweight operation for large graphs of objects. *

• The object represents a resource that cannot be reconstructed on the target machine. (database connection)

• The object represents sensitive information that you do not want to pass in a serialized stream.

Page 20: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

20

Object Serialization and RMI-IIOP

• RMI-IIOP relies on object serialization for passing parameters via remote method invocations.

• pass-by-referenceRMI-IIOP simulates a pass-by-reference convention, which means the arguments are not copied over. If you want to pass a parameter by reference, the parameter must itself be a remote object.

Running sample RMI and RMI-IIOPRMI/readme.txt RMI-IIOP/readme.txt

Page 21: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

21

Remote Object

pass-by-reference

RMI demo

Page 22: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

22

RMI-IIOP demo

Page 23: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

23

RMI-IIOP and Object Serialization

Page 24: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

24

Remote Object and Pass by Reference

Page 25: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

25

Java Naming and Directory Interface

• JNDI is the standard way of looking things up over the network. JNDI is used in EJB, RMI-IIOP, JDBC, and more.

• You can use JNDI to locate a printer on your intranet. You can also use it to locate a Java object or to connect with a database.

• JNDI is a J2EE API that provides a standard interface for locating users, machines, networks, objects, and services.

Page 26: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

26

Naming Service

• A naming service is analogous to a telephone operator. P506

• A naming service performs the following tasks: It associates names with objects. (bind) It provides a facility to find an object based on a name. (lookup)

– Examples of naming services

DNS (Domain Name System): translating a machine name to an IP address

File System: translating a filename into an actual file of data

Page 27: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

27

Directory Service

• A directory object differs from a generic object because you can store attributes with directory objects. (API:javax.naming)

• You can use a directory object to represent a user and store information about that user, such as login name, password, email address, phone number, and postal address, etc.

• A directory service is a naming service that has been enhanced and extended to provide directory object operations for manipulating attributes.

• Examples: Netscape Directory Server, Microsoft Active Directory

Page 28: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

28

A Hierarchical Directory Structure

• The directory’s contents-the set of connected directory objects-usually forms a hierarchical tree-like structure.

• Benefits of JNDI (4 items)

• In J2EE, you can use JNDI for many purposes.

Page 29: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

29

JNDI Architecture

• JNDI is made up of two halves: the client API and the Service Provider Interface (SPI).

• The client API allows your Java code to perform directory operations. This API is uniform for all types of directories. (similar to JDBC’API)

• The JNDI SPI is an interface to which naming and directory service vendors can plug in. The SPI allows naming and directory service vendors to fit their particular proprietary protocols into the system.

Page 30: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

30

JNDI Architecture

LDAP: Lightweight Directory Access Protocol

NIS: Network Information System

NDS: Novell’s Network Directory System *

Page 31: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

31

JNDI Concepts

• Atomic Namesimple, basic, indivisible component of a nameFor example, in the string /etc/fstab, etc and fstab are atomic names.

• Compound Namezero or more atomic names put togetherIn the previous example, the entire string /etc/fstab is a component name.

• Binding an association of a name with an object/etc/fstab consists of multiple bindings, one to etc, one to fstab

• Context containing zero or more bindings. Each binding has a distinct atomic name. For an instance, /usr/bin, /usr/local (in UNIX), /usr folder is a context which contains two distinct atomic names, bin and local. Each of these atomic names is bound to a subfolder, we can call them subcontext in JNDI terms.

Page 32: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

32

Naming System, Namespace, and Composite Name

• Naming SystemA naming system is a connected set of contexts. Each naming system has a different syntax for accessing contexts.

For example, in an LDAP tree, a compound name is identified by a string such as:cn=Ed Roman, ou=People, o=Middleware-Company.com, c=us

whereas a file system compound name might look like c:\java\lib\tools.jar.

Page 33: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

33

Naming System, Namespace, and Composite Name

• NamespaceA namespace is all the names contained within naming system.

• Composite NameA composite name is a name that spans multiple naming system.

http://java.sun.com/products/J2SDK

Schema-id namespaceDNS namespace File System namespace

Page 34: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

34

Initial Context Factory

• The starting point of exploring a namespace is called an initial context. An initial context is a starting point for performing all naming and directory operations.

• To acquire an initial context, you use an initial context factory. An initial context factory basically is your JNDI driver, such as LDAP initial context factory, file system initial context factory, and others. These initial context factories know the specific semantics of a particular directory structure.

Demo: InitCtx/readme.txt

Page 35: Middleware Technology (J2EE/EJB) RMI-IIOP/JNDI. 2 Distributed Objects EJB components are based on distributed objects. A distributed object can be called.

35

Example of a Composite Name