Java RMI Hello World-Beispiel

Java RMI Hello World Beispiel

RMI steht fürRemote Method Invocation und ist das objektorientierte Äquivalent von RPC (Remote Procedure Calls). RMI was designed to make the interaction between applications using the object-oriented model and run on different machines seem like that of stand-alone programs.

Der folgende Code gibt Ihnen die Grundlage für Java RMI anhand eines sehr einfachen Beispiels für ein Server-Client-Kommunikationsmodell.

1. Die Remote-Schnittstelle

Das erste, was wir entwerfen müssen, ist dieRemote-Schnittstelle, die sowohl Server als auch Client implementieren. DieInterface müssen immer öffentlich sein undRemote erweitern. Alle in derRemote-Schnittstelle beschriebenen Methoden müssenRemoteException in ihrer Throws-Klausel auflisten.

UnserRMIInterface hat nur eine Methode; Es empfängt einen String-Parameter und gibt String zurück.

RMIInterface.java

package com.techfou.rmiinterface;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RMIInterface extends Remote {

    public String helloTo(String name) throws RemoteException;

}

2. Der Kellner

Unser Server erweitertUnicastRemoteObject und implementiert die zuvor erstelltenInterface. In der Hauptmethode binden wir den Server auf localhost mit dem Namen "MyServer".

ServerOperation.java

package com.techfou.rmiserver;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import com.example.rmiinterface.RMIInterface;

public class ServerOperation extends UnicastRemoteObject implements RMIInterface{

    private static final long serialVersionUID = 1L;

    protected ServerOperation() throws RemoteException {

        super();

    }

    @Override
    public String helloTo(String name) throws RemoteException{

        System.err.println(name + " is trying to contact!");
        return "Server says hello to " + name;

    }

    public static void main(String[] args){

        try {

            Naming.rebind("//localhost/MyServer", new ServerOperation());
            System.err.println("Server ready");

        } catch (Exception e) {

            System.err.println("Server exception: " + e.toString());
            e.printStackTrace();

        }

    }

}

3. Der Kunde

Schließlich erstellen wir den Client. Um den Server zu "finden", verwendet der Client einRMIInterface-Objekt, das nach einer Referenz für das entfernte Objekt "sucht", das dem Namen zugeordnet ist, den wir als Parameter übergeben. Mit demRMIInterface-Objekt können wir jetzt mit dem Server sprechen und Antworten erhalten.

Datei: ClientOperation.java

package com.example.rmiclient;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.swing.JOptionPane;

import com.example.rmiinterface.RMIInterface;

public class ClientOperation {

    private static RMIInterface look_up;

    public static void main(String[] args)
        throws MalformedURLException, RemoteException, NotBoundException {

        look_up = (RMIInterface) Naming.lookup("//localhost/MyServer");
        String txt = JOptionPane.showInputDialog("What is your name?");

        String response = look_up.helloTo(txt);
        JOptionPane.showMessageDialog(null, response);

    }

}

4. Wie man es laufen lässt

4.1 Create the three java files with your favorite IDE or Download the code below. Navigieren Sie wie unten gezeigt zu Ihrem Quellordner.

java-rmi-hello-9a

4.2 First thing we need to do is compile our sources. Führen SiecompileEverything.bat aus, wenn Sie den folgenden Code heruntergeladen haben, oder öffnen Sie ein Befehlsfenster in Ihrem Verzeichnis und führen Sie Folgendes aus:

Terminal

javac src/com/example/rmiinterface/RMIInterface.java src/com/example/rmiserver/ServerOperation.java src/com/example/rmiclient/ClientOperation.java

4.3 Confirm that your sources were compiled by accessing their respective directories:

java-rmi-hello-9b

4.4 Next we need to start the rmiregistry. Führen Sie entweder diestartServer.bat erneut aus oder öffnen Sie ein Befehlsfenster und führen Sie Folgendes aus:

Terminal

cd src
start rmiregistry
java com.example.rmiserver.ServerOperation

java-rmi-hello-9c

4.5 If RmiRegistry started successfully, there will be another window that looks like this:

java-rmi-hello-9d

4.6 Now we are ready to run our Client:

Öffnen Sie ein neues Eingabeaufforderungsfenster (oder führen SierunClient.bat aus den heruntergeladenen Dateien aus) und führen Sie Folgendes aus:

Terminal

cd src
java com.example.rmiclient.ClientOperation

DasClientOperation wird ausgeführt und fordert uns zur Eingabe auf:

java-rmi-hello-9e

Wir geben einen Namen in das Eingabefeld ein (z. "Marilena") und klicken Sie auf "OK"

4.7 On Server side we can see this:

java-rmi-hello-9f

4.8 And on the Client side this:

java-rmi-hello-9g

4.9 The client closes after the exchange is over, while the server stays on and we can communicate again if we run the client file again. In der Tat führen wir den Client erneut aus und:

java-rmi-hello-9h

Server:

java-rmi-hello-9i

Klient :

java-rmi-hello-9j

Quellcode herunterladen

Laden Sie es herunter -SimpleRmiExample.zip (4 KB)