From 23a3e260ee82ccbbdac79b482469e30bde9666d9 Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 19 Dec 2024 13:36:16 +0100 Subject: [PATCH] feat: add RMI exercies --- RMI/.gitignore | 4 ++ RMI/Makefile | 16 ++++++ RMI/pom.xml | 54 +++++++++++++++++++ RMI/src/main/java/me/srvstr/rmi/Hello.java | 14 +++++ .../main/java/me/srvstr/rmi/HelloServer.java | 30 +++++++++++ RMI/src/main/java/me/srvstr/rmi/Main.java | 48 +++++++++++++++++ 6 files changed, 166 insertions(+) create mode 100644 RMI/.gitignore create mode 100644 RMI/Makefile create mode 100644 RMI/pom.xml create mode 100644 RMI/src/main/java/me/srvstr/rmi/Hello.java create mode 100644 RMI/src/main/java/me/srvstr/rmi/HelloServer.java create mode 100644 RMI/src/main/java/me/srvstr/rmi/Main.java diff --git a/RMI/.gitignore b/RMI/.gitignore new file mode 100644 index 0000000..6cb1223 --- /dev/null +++ b/RMI/.gitignore @@ -0,0 +1,4 @@ +/target/ +.project +.settings +.classpath diff --git a/RMI/Makefile b/RMI/Makefile new file mode 100644 index 0000000..9af1f8d --- /dev/null +++ b/RMI/Makefile @@ -0,0 +1,16 @@ + +build: + mvn package + +clean: + mvn clean + +server: + java -jar target/rmi-1.0-SNAPSHOT-jar-with-dependencies.jar --server + +client: + java -jar target/rmi-1.0-SNAPSHOT-jar-with-dependencies.jar --client + +registry: + # Source: https://stackoverflow.com/a/23643744 + rmiregistry -J-Djava.rmi.server.codebase=file:target/rmi-1.0-SNAPSHOT-jar-with-dependencies.jar & diff --git a/RMI/pom.xml b/RMI/pom.xml new file mode 100644 index 0000000..f0d172f --- /dev/null +++ b/RMI/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + me.srvstr.rmi + rmi + 1.0-SNAPSHOT + rmi + + UTF-8 + 17 + + + + + + + + + + + org.apache.maven.plugins + 3.13.0 + maven-compiler-plugin + + 16 + 16 + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + + + + + me.srvstr.rmi.Main + + + + jar-with-dependencies + + + + + + diff --git a/RMI/src/main/java/me/srvstr/rmi/Hello.java b/RMI/src/main/java/me/srvstr/rmi/Hello.java new file mode 100644 index 0000000..079505f --- /dev/null +++ b/RMI/src/main/java/me/srvstr/rmi/Hello.java @@ -0,0 +1,14 @@ +package me.srvstr.rmi; + +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.util.Date; + +public interface Hello extends Remote { + + void sayHello() throws RemoteException; + + String printMessage(String input) throws RemoteException; + + Date timestampToDate(long timestamp) throws RemoteException; +} diff --git a/RMI/src/main/java/me/srvstr/rmi/HelloServer.java b/RMI/src/main/java/me/srvstr/rmi/HelloServer.java new file mode 100644 index 0000000..1afd735 --- /dev/null +++ b/RMI/src/main/java/me/srvstr/rmi/HelloServer.java @@ -0,0 +1,30 @@ +package me.srvstr.rmi; + +import java.rmi.RemoteException; +import java.rmi.server.UnicastRemoteObject; +import java.time.Instant; +import java.util.Date; + +public class HelloServer extends UnicastRemoteObject implements Hello { + + public HelloServer() throws RemoteException { + super(); + } + + @Override + public Date timestampToDate(long timestamp) throws RemoteException { + return Date.from(Instant.ofEpochMilli(timestamp)); + } + + @Override + public String printMessage(String input) throws RemoteException { + System.out.println(input); + + return "Hallo zurück!"; + } + + @Override + public void sayHello() throws RemoteException { + System.out.println("Hello, World!!!"); + } +} diff --git a/RMI/src/main/java/me/srvstr/rmi/Main.java b/RMI/src/main/java/me/srvstr/rmi/Main.java new file mode 100644 index 0000000..9b452fe --- /dev/null +++ b/RMI/src/main/java/me/srvstr/rmi/Main.java @@ -0,0 +1,48 @@ +package me.srvstr.rmi; + +import java.net.MalformedURLException; +import java.rmi.Naming; +import java.rmi.NotBoundException; +import java.rmi.RemoteException; + +public class Main { + + private static void client() { + try { + // locate remote object + Hello stub = (Hello) Naming.lookup("rmi://localhost/HelloServer"); + + stub.sayHello(); + + System.out.println(stub.printMessage("Hallo vom Client!")); + + System.out.println(stub.timestampToDate(System.currentTimeMillis())); + + } catch (NotBoundException | RemoteException | MalformedURLException ex) { + System.out.println(ex.getMessage()); + } + } + + private static void server() { + try { + Naming.rebind("rmi://localhost/HelloServer", new HelloServer()); + System.out.println("Server ready"); + } catch (RemoteException | MalformedURLException ex) { + System.out.println(ex.getMessage()); + } + } + + public static void main(String[] args) { + + if ("--client".equals(args[0])) { + System.out.println("Running in client mode"); + + client(); + + } else if ("--server".equals(args[0])) { + System.out.println("Running in server mode"); + + server(); + } + } +}