Skip to content
Brian S. O'Neill edited this page Oct 5, 2024 · 3 revisions

Dirmi is replacement for Java RMI which supports bidirectional remote objects and a few more features, including:

Dirmi is designed for high performance, and it exceeds the performance of Java RMI. Unlike RMI, Dirmi does not have any firewall restrictions. Only one port needs to be opened for accepting sockets, and both client and server can export remote objects through it.

Like RMI, Dirmi supports distributed garbage collection, but only when using the AutoDispose annotation. When not used, remote objects must be explicitly disposed to release memory on both the client and the server. With or without the annotation, remote objects are automatically disposed when sessions are closed, or when the connection is lost.

Sample

Dirmi is superficially similar to Java RMI in that it requires interfaces to define remote objects. Like RMI, the interface must extend Remote, and each method must declare throwing RemoteException or a superclass. Dirmi allows alternate exceptions to be declared, using the RemoteFailure annotation.

Here's a simple example interface, which is defined exactly the same as for RMI:

import org.cojen.dirmi.Remote;
import org.cojen.dirmi.RemoteException;

public interface HelloDirmi extends Remote {
    void greetings(String name) throws RemoteException;
}

Everything else looks different from RMI. The server implementation of the interface looks like so:

import java.net.ServerSocket;

import org.cojen.dirmi.Environment;

public class HelloDirmiServer implements HelloDirmi {
    public static void main(String[] args) throws Exception {
        Environment env = Environment.create();
        env.export("main", new HelloDirmiServer());
        env.acceptAll(new ServerSocket(1234));
    }

    @Override
    public void greetings(String name) {
        System.out.println("Hello " + name);
    }
}

The Environment is a sharable object containing open sessions and pooled threads for executing them. The example is accepting any session on port 1234, and then sending a shared instance of the server to it. Sessions remain open as long as both endpoints are alive and have not explicitly closed it. The client code for establishing the session and receiving the remote object looks like:

import org.cojen.dirmi.Environment;
import org.cojen.dirmi.Session;

public class HelloDirmiClient {
    public static void main(String[] args) throws Exception {
        Environment env = Environment.create();
        String host = args[0];
        int port = 1234;
        Session<HelloDirmi> session = env.connect(HelloDirmi.class, "main", host, port);
        HelloDirmi client = session.root();

        client.greetings("Dirmi");

        env.close();
    }
}

Like the server, the client needs an Environment for managing sessions. Upon establishing a session, it accesses the root object exported by the server and calls it.

Clone this wiki locally