Skip to content
GitLab
About GitLab
GitLab: the DevOps platform
Explore GitLab
Install GitLab
How GitLab compares
Get started
GitLab docs
GitLab Learn
Pricing
Talk to an expert
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Projects
Groups
Snippets
Sign up now
Login
Sign in
Toggle navigation
Menu
Open sidebar
Vincent Charpentier
ZmqpubsubJava
Commits
0ad2659d
Commit
0ad2659d
authored
Apr 28, 2022
by
Vincent Charpentier
Browse files
feat: version 1.0
A working publisher and subscriber for ZMQ.
parent
5cf89f14
Changes
5
Hide whitespace changes
Inline
Side-by-side
pom.xml
View file @
0ad2659d
...
...
@@ -23,6 +23,22 @@
<version>
2.9.0
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>
org.json
</groupId>
<artifactId>
json
</artifactId>
<version>
20220320
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>
com.googlecode.json-simple
</groupId>
<artifactId>
json-simple
</artifactId>
<version>
1.1
</version>
</dependency>
...
...
src/main/java/Main.java
→
src/main/java/Main
Publisher
.java
View file @
0ad2659d
import
netscape.javascript.JSObject
;
import
org.zeromq.SocketType
;
import
org.zeromq.ZMQ
;
import
org.zeromq.ZContext
;
import
zmqpubsub.ZmqPublisher
;
import
com.google.gson.JsonObject
;
import
com.google.gson.JsonParser
;
import
java.util.concurrent.TimeUnit
;
public
class
Main
public
class
Main
Publisher
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
String
jsonString
=
"{'test1':'value1','test2':{'id':0,'name':'testName'}}"
;
JsonObject
jsonObject
=
(
JsonObject
)
JsonParser
.
parseString
(
jsonString
);
System
.
out
.
println
(
jsonObject
);
ZmqPublisher
publisher
=
new
ZmqPublisher
(
"127.0.0.1"
,
"2001"
);
ZmqPublisher
publisher
=
new
ZmqPublisher
(
"127.0.0.1"
,
"2001"
);
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
publisher
.
publish
(
"allData"
,
jsonObject
);
System
.
out
.
println
(
"
published
"
);
System
.
out
.
println
(
"
Sent!
"
);
TimeUnit
.
SECONDS
.
sleep
(
1
);
}
}
}
src/main/java/mainSubscriber.java
0 → 100644
View file @
0ad2659d
import
com.google.gson.JsonObject
;
import
zmqpubsub.ZmqSubscriber
;
public
class
mainSubscriber
{
protected
static
void
handleIncomingData
(
JsonObject
jsonObject
)
{
System
.
out
.
println
(
jsonObject
);
// you can uncomment this if you are working with the included publisher.
// System.out.println(jsonObject.get("test2"));
// JsonElement test2 = jsonObject.get("test2");
// JsonObject jsonObject1 = test2.getAsJsonObject();
// System.out.println(jsonObject1.get("id"));
// JsonElement id1 = jsonObject1.get("id");
// int id1AsInt = id1.getAsInt();
// System.out.println(id1AsInt);
}
public
static
void
main
(
String
[]
args
)
{
// ZmqSubscriber subscriber = new ZmqSubscriber("127.0.0.1", "2001");
ZmqSubscriber
subscriber
=
new
ZmqSubscriber
(
"193.190.127.147"
,
"2001"
);
// To test it with VS8
subscriber
.
subscribe
(
"allData"
,
mainSubscriber:
:
handleIncomingData
);
}
}
src/main/java/zmqpubsub/ZmqPublisher.java
View file @
0ad2659d
package
zmqpubsub
;
import
com.google.gson.JsonObject
;
import
netscape.javascript.JSObject
;
import
org.zeromq.SocketType
;
import
org.zeromq.ZMQ
;
import
org.zeromq.ZContext
;
import
org.zeromq.ZMQ
;
public
class
ZmqPublisher
{
...
...
@@ -26,7 +26,6 @@ public class ZmqPublisher
public
void
publish
(
String
topic
,
JsonObject
payload
)
{
System
.
out
.
println
(
payload
);
socket
.
send
(
topic
+
":"
+
payload
);
}
}
src/main/java/zmqpubsub/ZmqSubscriber.java
0 → 100644
View file @
0ad2659d
package
zmqpubsub
;
import
com.google.gson.JsonObject
;
import
com.google.gson.JsonParser
;
import
org.zeromq.SocketType
;
import
org.zeromq.ZContext
;
import
org.zeromq.ZMQ
;
import
java.util.function.Consumer
;
public
class
ZmqSubscriber
{
public
ZContext
context
;
public
ZMQ
.
Socket
socket
;
public
String
bind
;
public
ZmqSubscriber
(
String
ip
,
String
port
)
{
context
=
new
ZContext
();
socket
=
context
.
createSocket
(
SocketType
.
SUB
);
bind
=
"tcp://"
+
ip
+
":"
+
port
;
}
public
void
subscribe
(
String
topic
,
Consumer
<
JsonObject
>
consumer
)
{
socket
.
connect
(
bind
);
socket
.
subscribe
(
topic
);
new
Thread
(()
->
{
while
(
true
)
{
byte
[]
recv
=
socket
.
recv
();
String
receivedStringMessage
=
new
String
(
recv
);
// s1 contains everything after = in the original string (i.e. =....) therefore +1 to remove the =
JsonObject
jsonObject
=
(
JsonObject
)
JsonParser
.
parseString
(
receivedStringMessage
.
substring
(
receivedStringMessage
.
indexOf
(
":"
)
+
1
));
consumer
.
accept
(
jsonObject
);
}
}).
start
();
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment