mirror of
https://github.com/EDCD/EDDN.git
synced 2025-04-12 07:00:04 +03:00
A simple Java subscribe example with jermq
This commit is contained in:
parent
2b141bf8ac
commit
47091574a3
72
examples/Java/SimpleSubscribe/pom.xml
Normal file
72
examples/Java/SimpleSubscribe/pom.xml
Normal file
@ -0,0 +1,72 @@
|
||||
<?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>com.example</groupId>
|
||||
<artifactId>SimpleJavaEDDNSubscribe</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<name>SimpleJavaEDDNSubscribe</name>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.11</maven.compiler.source>
|
||||
<maven.compiler.target>1.11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.zeromq</groupId>
|
||||
<artifactId>jeromq</artifactId>
|
||||
<version>0.5.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||
<plugins>
|
||||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</plugin>
|
||||
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>2.5.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>2.8.2</version>
|
||||
</plugin>
|
||||
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-site-plugin</artifactId>
|
||||
<version>3.7.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,53 @@
|
||||
package org.eddn.examples;
|
||||
|
||||
import java.util.zip.Inflater;
|
||||
|
||||
import org.zeromq.SocketType;
|
||||
import org.zeromq.ZMQ;
|
||||
import org.zeromq.ZContext;
|
||||
|
||||
public class SimpleJavaEDDNSubscribe {
|
||||
private static final int MAX_MESSAGE_SIZE_KB = 200;
|
||||
private static final String EDDN_SERVER = "tcp://eddn.edcd.io:9500";
|
||||
private static ZContext context;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
context = new ZContext();
|
||||
String jsonMessage = getOneMessage();
|
||||
System.out.println(jsonMessage);
|
||||
context.close();
|
||||
}
|
||||
|
||||
private static String getOneMessage() throws Exception {
|
||||
byte[] deflatedMessage = receiveOneDeflatedMessage();
|
||||
return inflateMessage(deflatedMessage);
|
||||
}
|
||||
|
||||
private static byte[] receiveOneDeflatedMessage() {
|
||||
ZMQ.Socket socket = getEDDNSubscriptionSocket();
|
||||
byte[] deflatedMessage = socket.recv();
|
||||
return deflatedMessage;
|
||||
}
|
||||
|
||||
private static ZMQ.Socket getEDDNSubscriptionSocket() {
|
||||
ZMQ.Socket socket = context.createSocket(SocketType.SUB);
|
||||
socket.connect(EDDN_SERVER);
|
||||
|
||||
// need to subscribe to the empty topic to receive anything
|
||||
socket.subscribe("");
|
||||
return socket;
|
||||
}
|
||||
|
||||
public static String inflateMessage(byte[] bytes) throws Exception {
|
||||
|
||||
Inflater decompresser = new Inflater();
|
||||
decompresser.setInput(bytes);
|
||||
byte[] result = new byte[MAX_MESSAGE_SIZE_KB * 1024];
|
||||
int resultLength = decompresser.inflate(result);
|
||||
decompresser.end();
|
||||
|
||||
// Decode the bytes into a String
|
||||
String outputString = new String(result, 0, resultLength, "UTF-8");
|
||||
return outputString;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user