分布对象2.ppt

上传人:本田雅阁 文档编号:2544661 上传时间:2019-04-06 格式:PPT 页数:74 大小:1.83MB
返回 下载 相关 举报
分布对象2.ppt_第1页
第1页 / 共74页
分布对象2.ppt_第2页
第2页 / 共74页
分布对象2.ppt_第3页
第3页 / 共74页
亲,该文档总共74页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《分布对象2.ppt》由会员分享,可在线阅读,更多相关《分布对象2.ppt(74页珍藏版)》请在三一文库上搜索。

1、分布对象(2),Distributed Objects,2019/4/6,Institute of Computer Software Nanjing University,1,摘要,More about RMI J2EE/EJB,2019/4/6,Institute of Computer Software Nanjing University,2,摘要,More about RMI J2EE/EJB,2019/4/6,Institute of Computer Software Nanjing University,3,RMI,Java语言之内,充分利用这一点! Stub可下载! 可以传“

2、对象”! Garbage Collection! 传“引用” java.rmi.Remote (RemoteException),2019/4/6,Institute of Computer Software Nanjing University,4,2019/4/6,Institute of Computer Software Nanjing University,5,Some important parts of RMI,Stubs: Each remote object class has an associated stub class, which implements the sa

3、me remote interfaces. An instance of the stub class is needed on each client. Client-side remote invocations are “actually” local invocations on the stub class. Serialization: Arguments and results have to be “marshalled”converted to a representation that can be sent over the Net. In general this is

4、 a non-trivial transformation for Java objects. Serialization is also used for distributing stubs. The Server-side “Run-time System”: This is responsible for listening for invocating requests on suitable IP ports, and dispatching them to the proper, local resident, remote object.,2019/4/6,Institute

5、of Computer Software Nanjing University,6,RMI Architecture overview,RMI Layers Stub/skeleton layer objects used by client and server applications Remote reference layer creation/management of remote references distributed garbage collection Transport protocol layer binary data protocol,2019/4/6,Inst

6、itute of Computer Software Nanjing University,7,By using a layered architecture each layer could be enhanced or replaced without affecting the rest of the system: transport layer: UDP/IP layer or secure sockets (SSL).,Remote Reference Layer,RemoteRef Interprets and manages references to remote objec

7、ts. The stub objects use the invoke() method in RemoteRef to forward the method call. The RemoteRef object understands the invocation semantics for remote services. Leasing for distributed garbage collection Naming/Registry Service - rmiregistry,2019/4/6,Institute of Computer Software Nanjing Univer

8、sity,8,2019/4/6,Institute of Computer Software Nanjing University,9,Remote Reference Layer,Invocation Semantics v1.1: unicast / point-to-point. v1.2: support for activation of dormant remote service objects: Remote Object Activation RMI will instantiate a dormant object and restore its state from di

9、sk. As now: No multicast semantics.,Using RMI,1. Define interfaces for remote classes 2. Create and compile implementation of the remote classes 3. Create stub and skeleton classes using the rmic compiler No longer necessary in Java 1.5 because Java 1.5 adds support for the dynamic generation of stu

10、b classes at runtime. rmic must still be used to pre-generate stub classes for remote objects that need to support clients running on Java versions 1.4.,2019/4/6,Institute of Computer Software Nanjing University,10,Using RMI,4. Create and compile the server application (registration) 5. Create and c

11、ompile a client program to access the remote objects 6. Start the RMI Registry and the server application 7. Test the client,2019/4/6,Institute of Computer Software Nanjing University,11,Point 1 Remote Interface,Remote All remote interfaces must extend the interface java.rmi.Remote (tagging interfac

12、e) All methods must throw a java.rmi.RemoteException (extension of java.io.IOException),2019/4/6,Institute of Computer Software Nanjing University,12,Point 2 Implementation,UnicastRemoteObject Application must implement the defined remote interface Application extends class UnicastRemoteObject or ca

13、lls explicitly UnicastRemoteObject.exportObject link to RMI system base class performs RMI linking and remote object initialization constructor may throw a RemoteException Activatable Base class to be used for activatable objects 激活服务的目标:只有在需要时才启动服务进程,2019/4/6,Institute of Computer Software Nanjing

14、University,13,Dynamic stubs in Java 1.5,Dynamic Proxies Implemented using java.lang.reflect.Proxy (where the implementation is based on a RemoteObjectInvocationHandler) Dynamic proxy is only used if no pre-generated stub class is available or if the system property java.rmi.server.ignoreStubClasses

15、= true. It is only possible if clients run on Java 5. Notice : If a remote object has pre-1.5 clients, then that remote object should use a stub class pre-generated with rmic. There are two stub class. protocols: v1.1 / v1.2 (default).,2019/4/6,Institute of Computer Software Nanjing University,14,Po

16、int 3 Server,RMI service must be hosted in a server process whose job is: to create an instance; to register the object with the naming service. Naming/Registry service RMI can use different naming services: (i) Simple service: RMI Registry; (ii) JNDI (Java Naming and Directory interface).,2019/4/6,

