BAEL-7218: Inter-Process Communication Methods in Java (#15286)

This commit is contained in:
Graham Cox 2023-11-24 21:17:18 +00:00 committed by GitHub
parent b80ceb7ceb
commit f4c35c055c
5 changed files with 139 additions and 0 deletions

View File

@ -0,0 +1,16 @@
<?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>
<artifactId>core-java-ipc</artifactId>
<name>core-java-ipc</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>

View File

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

View File

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

View File

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

View File

@ -54,6 +54,7 @@
<module>core-java-concurrency-simple</module>
<module>core-java-datetime-string</module>
<module>core-java-io-conversions-2</module>
<module>core-java-ipc</module>
<module>core-java-jpms</module>
<module>core-java-lang-oop-constructors-2</module>
<module>core-java-methods</module>