Download presentation
Presentation is loading. Please wait.
1
Broker in practica: Middleware
Tool Generare Proxy-uri Biblioteca(API)+ Executabile Application Developer
2
Implementari de referinta ale arhitecturii Broker
Middleware care implementeaza tiparul Broker: RMI: Java Remote Method Invocation CORBA: Common Object Request Broker Architecture. Arhitectura de referinta elaborata de OMG (Object Management Group) Diverse implementari, comerciale sau open .NET Remoting Windows Communication Foundation (WCF) – in parte si functionalitate de tip broker WSDL web services : proxy generators (WSDL-to-language)
3
Invokes methods of Remote Object via a local Proxy object
Arhitectura comuna Invokes methods of Remote Object via a local Proxy object Server Client implements Remote Interface Remote Object uses implements ClientSide Proxy ServerSide Proxy Broker
4
Exemplu Aplicatie distribuita simpla: realizarea unui sistem StockMarket, care poate fi interogat de la distanta asupra valorii curente a actiunilor unor companii specificate Middleware (Broker) utilizat - 3 variante: RMI .NET Remoting CORBA Se compara: Suportul oferit de cele 3 implementari de Broker pentru dezvoltarea unei aplicatii distribuite simple Pasii necesari pentru realizarea aplicatiei Exemplul ajuta la intelegerea functionarii unui Broker in general
5
Pasi de dezvoltare ai aplicatiei
1. Definire interfata obiect la distanta INTERFACE StockMarket float get_price(Company) 2. Implementare obiect 4. Implementare Server 5. Implementare Client StockMarketServer StockMarketClient StockMarketImpl 6. Rulare aplicatie 3. Generare Proxy 3. Generare Proxy StockMarket ClientSide PROXY StockMarket ServerSide PROXY Broker
6
RMI: Arhitectura Server Client implements Remote Interface Remote
Object uses implements Stub (ClientSide Proxy) Skeleton (ServerSide Proxy) Remote Reference Layer Transport Layer
7
RMI - Pas1. Definire interfata
Se defineste interfata StockMarket Aceasta exporta operatia get_price: Parametru: numele companiei cotate la bursa Rezultat: valoarea actiunilor package SimpleStocks; import java.rmi.*; public interface StockMarket extends java.rmi.Remote { float get_price (String Company) throws RemoteException; } Specific RMI: Interfata trebuie sa extinda java.rmi.Remote Metodele trebuie sa poata genera exceptii RemoteException Fisier StockMarket.java
8
RMI - Pas 2. Implementare Obiect
Se implementeaza clasa StockMarketImpl Aceasta realizeaza operatia get_price import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class StockMarketImpl extends UnicastRemoteObject implements StockMarket { public StockMarketImpl() throws RemoteException {} public float get_price(String company) { float price=12345; return price; } Specific RMI: Implementarea trebuie sa extinda UnicastRemoteObject Fisier StockMarketImpl.java
9
RMI - Pas 3.V1: Generare Traditionala Proxy-uri
RMI Stub/Skeleton Se genereaza automat din implementarea obiectului server cu ajutorul tool-ului rmic >rmic –v1.1 –keep StockMarketImpl Argumente: -v1.1 pentru ca incepand cu versiunea Java 1.2 implicit nu se mai genereaza skeleton-ul, de aceea e optiunea –v1.1 -keep ca sa pastreze si codul sursa al claselor generate (doar ca sa il putem vedea de curiozitate) Rezulta clase pentru stub si skeleton, utilizand conventia de nume: StockMarketImpl_Stub.class StockMarketImpl_Skel.class
10
RMI - Pas 4. Implementare Server
Se implementeaza un program StockMarketServer, care creaza un obiect de tip StockMarket, ce poate fi accesat de la distanta Obiectul creat este inregistrat sub numele “NASDAQ” import java.rmi.*; import SimpleStocks.*; public class StockMarketServer { public static void main(String[] args) { try { StockMarketImpl stockMarketImpl = new StockMarketImpl(); Naming.rebind("NASDAQ", stockMarketImpl ); } catch (Exception e) {} } Specific RMI: Naming: Obiectul Server este inregistrat cu numele “Nasdaq” Fisier StockMarketServer.java
11
RMI - Pas 5. Implementare Client
Se implementeaza un program StockMarketClient, care acceseaza un obiect de tip StockMarket aflat la distanta, si invoca operatia get_price pentru un nume de companie Prin intermediul Broker se localizeaza obiectul cu numele “NASDAQ” import java.rmi.*; import SimpleStocks.*; public class StockMarketClient { public static void main(String[] args) { try { StockMarket market= (StockMarket) Naming.lookup("rmi://localhost/NASDAQ"); float price=market.get_price("ABC SRL"); System.out.println("Price is "+price); } catch (Exception e) { System.out.println("Exception !");} } Specific RMI: Naming.lookup: se obtine o referinta (un proxy de fapt) la obiectul server care a fost inregistrat ca “Nasdaq” Nu trebuie sa gestionez deloc adrese ! Fisier StockMarketClient.java
12
RMI – To Do Checklist: Cod sursa scris de programatorul aplicatiei Stockmarket: StockMarket.java, StockMarketImpl.java, StockMarketServer.java, StockMarketClient.java Compilare cod sursa : javac *.java Generare proxy-uri: rmic –v1.1 StockMarketImpl Se obtin StockMarketImpl_Stub.class si StockMarketImpl_Skel.class Deployment Server: StockMarket.class, StockMarketImpl.class, StockMarketImpl_Skel.class, StockMarketServer.class Deployment Client: StockMarket.class, StockMarketImpl_Stub.class, StockMarketClient.class
13
RMI - Pas 6. Rulare aplicatie
RMI Registry = non-persistent Naming Service > start rmiregistry > start java StockMarketServer > java StockMarketClient Codul aplicatiei exemplu poate fi download-ad de pe pagina cursului, de la sectiunea de exemple de cod pentru cursul c7: RMI_StockMarket
14
RMI - Pas 3.V2: Generare Proxy - Doar Client
Varianta 2: In pasul 3 se genereaza doar RMI Stub Se genereaza automat din implementarea obiectului server >rmic StockMarketImpl Rezulta clase pentru stub, utilizand conventia de nume: StockMarketImpl_Stub.class Unde a disparut skeleton-ul ? (server-side proxy ?) - in acest caz, server-side proxy-ul generat specific aplicatiei este inlocuit cu unul general, care lucreaza exploatand faptul ca Java permite invocarea de metode prin Reflection
15
RMI – To Do Checklist – V2:
Cod sursa scris de programatorul aplicatiei Stockmarket: StockMarket.java, StockMarketImpl.java, StockMarketServer.java, StockMarketClient.java Compilare cod sursa : javac *.java Generare proxy-uri: rmic StockMarketImpl Se obtine StockMarketImpl_Stub.class Deployment Server: StockMarket.class, StockMarketImpl.class, StockMarketServer.class Deployment Client: StockMarket.class, StockMarketImpl_Stub.class, StockMarketClient.class Rulare aplicatie
16
RMI - Pas 3.V3: fara generare Proxy
Pasul 3 – Varianta 3: incepand cu versiunea Java 1.5, nu mai este necesara pre-generarea stub-ului cu rmic ! property java.rmi.server.ignoreStubClasses: daca e falsa, se asteapta sa gaseasca un stub pregenerat (by default e falsa) daca e true, nu mai e nevoie de existenta stub-urilor pre-generate cu rmic Unde a disparul si stub-ul ? (client-side proxy ?) de fapt el exista, dar poate fi generat dinamic la runtime, in mod total transparent pentru dezvoltatorul aplicatiei, prin mecanismul de dynamic proxy
17
RMI – To Do Checklist – V3:
Cod sursa scris de programatorul aplicatiei Stockmarket: StockMarket.java, StockMarketImpl.java, StockMarketServer.java, StockMarketClient.java Compilare cod sursa : javac *.java Generare proxy-uri: Nu se mai face nimic ! (dar va trebui setat un property la rulare!) Deployment Server: StockMarket.class, StockMarketImpl.class, StockMarketServer.class Deployment Client: StockMarket.class, StockMarketClient.class Rulare aplicatie
18
RMI - Pas 6 – Varianta 3: Rulare aplicatie
> start rmiregistry > start java StockMarketServer > java -Djava.rmi.server.ignoreStubClasses=true StockMarketClient
19
ORB (Object Request Broker)
CORBA: Arhitectura Server Client Remote Interface IDL Remote Object Dynamic Invocation Interface IDL Stub (ClientSide Proxy) IDL Skeleton (ServerSide Proxy) Object Adapter ORB Interface Implem Repository Interface Repository ORB (Object Request Broker)
20
CORBA - Pas1. Definire interfata
module SimpleStocks { interface StockMarket { float get_price(in string Company); }; Se defineste interfata StockMarket Aceasta exporta operatia get_price: Parametru: numele companiei cotate la bursa Rezultat: valoarea actiunilor Specific CORBA: Interfata se defineste intr-un limbaj neutru, CORBA IDL Fisier StockMarket.idl
21
CORBA - Pas 2. Generare Proxy-uri
CORBA IDL Stub/Skeleton Se genereaza automat din definitia interfetei IDL > idl2java StockMarket.idl Rezulta clasele/fisierele: Client Stub: _st_StockMarket.java Server Stub: _StockMarketImplBase.java
22
CORBA - Pas 3. Implementare Obiect
Se implementeaza clasa StockMarketImpl Aceasta realizeaza operatia get_price import org.omg.CORBA.*; import SimpleStocks.*; public class StockMarketImpl extends _StockMarketImplBase { public float get_price(in string company) { float price=12345; return price; } public StockMarketImpl (String name) { super(name); Specific CORBA: Un obiect accesibil la distanta trebuie sa extinda server-side-proxy-ul generat automat din descrierea interfetei Fisier StockMarketImpl.java
23
CORBA - Pas 4. Implementare Server
import org.omg.CORBA.*; Import SimpleStocks.*; public class StockMarketServer { public static void main(String[] args) { try { ORB orb=ORB.init(); BOA boa=orb.BOA_init(); StockMarketImpl stockMarketImpl= new StockMarketImpl(“NASDAQ”); boa.obj_is_ready(stockMarketImpl); boa. Impl_is_ready(); } catch (Exception e) {} } Se implementeaza un program StockMarketServer, care creaza un obiect de tip StockMarketImpl, ce poate fi accesat de la distanta Obiectul creat este inregistrat sub numele “NASDAQ” Specific CORBA: ORB BOA Fisier StockMarketServer.java
24
CORBA - Pas 5. Implementare Client
import org.omg.CORBA.*; import SimpleStocks.*; public class StockMarketClient { public static void main(String[] args) { try { ORB orb=ORB.init(); StockMarket market= StockMarketHelper.bind(orb, “NASDAQ”); market.get_price(“ABC SRL”); } catch (Exception e) {} } Se implementeaza un program StockMarketClient, care acceseaza un obiect de tip StockMarket aflat la distanta, si invoca operatia get_price pentru un nume de companie Prin intermediul Broker se localizeaza obiectul cu numele “NASDAQ” Specific CORBA: ORB StockMarketHelper = proxy generat Fisier StockMarketClient.java
25
CORBA - Pas 6. Rulare aplicatie
Location service (numele OSAgent e specific unei anumite implementari CORBA) > start osagent start java StockMarketServer start java StockMarketClient
26
.NET Remoting Architecture
Server Client Remote Interface Remote Object TransparentProxy RealProxy Remoting system Remoting system Channel
27
.NET – Pas 1. Definire Interfata Remote
public interface StockMarket { float get_price(string company); } Fisier StockMarket.cs Se defineste interfata StockMarket Aceasta exporta operatia get_price: Parametru: numele companiei cotate la bursa Rezultat: valoarea actiunilor Specific .NET: Interfata nu contine nimic special Nu este neaparat nevoie sa fie definita o interfata
28
.NET – Pas 2. Definire Obiect Remote
using System; public class StockMarketImpl : MarshalByRefObject, StockMarket{ public float get_price(string company){ return 12345; } Fisier StockMarketImpl.cs Se implementeaza clasa StockMarketImpl Aceasta realizeaza operatia get_price Specific .NET: Obiectul accesibil la distanta trebuie sa extinda MarshalByRefObject
29
.NET – Pas 3. Implementare Server
Se implementeaza un program StockMarketServer, care creaza un obiect de tip StockMarket, ce poate fi accesat de la distanta Obiectul creat este inregistrat sub numele “NASDAQ” using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; public class StockMarketServer{ public static void Main(){ StockMarketImpl myStockMarket = new StockMarketImpl(); TcpChannel m_TcpChan = new TcpChannel(9999); ChannelServices. RegisterChannel(m_TcpChan); RemotingServices.Marshal( myStockMarket, "NASDAQ"); System.Console.WriteLine( "Listening for requests. Press ENTER to quit"); System.Console.ReadLine(); } Specific .NET: ChannelServices RemotingServices Fisier StockMarketServer.cs
30
.NET – Pas 4. Implementare Client
Se implementeaza un program StockMarketClient, care acceseaza un obiect de tip StockMarket aflat la distanta, si invoca operatia get_price pentru un nume de companie Prin intermediul Broker se localizeaza obiectul cu numele “NASDAQ” using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; public class Client{ public static void Main(){ TcpChannel tcpChan = new TcpChannel(); ChannelServices.RegisterChannel(tcpChan); StockMarket stockMarket = (StockMarket) Activator.GetObject(typeof(StockMarket), "tcp://localhost:9999/NASDAQ"); Console.WriteLine(stockMarket.get_price( "abc SRL")); } Specific .NET: ChannelServices Activator.GetObject Fisier StockMarketClient.cs
31
.NET - Pas 5. Rulare aplicatie
> Presupunand ca Windows si .NET Framework este instalat: start StockMarketServer start StockMarketClient
32
.NET - Pas 6. Creare Proxy ? Crearea de Proxy este total transparenta pentru developer-ul de aplicatii Proxy se creaza automat de catre .NET Framework in timpul executiei cand un client activeaza un obiect la distanta.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.