Add ZMQ context customization
This commit is contained in:
parent
6f5270ee37
commit
405bcd6ba3
@ -20,8 +20,9 @@ public class ZmqMagixEndpoint(
|
||||
private val pubPort: Int = MagixEndpoint.DEFAULT_MAGIX_ZMQ_PUB_PORT,
|
||||
private val pullPort: Int = MagixEndpoint.DEFAULT_MAGIX_ZMQ_PULL_PORT,
|
||||
private val coroutineContext: CoroutineContext = Dispatchers.IO,
|
||||
private val zmqContext: ZContext = ZContext()
|
||||
|
||||
) : MagixEndpoint, AutoCloseable {
|
||||
private val zmqContext by lazy { ZContext() }
|
||||
|
||||
override fun subscribe(filter: MagixMessageFilter): Flow<MagixMessage> {
|
||||
val socket = zmqContext.createSocket(SocketType.SUB)
|
||||
|
@ -24,35 +24,34 @@ public class ZmqMagixFlowPlugin(
|
||||
scope: CoroutineScope,
|
||||
receive: Flow<MagixMessage>,
|
||||
sendMessage: suspend (MagixMessage) -> Unit,
|
||||
): Job =
|
||||
scope.launch(Dispatchers.IO) {
|
||||
val logger = LoggerFactory.getLogger("magix-server-zmq")
|
||||
): Job = scope.launch(Dispatchers.IO) {
|
||||
val logger = LoggerFactory.getLogger("magix-server-zmq")
|
||||
|
||||
ZContext().use { context ->
|
||||
//launch the publishing job
|
||||
val pubSocket = context.createSocket(SocketType.PUB)
|
||||
pubSocket.bind("$localHost:$zmqPubSocketPort")
|
||||
receive.onEach { message ->
|
||||
val string = MagixEndpoint.magixJson.encodeToString(message)
|
||||
pubSocket.send(string)
|
||||
logger.trace("Published: $string")
|
||||
}.launchIn(this)
|
||||
ZContext().use { context ->
|
||||
//launch the publishing job
|
||||
val pubSocket = context.createSocket(SocketType.PUB)
|
||||
pubSocket.bind("$localHost:$zmqPubSocketPort")
|
||||
receive.onEach { message ->
|
||||
val string = MagixEndpoint.magixJson.encodeToString(message)
|
||||
pubSocket.send(string)
|
||||
logger.trace("Published: $string")
|
||||
}.launchIn(this)
|
||||
|
||||
//launch pulling job
|
||||
val pullSocket = context.createSocket(SocketType.PULL)
|
||||
pullSocket.bind("$localHost:$zmqPullSocketPort")
|
||||
pullSocket.receiveTimeOut = 500
|
||||
//suspending loop while pulling is active
|
||||
while (isActive) {
|
||||
val string: String? = pullSocket.recvStr()
|
||||
if (string != null) {
|
||||
logger.trace("Received: $string")
|
||||
val message = MagixEndpoint.magixJson.decodeFromString<MagixMessage>(string)
|
||||
sendMessage(message)
|
||||
}
|
||||
//launch pulling job
|
||||
val pullSocket = context.createSocket(SocketType.PULL)
|
||||
pullSocket.bind("$localHost:$zmqPullSocketPort")
|
||||
pullSocket.receiveTimeOut = 500
|
||||
//suspending loop while pulling is active
|
||||
while (isActive) {
|
||||
val string: String? = pullSocket.recvStr()
|
||||
if (string != null) {
|
||||
logger.trace("Received: $string")
|
||||
val message = MagixEndpoint.magixJson.decodeFromString<MagixMessage>(string)
|
||||
sendMessage(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user