Ch 18: Distributed Computing
Source: Head First Java, Second Edition | Pages: 641-682
🎯 Learning Objectives
Remote Method Invocation
📚 Key Concepts
RMI concept
Stubs and skeletons
Remote interface
RMI registry
Distributed computing
Servlets intro
EJB overview
📖 Detailed Notes
1. RMI concept
Essential concept for mastering Java and OOP.
Example:
2. Stubs and skeletons
Essential concept for mastering Java and OOP.
Example:
3. Remote interface
Essential concept for mastering Java and OOP.
Example:
4. RMI registry
Essential concept for mastering Java and OOP.
Example:
5. Distributed computing
Essential concept for mastering Java and OOP.
Example:
6. Servlets intro
Essential concept for mastering Java and OOP.
Example:
7. EJB overview
Essential concept for mastering Java and OOP.
Example:
💡 Important Points to Remember
thing is to find out exactly where your servlet class files
method! The method of the
that even though
that fabulous little ‘music video’ program from the first
the Servlet library
✅ Self-Check Questions
Test your understanding:
Can you explain the main concepts covered in this chapter?
Can you write code examples demonstrating these concepts?
Do you understand when and why to use these features?
Can you explain the benefits and tradeoffs?
🔄 Quick Revision Points
📝 Practice Exercises
Write your own code examples for each key concept
Modify existing examples to test edge cases
Explain concepts to someone else
Create a small project using these concepts
🔗 Related Chapters
Review related concepts from other chapters to build comprehensive understanding.
For complete details, diagrams, and all examples, refer to Head First Java Second Edition, pages 641-682.
Chapter 18: Distributed Computing — Study Notes
This chapter explores how to break your application into parts that run on different machines but work together as a single system. It covers RMI (Remote Method Invocation), Servlets, and Jini, focusing on how Java objects can talk to each other across a network.
1. Remote Method Invocation (RMI)
RMI allows a Java object on one machine (the client) to call a method on a Java object on a different machine (the server) just as if it were a local object.
The Big Picture
The Goal: You want to call a method on a remote object (e.g.,
serverObject.doStuff()).The Problem: The
serverObjectis on a different heap in a different memory space. You can't get a direct reference to it.The Solution: You use a proxy (called a Stub) on the client side. The client calls the method on the stub, and the stub handles the networking to talk to the real service.
The RMI Architecture
Client: Calls a method on the
Stub.Stub (Proxy): Takes the call, packs the arguments (marshalling), and sends them over the network.
Skeleton: Receives the call on the server side, unpacks the arguments, and calls the actual method on the real service object.
Service Object: Does the work and returns the result to the Skeleton.
Skeleton: Packs the result and sends it back to the Stub.
Stub: Unpacks the result and returns it to the client.
Making an RMI Service (5 Steps)
Make a Remote Interface: Define the methods that the client can call.
Must extend
java.rmi.Remote.All methods must declare
throws RemoteException.
Make a Remote Implementation: Implement the interface.
Typically extends
UnicastRemoteObject.Must write a constructor that declares
throws RemoteException.
Generate Stub and Skeleton: Run
rmicon your implementation class (e.g.,rmic MyServiceImpl).Start the RMI Registry: Run
rmiregistryfrom the command line. This is like the phone book for RMI services.Start the Service: Run your service class. It must register itself with the RMI registry using
Naming.rebind("ServiceName", serviceObject).
2. Servlets (Web Applications)
Servlets are Java programs that run on a web server (like Apache Tomcat) and handle requests from web browsers.
How it Works
User: Types a URL into a browser (e.g.,
http://myserver.com/servlets/MyServlet).Web Server: Receives the request and realizes it's for a servlet, not a static HTML file.
Servlet: The server loads the servlet class and calls its
doGet()ordoPost()method.Response: The servlet writes an HTML response (using
out.println()) which the server sends back to the user's browser.
Simple Servlet Code
Java
3. Jini (Universal Network Plug-and-Play)
Jini (now Apache River) is a technology for building distributed systems that are dynamic and adaptive. It allows devices and services to find each other on a network without prior configuration.
Key Concepts
Discovery: A service searches the network for a "Lookup Service" (like an RMI registry, but smarter).
Join: The service registers itself with the Lookup Service, uploading its own proxy (serialized object).
Lookup: A client asks the Lookup Service for a specific type of service (e.g., "I need a printer").
Service Object: The Lookup Service sends the service's proxy to the client. The client now talks directly to the service.
Self-Healing Networks
Jini uses Leasing. When a service registers, it gets a "lease" for a fixed time. The service must periodically renew the lease. If the service crashes (or is unplugged), the lease expires, and the Lookup Service automatically removes it from the list.
4. Revision Checklist
RMI Exceptions: Remember that every remote method call is risky (network down, server crash), so they all throw
RemoteException.Serialization in RMI: Arguments and return values in RMI are passed by value (copies) using serialization, not by reference (unless they are remote objects themselves).
Interface Rule: The client always uses the Remote Interface type (e.g.,
MyService), never the implementation class type (e.g.,MyServiceImpl).Servlet Output: Servlets create dynamic web pages by printing HTML tags as Strings to the response stream.
Last updated