Cover image for Java RMI

Book description

Java RMI contains a wealth of experience in designing and implementing Java's Remote Method Invocation. If you're a novice reader, you will quickly be brought up to speed on why RMI is such a powerful yet easy to use tool for distributed programming, while experts can gain valuable experience for constructing their own enterprise and distributed systems. With Java RMI, you'll learn tips and tricks for making your RMI code excel. The book also provides strategies for working with serialization, threading, the RMI registry, sockets and socket factories, activation, dynamic class downloading, HTTP tunneling, distributed garbage collection, JNDI, and CORBA. In short, a treasure trove of valuable RMI knowledge packed into one book.

Table of Contents

  1. Java RMI
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Preface
      1. About This Book
        1. Part I
        2. Part II
        3. Part III
      2. About the Example Code
      3. Conventions Used in This Book
        1. Coding Conventions
        2. Applications
        3. Compiling and Building
        4. Downloading the Source Examples
      4. For Further Information
      5. How to Contact Us
      6. Acknowledgments
    4. I. Designing and Building: The Basics of RMI Applications
      1. 1. Streams
        1. The Core Classes
          1. InputStream
            1. Reading data
            2. Stream navigation
            3. Resource management
          2. IOException
          3. OutputStream
            1. Writing data
            2. Resource management
        2. Viewing a File
        3. Layering Streams
          1. Compressing a File
            1. How this works
          2. Some Useful Intermediate Streams
        4. Readers and Writers
          1. Revisiting the ViewFile Application
      2. 2. Sockets
        1. Internet Definitions
        2. Sockets
          1. Creating a Socket
            1. A simple client application
          2. Protocols and Metadata
            1. Protocols
            2. Metadata
        3. ServerSockets
          1. The accept( ) method
          2. A Simple Web Server
        4. Customizing Socket Behavior
        5. Special-Purpose Sockets
          1. Direct Stream Manipulation
          2. Subclassing Socket Is a Better Solution
          3. A Special-Purpose Socket
          4. Factories
          5. Socket Factories
          6. Security
        6. Using SSL
          1. The SSL Handshake
          2. Using SSL with JSSE
            1. Registering providers
            2. Configuring SSLServerSocket
            3. Configuring SSLSocket
            4. Sending data
          3. Revisiting Our Web Server
      3. 3. A Socket-Based Printer Server
        1. A Network-Based Printer
        2. The Basic Objects
        3. The Protocol
          1. Encapsulation and Sending Objects
            1. DocumentDescription
          2. Network-Aware Wrapper Objects
            1. ClientNetworkWrapper
            2. ServerNetworkWrapper
        4. The Application Itself
          1. Writing the Client
          2. Redrawing the Architecture Diagram
        5. Evolving the Application
          1. What These Changes Entail
      4. 4. The Same Server, Written Using RMI
        1. The Basic Structure of RMI
          1. Methods Across the Wire
          2. Passing by Value Versus Passing by Reference
        2. The Architecture Diagram Revisited
        3. Implementing the Basic Objects
          1. The Printer Interface
          2. Implementing a Printer
            1. Examining the skeleton
          3. The Data Objects
            1. DocumentDescription
        4. The Rest of the Server
        5. The Client Application
        6. Summary
      5. 5. Introducing the Bank Example
        1. The Bank Example
        2. Sketching a Rough Architecture
          1. Five Steps to a Sketch
        3. The Basic Use Case
        4. Additional Design Decisions
          1. Design Postponements
            1. Security
            2. Scalability
          2. Implications of the Environment
        5. A Distributed Architecturefor the Bank Example
        6. Problems That Arise in Distributed Applications
          1. Partial Failures
          2. Network Latency
      6. 6. Deciding on the Remote Server
        1. A Little Bit of Bias
        2. Important Questions WhenThinking About Servers
          1. Does Each Instance of the Server Requirea Shared/Scarce Resource?
            1. Memory, in general, is not an issue here
            2. Sockets in RMI aren’t a limitation either
            3. An example of a resource limitation
            4. Moving things to a singleton resource object handles this problem
            5. Applying this to Bank versus Accounts
          2. How Well Does the Given Server Replicate/Scaleto Multiple Machines?
            1. Applying this to Bank versus Accounts
          3. Can a Single Server Handle a Typical Client Interaction?
            1. How many of the servers does the client need to know about during the courseof a typical conceptual operation?
            2. Applying this to Bank versus Accounts
          4. How Easy Is It to Make a Server Handle Multiple Simultaneous Clients?
            1. Applying this to Bank versus Accounts
          5. How Easy Is It to Tell Whether the Code Is Correct?
            1. Applying this to Bank versus Accounts
          6. How Fatal Is a Server Failure?
            1. Applying this to Bank versus Accounts
          7. How Easy Is It to Gracefully Extend the Software and Add New Functionality?
            1. Applying this to Bank versus Accounts
        3. Should We Implement Bank or Account?
      7. 7. Designing the Remote Interface
        1. Important Questions When Designing Remote Interfaces
          1. Should We Pass Method Objects?
            1. Applying this to the Account interface
          2. Should We Pass Objects as Argumentsor Use Primitive Values?
            1. Applying this to the Account interface
          3. Should We Receive Objects as Return Valuesor Receive Primitive Values?
            1. Applying this to the Account interface
          4. Do Individual Method Calls Waste Bandwidth?
            1. Applying this to the Account interface
          5. Is Each Conceptual Operation a Single Method Call?
            1. Iterators, again
            2. Applying this to the Account interface
          6. Does the Interface Expose the Right Amount of Metadata?
            1. Applying this to the Account interface
          7. Have We Identified a Reasonable Set of Distributed Exceptions?
            1. Applying this to the Account interface
        2. Building the Data Objects
          1. Data Objects Don’t Usually Have Functional Methods
          2. Interfaces Give You the Data Objects
        3. Accounting for Partial Failure
      8. 8. Implementing the Bank Server
        1. The Structure of a Server
        2. Implementing the Server
          1. A Server That Extends UnicastRemoteObject
          2. A Server That Does Not Extend UnicastRemoteObject
          3. Extending UnicastRemoteObject
            1. The benefits of UnicastRemoteObject
            2. The costs of UnicastRemoteObject
        3. Generating Stubs and Skeletons
          1. Getting Rid of the Skeletons
      9. 9. The Rest of the Application
        1. The Need for Launch Code
          1. The Idea of a Server Lifecycle
        2. Our Actual Launch Code
        3. Build Test Applications
        4. Build the Client Application
          1. Don’t Hold Connections to a Server You’re Not Using
          2. Validate Arguments on the Client Side Whenever Reasonable
          3. The Actual Client Application
        5. Deploying the Application
    5. II. Drilling Down: Scalability
      1. 10. Serialization
        1. The Need for Serialization
          1. Drilling Down on Object Creation
        2. Using Serialization
          1. ObjectOutputStream
            1. The “write” methods
            2. The stream manipulation methods
            3. Methods that customize the serialization mechanism
          2. ObjectInputStream
            1. The “read” methods
            2. The stream manipulation methods
            3. Methods that customize the serialization mechanism
        3. How to Make a Class Serializable
          1. Implement the Serializable Interface
          2. Make Sure That Instance-Level, Locally Defined StateIs Serialized Properly
            1. Declaring transient fields
            2. Implementing writeObject() and readObject( )
            3. Declaring serialPersistentFields
          3. Make Sure That Superclass State Is Handled Correctly
          4. Override equals( ) and hashCode( ) if Necessary
          5. Making DocumentDescription Serializable
            1. Implement the Serializable interface
            2. Make sure that instance-level, locally defined state is serialized properly
            3. Make sure that superclass state is handled correctly
            4. Override equals() and hashCode( ) if necessary
        4. The Serialization Algorithm
          1. The Data Format
          2. A Simplified Version of the Serialization Algorithm
            1. Writing
            2. Reading
          3. RMI Customizes the Serialization Algorithm
            1. annotateClass( )
            2. replaceObject( )
          4. Maintaining Direct Connections
        5. Versioning Classes
          1. The Two Types of Versioning Problems
          2. How Serialization Detects When a Class Has Changed
          3. Implementing Your Own Versioning Scheme
        6. Performance Issues
          1. Serialization Depends on Reflection
          2. Serialization Has a Verbose Data Format
          3. It Is Easy to Send More Data Than Is Required
        7. The Externalizable Interface
          1. Comparing Externalizable to Serializable
          2. One Final Point
      2. 11. Threads
        1. More Than One Client
        2. Basic Terminology
          1. The Calling Stack
          2. The Heap
          3. Threads
          4. Mutexes
          5. Applying This to the Printer Server
        3. Threading Concepts
          1. Controlling Individual Threads
          2. Coordinating Thread Activities
          3. Cache Management
          4. Assigning Priorities to Threads
        4. Support for Threads in Java
          1. Objects Have Associated Mutex Variables
            1. The effects of synchronization on the thread’s local cache
            2. Acquiring the same lock more than once
          2. Thread Manipulation Methods Defined on Object
            1. The wait methods
            2. The notify methods
          3. Classes
            1. Starting a thread is easy
            2. Stopping a thread is harder
            3. Using Runnable instead of subclassing Thread
            4. Useful methods defined on the Thread class
        5. Deadlock
        6. Threading and RMI
      3. 12. Implementing Threading
        1. The Basic Task
        2. Guidelines for Threading
          1. Start with Code That Works
          2. Above All Else, Ensure Data Integrity
            1. Applying this to the bank example
          3. Minimize Time Spent in Synchronized Blocks
            1. Synchronize around the smallest possible block of code
            2. Don’t synchronize across device accesses
            3. Don’t synchronize across secondary remote method invocations
          4. Be Careful When Using Container Classes
            1. Concurrent modification exceptions
            2. The other containers
          5. Use Containers to Mediate Interthread Communication
          6. Immutable Objects Are Automatically Threadsafe
          7. Always Have a Safe Way to Stop Your Threads
          8. Background Threads Should Have Low Priority
            1. Applying this to the bank example
          9. Pay Careful Attention to What You Serialize
          10. Use Threading to Reduce Response-Time Variance
          11. Limit the Number of Objects a Thread Touches
          12. Acquire Locks in a Fixed Order
          13. Use Worker Threads to Prevent Deadlocks
        3. Pools: An Extended Example
          1. The Idea of a Pool
          2. Two Interfaces That Define a Pool
          3. A First Implementation of Pooling
          4. Problems with SimplePool
          5. The Creation Thread
          6. Adding a Return Thread
          7. Gradually Shrinking the Pool
          8. Two Additional Considerations When Implementing a Pool
            1. The initial-resource creation strategy
            2. The difference between the steady-state size and the maximum-pool size
        4. Some Final Words on Threading
      4. 13. Testing a Distributed Application
        1. Testing the Bank Application
          1. What We’re Testing
          2. How to Test a Server
          3. Testing the Bank Application
            1. Build simple test objects
            2. Build aggregate tests that test entire use cases
            3. Build a threaded tester that repeatedly performs these tests
            4. Build a thread container that launches many threads and stores the resultsof the test in an indexed structure
            5. Build a reporting mechanism
      5. 14. The RMI Registry
        1. Why Use a Naming Service?
          1. Bootstrapping
            1. Installing a new printer
          2. When Are Naming Services Appropriate?
        2. The RMI Registry
          1. bind( ), rebind( ), and unbind( )
          2. lookup( ) and list( )
        3. The RMI Registry Is an RMI Server
          1. Bootstrapping the Registry
        4. Examining the Registry
          1. Querying the Registry
          2. Launching an Application-Specific Registry
        5. Limitations of the RMI Registry
          1. Directories and Entries
            1. Filesystems
            2. Yellow pages
            3. The general idea of directories and entries
        6. Security Issues
      6. 15. Naming Services
        1. Basic Design, Terminology,and Requirements
          1. Hierarchies
            1. Operations on contexts
          2. Query by Attribute
            1. Attributes are string-valued, name-value pairs
        2. Requirements for Our Naming Service
          1. Our Use Case
        3. Federation and Threading
          1. Federation
        4. The Context Interface
          1. Value Objects Represent Sets and Lists
          2. Paths, Names, and Attributes Are All Distinct
          3. Null Arguments Are Okay
          4. Attributes Are Single Valued
          5. Contexts Are Handled by a Separate Set of Methods
          6. Contexts, When Bound as Contexts, Have No Attributes
          7. Contexts Can Create Subcontexts Directly
          8. There Are No Remote Iterators
        5. The Value Objects
          1. AttributeSet
          2. Path and ContextList
        6. ContextImpl
          1. NameAttributeSetPair
          2. RemoteHolder
          3. ContextHolder
          4. ContextImpl
          5. Bootstrapping
          6. Version 2.0
        7. Switching Between Naming Services
          1. Adapting the Bank Example
            1. Modifying the launch code
            2. Modifying the client code
        8. The Java Naming and Directory Interface (JNDI)
          1. The Context Interface
          2. Using JNDI with the Bank Example
      7. 16. The RMI Runtime
        1. Reviewing the Mechanics of a Remote Method Call
          1. How RMI Solves the Bootstrapping Problem
        2. Distributed Garbage Collection
          1. Ordinary Garbage Collection
          2. Defining Network Garbage
          3. Leasing
          4. The Actual Distributed Garbage Collector
          5. The Unreferenced Interface
        3. RMI’s Logging Facilities
          1. The Standard Log
          2. The Specialized Logs
          3. The Debugging Log
        4. Other JVM Parameters
          1. Basic Java Parameters
            1. Xms
            2. Xmx
          2. Basic RMI Parameters
            1. java.rmi.server.randomIDs
            2. sun.rmi.server.exceptionTrace
          3. Transport Layer Parameters
            1. sun.rmi.transport.connectionTimeout
            2. sun.rmi.transport.tcp.readTimeout
            3. sun.rmi.transport.proxy.connectTimeout
          4. Parameters That Affect the Distributed Garbage Collector
            1. java.rmi.dgc.leaseValue
            2. sun.rmi.dgc.client.gcInterval
            3. sun.rmi.dgc.server.gcInterval
            4. sun.rmi.dgc.checkInterval
            5. sun.rmi.dgc.cleanInterval
      8. 17. Factories and the Activation Framework
        1. Resource Management
        2. Factories
          1. Revisiting Our Basic Design
        3. Implementing a Generic Factory
          1. A Basic Factory
          2. Modifying the Existing Application
            1. The consequences for the client
            2. The consequences for the naming service
            3. The consequences for the account servers
        4. A Better Factory
          1. Some Prior Art
          2. Building on the Account-Locking Mechanism
            1. The new factory
            2. The new account
            3. The launch code and the client
        5. Persistence and the Server Lifecycle
        6. Activation
          1. Code Changes Necessary for Activation
            1. Making a server into an activatable object
            2. Modifying our launch code
          2. Deploying an Activatable System
          3. ActivationDesc, ActivationGroupDesc, and ActivationGroup in More Detail
          4. Shutting Down an Activatable Server
          5. rmid Command-Line Arguments
            1. -port
            2. -log
            3. -stop
            4. -C
          6. Activation Parameters
            1. java.rmi.activation.activator.class
            2. java.rmi.activation.port
            3. sun.rmi.activation.execTimeout
            4. sun.rmi.activation.snapshotInterval
            5. sun.rmi.rmid.maxstartgroup
            6. sun.rmi.server.activation.debugExec
        7. A Final Word About Factories
    6. III. Advanced Topics
      1. 18. Using Custom Sockets
        1. Custom Socket Factories
          1. Creating Custom Socket Factories
            1. Implementing Serializable
            2. Implementing equals( ) and hashCode( )
        2. Incorporating a Custom Socket into an Application
          1. Modifying Ordinary Servers
          2. Modifying Activatable Servers
          3. Modifying the Default Servers
            1. Failure handlers
          4. Interaction with Parameters
      2. 19. Dynamic Classloading
        1. Deploying Can Be Difficult
        2. Classloaders
          1. How Classes Are Loaded
        3. How Dynamic Classloading Works
          1. A Redeployment Scenario
          2. A Multiple-Deployment Scenario
        4. The Class Server
          1. Requesting a Class
          2. Receiving a Class
          3. Handling JAR files
          4. Sun’s Class Server
        5. Using Dynamic Classloadingin an Application
          1. Server-Side Changes
          2. Naming-Service Changes
          3. Client-Side Changes
          4. Disabling Dynamic Classloading Entirely
      3. 20. Security Policies
        1. A Different Kind of Security Problem
        2. Permissions
          1. The Types of Permissions
            1. AWT permissions
            2. File permissions
            3. Socket permissions
            4. Property permissions
        3. Security Managers
          1. Installing an Instance of SecurityManager
          2. How a Security Manager Works
          3. java.security.debug
        4. Setting Up a Security Policy
          1. The Three Policy Files
            1. Setting the application-specific policy file
          2. Inside a Policy File
          3. Using Security Policies with RMI
          4. Policy Tool
          5. Some Final Words
      4. 21. Multithreaded Clients
        1. Different Types of Remote Methods
          1. Printer-Type Methods
          2. Report-Type Methods
        2. Handling Printer-Type Methods
          1. A First Pass at a Solution
          2. Better Solutions
            1. Client-side polling
            2. Polling code in the printer application
            3. Server-side callbacks
            4. Define a client-side callback interface
            5. Implement the client-side interface
        3. Handling Report-Type Methods
          1. Chapter 7 Revisited
            1. Server-evaluation models
            2. Iterators on the client side
          2. Implementing Background Downloading on the Client Side
        4. Generalizing from These Examples
      5. 22. HTTP Tunneling
        1. Firewalls
        2. CGI and Dynamic Content
          1. The Common Gateway Interface
          2. Servlets
        3. HTTP Tunneling
          1. How RMI Tunnels Messages
            1. Naming services and “the server machine”
        4. A Servlet Implementationof HTTP Tunneling
          1. The Servlet Code
        5. Modifying the Tunneling Mechanism
        6. The Bank via HTTP Tunneling
        7. Drawbacks of HTTP Tunneling
        8. Disabling HTTP Tunneling
      6. 23. RMI, CORBA, and RMI/IIOP
        1. How CORBA Works
          1. CORBA Contains Other Things Too
        2. The Bank Example in CORBA
          1. Defining the Interface
          2. Generating Stubs and Skeletons
          3. The Server
          4. The Launch and Client Code
        3. A Quick Comparison of CORBA and RMI
        4. RMI on Top of CORBA
          1. The Fine Print
        5. Converting the Bank Example to RMI/IIOP
          1. Communicating via CORBA
    7. Index
    8. About the Author
    9. Colophon
    10. SPECIAL OFFER: Upgrade this ebook with O’Reilly