From f4c35c055c48de3e15892bb10237f9cc827f27b7 Mon Sep 17 00:00:00 2001 From: Graham Cox Date: Fri, 24 Nov 2023 21:17:18 +0000 Subject: [PATCH] BAEL-7218: Inter-Process Communication Methods in Java (#15286) --- core-java-modules/core-java-ipc/pom.xml | 16 ++++++ .../com/baeldung/ipc/DirectoryLiveTest.java | 32 ++++++++++++ .../java/com/baeldung/ipc/JmxLiveTest.java | 52 +++++++++++++++++++ .../com/baeldung/ipc/SocketsLiveTest.java | 38 ++++++++++++++ core-java-modules/pom.xml | 1 + 5 files changed, 139 insertions(+) create mode 100644 core-java-modules/core-java-ipc/pom.xml create mode 100644 core-java-modules/core-java-ipc/src/test/java/com/baeldung/ipc/DirectoryLiveTest.java create mode 100644 core-java-modules/core-java-ipc/src/test/java/com/baeldung/ipc/JmxLiveTest.java create mode 100644 core-java-modules/core-java-ipc/src/test/java/com/baeldung/ipc/SocketsLiveTest.java diff --git a/core-java-modules/core-java-ipc/pom.xml b/core-java-modules/core-java-ipc/pom.xml new file mode 100644 index 0000000000..b7b37afb1d --- /dev/null +++ b/core-java-modules/core-java-ipc/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + core-java-ipc + core-java-ipc + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + diff --git a/core-java-modules/core-java-ipc/src/test/java/com/baeldung/ipc/DirectoryLiveTest.java b/core-java-modules/core-java-ipc/src/test/java/com/baeldung/ipc/DirectoryLiveTest.java new file mode 100644 index 0000000000..ca7b6d2a0e --- /dev/null +++ b/core-java-modules/core-java-ipc/src/test/java/com/baeldung/ipc/DirectoryLiveTest.java @@ -0,0 +1,32 @@ +package com.baeldung.ipc; + +import org.junit.jupiter.api.Test; + +import java.nio.file.FileSystems; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardWatchEventKinds; +import java.nio.file.WatchKey; +import java.nio.file.WatchEvent; +import java.nio.file.WatchService; + + +public class DirectoryLiveTest { + @Test + public void consumer() throws Exception { + WatchService watchService = FileSystems.getDefault().newWatchService(); + // Set this to an appropriate directory. + Path path = Paths.get("/tmp/ipc"); + + path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); + + WatchKey key; + while ((key = watchService.take()) != null) { + for (WatchEvent event : key.pollEvents()) { + // React to new file. + System.out.println(event); + } + key.reset(); + } + } +} diff --git a/core-java-modules/core-java-ipc/src/test/java/com/baeldung/ipc/JmxLiveTest.java b/core-java-modules/core-java-ipc/src/test/java/com/baeldung/ipc/JmxLiveTest.java new file mode 100644 index 0000000000..8ac16fdfc3 --- /dev/null +++ b/core-java-modules/core-java-ipc/src/test/java/com/baeldung/ipc/JmxLiveTest.java @@ -0,0 +1,52 @@ +package com.baeldung.ipc; + +import org.junit.jupiter.api.Test; + +import javax.management.JMX; +import javax.management.MBeanServer; +import javax.management.ObjectName; +import javax.management.remote.JMXConnector; +import javax.management.remote.JMXConnectorFactory; +import javax.management.remote.JMXServiceURL; +import java.lang.management.ManagementFactory; +import java.util.concurrent.TimeUnit; + +public class JmxLiveTest { + /* + * This test needs to be run with the following system properties defined: + * -Dcom.sun.management.jmxremote=true + * -Dcom.sun.management.jmxremote.port=1234 + * -Dcom.sun.management.jmxremote.authenticate=false + * -Dcom.sun.management.jmxremote.ssl=false + */ + @Test + public void consumer() throws Exception { + ObjectName objectName = new ObjectName("com.baeldung.ipc:type=basic,name=test"); + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + server.registerMBean(new IPCTest(), objectName); + + TimeUnit.MINUTES.sleep(50); + } + + @Test + public void producer() throws Exception { + JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1234/jmxrmi"); + try (JMXConnector jmxc = JMXConnectorFactory.connect(url, null)) { + ObjectName objectName = new ObjectName("com.baeldung.ipc:type=basic,name=test"); + + IPCTestMBean mbeanProxy = JMX.newMBeanProxy(jmxc.getMBeanServerConnection(), objectName, IPCTestMBean.class, true); + mbeanProxy.sendMessage("Hello"); + } + } + + public interface IPCTestMBean { + void sendMessage(String message); + } + + class IPCTest implements IPCTestMBean { + @Override + public void sendMessage(String message) { + System.out.println("Received message: " + message); + } + } +} diff --git a/core-java-modules/core-java-ipc/src/test/java/com/baeldung/ipc/SocketsLiveTest.java b/core-java-modules/core-java-ipc/src/test/java/com/baeldung/ipc/SocketsLiveTest.java new file mode 100644 index 0000000000..9b006c110e --- /dev/null +++ b/core-java-modules/core-java-ipc/src/test/java/com/baeldung/ipc/SocketsLiveTest.java @@ -0,0 +1,38 @@ +package com.baeldung.ipc; + +import org.junit.jupiter.api.Test; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.ServerSocket; +import java.net.Socket; + +public class SocketsLiveTest { + @Test + public void consumer() throws Exception { + try (ServerSocket serverSocket = new ServerSocket(1234)) { + Socket clientSocket = serverSocket.accept(); + + PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); + BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + + String line; + while ((line = in.readLine()) != null) { + System.out.println("Received message: " + line); + } + } + } + + @Test + public void producer() throws Exception { + try (Socket clientSocket = new Socket("localhost", 1234)) { + PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); + BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + + out.println("Hello"); + + String response = in.readLine(); + } + } +} \ No newline at end of file diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 9343343b6e..b3ef8167a5 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -54,6 +54,7 @@ core-java-concurrency-simple core-java-datetime-string core-java-io-conversions-2 + core-java-ipc core-java-jpms core-java-lang-oop-constructors-2 core-java-methods