17、Institute of Computer Software Nanjing University,15,Point 4 Start RMI Register,rmiregistry default port: 1099 error if port is already used by another process (e.g. another rmiregistry) daemon has to be started in directory which contains used classes or the classes have to be on the CLASSPATH Code

18、 Base: You must specify where are the class files. Security policy file: You must give permission to use port 1099.,2019/4/6,Institute of Computer Software Nanjing University,16,load classes dynamically,Required classes can be loaded over the network: e.g. provided by a web server via http; other pr

19、otocols are also possible (file:/, ftp:/, .) RMI class loading and security. Two conditions must be met: 1. a special class loader is provided: RMIClassLoader 2. a security manager has to support remote class loading System.setSecurityManager(new RMISecurityManager() Start of RMI-Registry in this ca

20、se: rmiregistry must not contain the needed classes in its path (otherwise what is the point of dynamically load the classes?),2019/4/6,Institute of Computer Software Nanjing University,17,load classes dynamically,Start of server: specify codebase for downloading class files java -Djava.rmi.server.c

21、odebase=http:/10.0.2.112:8080/calculator.jar nju.ics.yuping.dc.rmi.CalculatorServer Start of client: permission to access server has to be provided (due to security manager): java -Djava.security.policy=java.policy nju.ics.yuping.dc.rmi.CalculatorClient Policy file: grant / connect to or accept conn

22、ections on unprivileged ports / (ports greater than 1024) on host loki.cs.fh-aargau.ch permission .SocketPermission ”10.0.2.112:2001-“, “connect,resolve“; ;,2019/4/6,Institute of Computer Software Nanjing University,18,Point 5 - Codebase,2019/4/6,Institute of Computer Software Nanjing University,19,

23、-Djava.rmi.server.codebase=file:/e:coursecodermiserver -Djava.rmi.server.codebase=http:/10.0.2.112:8080/calculator.jar,Point 6 - Marshalling,How are parameters transferred to remote objects? Primitive Parameters passed by value, in a machine-independent format Serializable Objects serializable objec

24、ts are copied call by value Remote Object Parameters only the reference to the remote object is passed, i.e. a new proxy is generated call by reference Non-Serializable/Remote Objects cannot be transferred checked at runtime (not by rmic!),2019/4/6,Institute of Computer Software Nanjing University,2

25、0,Note on passing remote objects,Remote objects are commonly defined as parameters and return types. Example of usage: Callbacks Factory classes that create remote references Reminder: Remote objects are passed by reference. When Remote exported objects are passed to a client, RMI substitutes the re

26、ference with that of the Remote proxy (stub).,2019/4/6,Institute of Computer Software Nanjing University,21,Callbacks,In many cases, applications require more complex bi-directional interactions. Servers may wish to make calls to the client (this is known as a callback). Why? Error or problem report

27、ing Periodic updating and progress reports UI notification (Observer pattern ! ) In OO programs the role of clients and servers are not always clear cut. Client-server applications often operate in a peer-to-peer manner. At different stages an object may either act as a server or as a client.,2019/4

28、/6,Institute of Computer Software Nanjing University,22,Callback: How to,How do you create a callback? Make your client into a server! 1. Make your client implement a Remote interface: Define a client remote interface 2. Make it available as a server (export your client interface as a Remote object)

29、 extend UnicastRemoteObject or use UnicastRemoteObject.exportObject(Remote) 3. Pass a client Remote reference to the server. The server can then use this reference to make calls on the client.,2019/4/6,Institute of Computer Software Nanjing University,23,Callback Example,2019/4/6,Institute of Comput

30、er Software Nanjing University,24,摘要,More about RMI J2EE/EJB,2019/4/6,Institute of Computer Software Nanjing University,25,J2EE,JDBC JNDI EJB RMI Java IDL/CORBA JSP Java Servlet,2019/4/6,Institute of Computer Software Nanjing University,26,XML JMS JTA JavaMail JAF,Application Servers,“The Multi- tie

31、r applications“ have several independent components An application server provides the infrastructure and services to run such applications Application server products can be separated into 3 categories: J2EE-based solutions Non-J2EE solutions (PHP, ColdFusion, Perl, etc.) And the Microsoft solution

32、 (ASP/COM and now .NET with ASP.NET, VB.NET, C#, etc.),2019/4/6,Institute of Computer Software Nanjing University,27,J2EE Application Servers,Major J2EE products: BEA WebLogic IBM WebSphere Borland AppServer Sun/Oracle GlassFish JBoss,2019/4/6,Institute of Computer Software Nanjing University,28,Web

33、 Server and Application Server,2019/4/6,Institute of Computer Software Nanjing University,29,Web Server (HTTP Server),App Server 1,App Server 2,Internet Browser,HTTP(S),J2EE Multi-tier Model,2019/4/6,Institute of Computer Software Nanjing University,30,J2EE Application Scenarios,Multi-tier typical a

34、pplication,2019/4/6,Institute of Computer Software Nanjing University,31,J2EE Application Scenarios,Stand-alone client,2019/4/6,Institute of Computer Software Nanjing University,32,J2EE Application Scenarios,Web-centric application,2019/4/6,Institute of Computer Software Nanjing University,33,J2EE A

35、pplication Scenarios,Business-to-business,2019/4/6,Institute of Computer Software Nanjing University,34,J2EE Architecture,2019/4/6,Institute of Computer Software Nanjing University,35,Now,JEE 5 JEE 6 Homepage: http:/ of Computer Software Nanjing University,36,Main technologies,JavaServer Pages (JSP)

36、 Servlet Enterprise JavaBeans (EJB) JSPs, servlets and EJBs are application components,2019/4/6,Institute of Computer Software Nanjing University,37,JSP,Used for web pages with dynamic content Processes HTTP requests (non-blocking call-and-return) Accepts HTML tags, special JSP tags, and scriptlets

37、of Java code Separates static content from presentation logic Can be created by web designer using HTML tools,2019/4/6,Institute of Computer Software Nanjing University,38,Servlet,Used for web pages with dynamic content Processes HTTP requests (non-blocking call-and-return) Written in Java; uses pri

38、nt statements to render HTML Loaded into memory once and then called many times Provides APIs for session management,2019/4/6,Institute of Computer Software Nanjing University,39,EJB,EJBs are distributed components used to implement business logic (no UI) Developer concentrates on business logic Ava

39、ilability, scalability, security, interoperability and integrability handled by the J2EE server Client of EJBs can be JSPs, servlets, other EJBs and external aplications Clients see interfaces,2019/4/6,Institute of Computer Software Nanjing University,40,EJB,EJB 1.1 EJB 2.0 EJB 2.1 EJB 3.0 EJB 3.1,2

40、019/4/6,Institute of Computer Software Nanjing University,41,更简单,EJB技术要解决的问题,EJB 技术的初衷是简化企业级应用的开发 通过EJB容器环境 提供可共享的服务: Concurrency,Distribution, Transactions, EIS integration, Resource pooling, Security, Persistence 提供EJB运行环境 减轻企业级应用开发人员的负担,2019/4/6,Institute of Computer Software Nanjing University,4

41、2,EJB的相关概念,EJB容器:由厂商提供的位于EJB服务器上的实体,管理EJB的系统级服务,控制EJB的生命周期。针对每一种组件,都有相应的容器进行处理。 EJB服务器:作为容器和低层平台的桥梁管理着EJB容器和函数,向EJB容器提供了访问系统服务的能力。 EJB接口(2.x):Home接口,Remote接口,Local和LocalHome接口,2019/4/6,Institute of Computer Software Nanjing University,43,EJB分类,Session Bean Stateless Stateful Entity Bean Message Driv

42、en Bean,2019/4/6,Institute of Computer Software Nanjing University,44,同步通信,异步通信,Session Bean,Stateless session bean: Contains no user-specific data Business process that provides a generic service Container can pool stateless beans Example: shopping catalog,2019/4/6,Institute of Computer Software Na

43、njing University,45,Session Bean,Stateful session bean: Retains conversational state (data) on behalf of an individual client If state changed during this invocation, the same state will be available upon the following invocation Example: shopping cart,2019/4/6,Institute of Computer Software Nanjing

44、 University,46,Entity Bean,Represents business data stored in a database persistent object Underlying data is normally one row of a table A primary key uniquely identifies each bean instance Allows shared access from multiple clients Can live past the duration of client s session Example: shopping o

45、rder,2019/4/6,Institute of Computer Software Nanjing University,47,Message-Driven Bean,Message consumer for a JMS queue or topic Benefits from EJB container services that are not available to standard JMS consumers Has no home or remote interface Example: Order processing stock info,2019/4/6,Institu

46、te of Computer Software Nanjing University,48,EJB 2.x的问题,APIs设计的角度不对,多是为了容器正常工作设计,较少考虑易用性 EJBHome interface EJBObject interface EnterpriseBean interfaces JNDI interfaces Deployment descriptor 解决了老问题,引入了新问题 程序复杂,臃肿,普及度不高。 Boiler Code 部署脚本,2019/4/6,Institute of Computer Software Nanjing University,49,

47、简单的EJB 2.x例子,/ EJB 2.1 Stateless Session Bean: Bean Class public class PayrollBean implements javax.ejb.SessionBean SessionContext ctx; DataSource payrollDB; public void setSessionContext(SessionContext ctx) this.ctx = ctx; public void ejbActivate() public void ejbPassivate() public void ejbRemove()

48、 ,2019/4/6,Institute of Computer Software Nanjing University,50,简单的EJB 2.x例子,/ EJB 2.1 Stateless Session Bean: Bean Class (continued) public void ejbCreate() . Context initialCtx = new InitialContext(); payrollDB = (DataSource)initialCtx.lookup (“java:com/env/jdbc/empDB”); . public void setTaxDeduct

49、ions(int empId,int deductions) . Connection conn = payrollDB.getConnection(); Statement stmt = conn.createStatement(); . ,2019/4/6,Institute of Computer Software Nanjing University,51,简单的EJB 2.x例子,/ EJB 2.1 Stateless Session Bean: Interfaces public interface PayrollHome extends javax.ejb.EJBLocalHome public Payroll create() throws CreateException; public interface Payroll extends javax.ejb.EJBLocalObject p

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1