Last active
January 29, 2019 12:38
-
-
Save shs96c/7607f5ce1cd436418e5c5aaa54cf4ec1 to your computer and use it in GitHub Desktop.
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
package com.example; | |
import static org.junit.Assert.assertTrue; | |
import org.junit.Test; | |
import org.zeromq.ZContext; | |
import org.zeromq.ZMQ; | |
public class Sockets { | |
@Test | |
public void differentContexts() throws InterruptedException { | |
String connection = "tcp://*:1234"; | |
ZContext one = new ZContext(); | |
// Create a publishing socket, and bind to the port | |
ZMQ.Socket bindingPublisher = one.createSocket(ZMQ.PUB); | |
bindingPublisher.setImmediate(true); | |
assertTrue(bindingPublisher.bind(connection)); | |
// Simulate connecting from a different process, by creating | |
// a new context. | |
ZContext two = new ZContext(); | |
// Add a subscriber | |
ZMQ.Socket sub = two.createSocket(ZMQ.SUB); | |
sub.setImmediate(true); | |
assertTrue(sub.connect(connection)); | |
sub.subscribe(ZMQ.SUBSCRIPTION_ALL); | |
// From the new process, also try and publish events | |
ZMQ.Socket pub = two.createSocket(ZMQ.PUB); | |
pub.setImmediate(true); | |
assertTrue(pub.connect(connection)); | |
// Give everything a chance to start in entirely the wrong way | |
Thread.sleep(1000); | |
assertTrue(pub.send("Hello, World!")); | |
String str = sub.recvStr(); | |
System.out.println(str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment