1 / 74

Distributed Objects

Distributed Objects. 分布对象 (2). 摘要. More about RMI J2EE/EJB. 摘要. More about RMI J2EE/EJB. RMI. Java 语言之内,充分利用这一点! Stub 可下载! 可以传“对象”! Garbage Collection! 传“引用” java.rmi.Remote (RemoteException). Some important parts of RMI. Stubs:

Download Presentation

Distributed Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Distributed Objects 分布对象(2) Institute of Computer Software Nanjing University

  2. 摘要 • More about RMI • J2EE/EJB Institute of Computer Software Nanjing University

  3. 摘要 • More about RMI • J2EE/EJB Institute of Computer Software Nanjing University

  4. RMI • Java语言之内,充分利用这一点! • Stub可下载! • 可以传“对象”! • Garbage Collection! • 传“引用” • java.rmi.Remote (RemoteException) Institute of Computer Software Nanjing University

  5. Institute of Computer Software Nanjing University

  6. Some important parts of RMI • Stubs: • Each remote object class has an associated stub class, which implements the same 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 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. Institute of Computer Software Nanjing University

  7. 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 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). Institute of Computer Software Nanjing University

  8. Remote Reference Layer • RemoteRef • Interprets and manages references to remote objects. The stub objects use the invoke() method in RemoteRefto forward the method call. The RemoteRefobject understands the invocation semantics for remote services. • Leasing for distributed garbage collection • Naming/Registry Service -- rmiregistry 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 disk. • As now: No multicast semantics. Institute of Computer Software Nanjing University

  10. 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 stub 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. Institute of Computer Software Nanjing University

  11. Using RMI 4. Create and compile the server application (registration) 5. Create and compile a client program to access the remote objects 6. Start the RMI Registry and the server application 7. Test the client Institute of Computer Software Nanjing University

  12. Point 1 – Remote Interface • Remote • All remote interfaces must extend the interface java.rmi.Remote (tagging interface) • All methods must throw a java.rmi.RemoteException (extension of java.io.IOException) Institute of Computer Software Nanjing University

  13. Point 2 – Implementation • UnicastRemoteObject • Application must implement the defined remote interface • Application extends class UnicastRemoteObject or calls 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 • 激活服务的目标:只有在需要时才启动服务进程 Institute of Computer Software Nanjing University

  14. 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 = 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). Institute of Computer Software Nanjing University

  15. Point 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). Institute of Computer Software Nanjing University

  16. Point 4 – Start RMI Register • rmiregistry <port> • 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 Base: You must specify where are the class files. • Security policy file: You must give permission to use port 1099. Institute of Computer Software Nanjing University

  17. load classes dynamically • Required classes can be loaded over the network: • e.g. provided by a web server via http; • other protocols 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 case: • rmiregistry must not contain the needed classes in its path (otherwise what is the point of dynamically load the classes?) Institute of Computer Software Nanjing University

  18. load classes dynamically • Start of server: • specify codebase for downloading class files • java -Djava.rmi.server.codebase=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 connections on unprivileged ports // (ports greater than 1024) on host loki.cs.fh-aargau.ch permission java.net.SocketPermission ”10.0.2.112:2001-", "connect,resolve"; }; Institute of Computer Software Nanjing University

  19. Point 5 - Codebase -Djava.rmi.server.codebase=file:///e:\course\code\rmi\server\ -Djava.rmi.server.codebase=http://10.0.2.112:8080/calculator.jar Institute of Computer Software Nanjing University

  20. Point 6 - Marshalling • How are parameters transferred to remote objects? • Primitive Parameters • passed by value, in a machine-independent format • Serializable Objects • serializable objects 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!) Institute of Computer Software Nanjing University

  21. 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 reference with that of the Remote proxy (stub). Institute of Computer Software Nanjing University

  22. 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 reporting • → 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. Institute of Computer Software Nanjing University

  23. 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) → 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. Institute of Computer Software Nanjing University

  24. Callback Example Institute of Computer Software Nanjing University

  25. 摘要 • More about RMI • J2EE/EJB Institute of Computer Software Nanjing University

  26. J2EE • JDBC • JNDI • EJB • RMI • Java IDL/CORBA • JSP • Java Servlet • XML • JMS • JTA • JavaMail • JAF Institute of Computer Software Nanjing University

  27. Application Servers • "The Multi- tier 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 (ASP/COM and now .NET with ASP.NET, VB.NET, C#, etc.) Institute of Computer Software Nanjing University

  28. J2EE Application Servers • Major J2EE products: • BEA WebLogic • IBM WebSphere • Borland AppServer • Sun/Oracle GlassFish • JBoss Institute of Computer Software Nanjing University

  29. Web Server and Application Server App Server 1 Internet Browser Web Server(HTTP Server) HTTP(S) App Server 2 Institute of Computer Software Nanjing University

  30. J2EE Multi-tier Model Institute of Computer Software Nanjing University

  31. J2EE Application Scenarios • Multi-tier typical application Institute of Computer Software Nanjing University

  32. J2EE Application Scenarios • Stand-alone client Institute of Computer Software Nanjing University

  33. J2EE Application Scenarios • Web-centric application Institute of Computer Software Nanjing University

  34. J2EE Application Scenarios • Business-to-business Institute of Computer Software Nanjing University

  35. J2EE Architecture Institute of Computer Software Nanjing University

  36. Now • JEE 5 • JEE 6 • Homepage: http://www.oracle.com/technetwork/java/javaee/tech/index-jsp-142185.html Institute of Computer Software Nanjing University

  37. Main technologies • JavaServer Pages (JSP) • Servlet • Enterprise JavaBeans (EJB) • JSPs, servlets and EJBs are application components Institute of Computer Software Nanjing University

  38. JSP • Used for web pages with dynamic content • Processes HTTP requests (non-blocking call-and-return) • Accepts HTML tags, special JSP tags, and scriptlets of Java code • Separates static content from presentation logic • Can be created by web designer using HTML tools Institute of Computer Software Nanjing University

  39. Servlet • Used for web pages with dynamic content • Processes HTTP requests (non-blocking call-and-return) • Written in Java; uses print statements to render HTML • Loaded into memory once and then called many times • Provides APIs for session management Institute of Computer Software Nanjing University

  40. EJB • EJBs are distributed components used to implement business logic (no UI) • Developer concentrates on business logic • Availability, 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 Institute of Computer Software Nanjing University

  41. EJB • EJB 1.1 • EJB 2.0 • EJB 2.1 • EJB 3.0 • EJB 3.1 更简单 Institute of Computer Software Nanjing University

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

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

  44. EJB分类 • Session Bean • Stateless • Stateful • Entity Bean • Message Driven Bean 同步通信 异步通信 Institute of Computer Software Nanjing University

  45. 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 Institute of Computer Software Nanjing University

  46. 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 Institute of Computer Software Nanjing University

  47. 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 order Institute of Computer Software Nanjing University

  48. 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 Institute of Computer Software Nanjing University

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

  50. 简单的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() {} Institute of Computer Software Nanjing University

More Related