Skip to content
This repository was archived by the owner on Aug 23, 2020. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/main/java/com/iota/iri/zmq/MessageQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import org.zeromq.ZMQ;

import java.text.MessageFormat;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
* Created by paul on 6/20/17.
Expand All @@ -13,8 +14,10 @@ public class MessageQ {
private final ZMQ.Socket publisher;
private boolean enabled = false;

private final ExecutorService publisherService = Executors.newSingleThreadExecutor();

public MessageQ(int port, String ipc, int nthreads, boolean enabled) {
if(enabled) {
if (enabled) {
context = ZMQ.context(nthreads);
publisher = context.socket(ZMQ.PUB);
publisher.bind(String.format("tcp://*:%d", port));
Expand All @@ -29,12 +32,19 @@ public MessageQ(int port, String ipc, int nthreads, boolean enabled) {
}

public void publish(String message, Object... objects) {
if(enabled) {
publisher.send(String.format(message, objects));
if (enabled) {
String toSend = String.format(message, objects);
publisherService.submit(() -> publisher.send(toSend));
}
}

public void shutdown () {
public void shutdown() {
publisherService.shutdown();
try {
publisherService.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be printed with a logger?

}
publisher.close();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is a try I find it more readable to put the last two lines in a finally block.

context.term();
}
Expand Down