-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2093 from tronprotocol/native_eventqueue
Native eventqueue
- Loading branch information
Showing
6 changed files
with
246 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/org/tron/common/logsfilter/nativequeue/NativeMessageQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.tron.common.logsfilter.nativequeue; | ||
|
||
import org.zeromq.SocketType; | ||
import org.zeromq.ZContext; | ||
import org.zeromq.ZMQ; | ||
import java.util.Objects; | ||
|
||
public class NativeMessageQueue { | ||
ZContext context = null; | ||
private ZMQ.Socket publisher = null; | ||
private static NativeMessageQueue instance; | ||
private static final int DEFAULT_BIND_PORT = 5555; | ||
private static final int DEFAULT_QUEUE_LENGTH = 1000; | ||
public static NativeMessageQueue getInstance() { | ||
if (Objects.isNull(instance)) { | ||
synchronized (NativeMessageQueue.class) { | ||
if (Objects.isNull(instance)) { | ||
instance = new NativeMessageQueue(); | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
public boolean start(int bindPort, int sendQueueLength) { | ||
context = new ZContext(); | ||
publisher = context.createSocket(SocketType.PUB); | ||
|
||
if (Objects.isNull(publisher)){ | ||
return false; | ||
} | ||
|
||
if (bindPort == 0 || bindPort < 0) { | ||
bindPort = DEFAULT_BIND_PORT; | ||
} | ||
|
||
if (sendQueueLength ==0 || sendQueueLength < 0){ | ||
sendQueueLength = DEFAULT_QUEUE_LENGTH; | ||
} | ||
|
||
context.setSndHWM(sendQueueLength); | ||
|
||
String bindAddress = String.format("tcp://*:%d", bindPort); | ||
publisher.bind(bindAddress); | ||
|
||
return true; | ||
} | ||
|
||
public void stop(){ | ||
if (Objects.nonNull(publisher)){ | ||
publisher.close(); | ||
} | ||
|
||
if (Objects.nonNull(context)){ | ||
context.close(); | ||
} | ||
} | ||
|
||
public void publishTrigger(String data, String topic){ | ||
if (Objects.isNull(publisher) || Objects.isNull(context.isClosed()) || context.isClosed()) { | ||
return; | ||
} | ||
|
||
publisher.sendMore(topic); | ||
publisher.send(data); | ||
} | ||
} |
Oops, something went wrong.