Backend Development 8 min read

Understanding Java RMI Serialization and Object Transmission with Example Code

This article explains the Java RMI communication model, the need for object serialization, the requirements for serializable classes, and provides complete example code for a Person class, object I/O operations, and a simple socket‑based stub‑skeleton implementation to demonstrate remote object retrieval.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Understanding Java RMI Serialization and Object Transmission with Example Code

The RMI communication model faces two key challenges: discovering remote objects and transmitting data, which relies on passing reference types that must be serialized into byte streams for socket‑based transfer between JVMs.

In Java, an object can be serialized only if it implements the java.io.Serializable interface; primitive types are inherently serializable, and any nested objects must also be serializable unless marked as transient.

Example of a serializable Person class:

import java.io.Serializable;

public class Person implements Serializable {
    private String name;
    private int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
}

A test program demonstrates writing a list of Person objects to a file with ObjectOutputStream and reading them back with ObjectInputStream :

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ObjectIOTest {
    public static void main(String[] args) {
        writeObject();
        getObject();
    }
    private static void writeObject() {
        try {
            List
persons = new ArrayList<>();
            persons.add(new Person("zhangsan", 24));
            persons.add(new Person("lisi", 25));
            persons.add(new Person("wanger", 26));
            persons.add(new Person("gouzi", 27));
            persons.add(new Person("huizi", 28));
            ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream("person.txt"));
            stream.writeObject(persons);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void getObject() {
        try {
            ObjectInputStream stream = new ObjectInputStream(new FileInputStream("person.txt"));
            List
personList = (List
) stream.readObject();
            if (personList != null && !personList.isEmpty()) {
                for (Person p : personList) {
                    System.out.println(p);
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Running the program prints:

Person [name=zhangsan, age=24]
Person [name=lisi, age=25]
Person [name=wanger, age=26]
Person [name=gouzi, age=27]
Person [name=huizi, age=28]

A simplified RMI‑like mechanism is then built using a Stub that opens a socket to the server, sends the string "getPerson", and receives the serialized object, and a Skeleton that listens on a server socket, reads the request, creates a Person instance, and writes it back.

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ObjectStub {
    private Socket socket;
    public ObjectStub() throws Throwable {
        socket = new Socket("localhost", 8888);
    }
    public Object getObject() throws Throwable {
        ObjectOutputStream outStream = new ObjectOutputStream(socket.getOutputStream());
        outStream.writeObject("getPerson");
        outStream.flush();
        ObjectInputStream inStream = new ObjectInputStream(socket.getInputStream());
        return inStream.readObject();
    }
    public static void main(String[] args) {
        try {
            ObjectStub stub = new ObjectStub();
            Object o = stub.getObject();
            if (o instanceof Person) {
                System.out.println("获取对象成功:" + o);
            } else {
                System.out.println("获取对象失败:" + o);
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
}
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ObjectSkeleton implements Runnable {
    @Override
    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            Socket socket = serverSocket.accept();
            while (socket != null) {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                String action = (String) ois.readObject();
                if (action.equals("getPerson")) {
                    Person person = new Person("zhangsan", 24);
                    oos.writeObject(person);
                    oos.flush();
                } else {
                    oos.writeObject("error");
                    oos.flush();
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(0);
        }
    }
    public static void main(String[] args) {
        new Thread(new ObjectSkeleton()).start();
    }
}

The article concludes that this simple example demonstrates how RMI serializes reference types over sockets, and encourages readers to explore further resources or join discussions.

JavaBackend DevelopmentserializationsocketObjectIORMI
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.