feat: add RMI exercies
This commit is contained in:
commit
23a3e260ee
|
@ -0,0 +1,4 @@
|
|||
/target/
|
||||
.project
|
||||
.settings
|
||||
.classpath
|
|
@ -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 &
|
|
@ -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>
|
|
@ -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;
|
||||
}
|
|
@ -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!!!");
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue