feat: add RMI exercies

This commit is contained in:
Sven Vogel 2024-12-19 13:36:16 +01:00
commit 23a3e260ee
6 changed files with 166 additions and 0 deletions

4
RMI/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/target/
.project
.settings
.classpath

16
RMI/Makefile Normal file
View File

@ -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 &

54
RMI/pom.xml Normal file
View File

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.srvstr.rmi</groupId>
<artifactId>rmi</artifactId>
<version>1.0-SNAPSHOT</version>
<name>rmi</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<dependencyManagement>
<dependencies>
</dependencies>
</dependencyManagement>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>3.13.0</version>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
<!-- Source: https://stackoverflow.com/a/41383973 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>me.srvstr.rmi.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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;
}

View File

@ -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!!!");
}
}

View File

@ -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();
}
}
}