本文整理汇总了Java中org.zeromq.ZMQ.Context类的典型用法代码示例。如果您正苦于以下问题:Java Context类的具体用法?Java Context怎么用?Java Context使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Context类属于org.zeromq.ZMQ包,在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
public static void main (String[] args) {
// Prepare our context and sockets
Context context = ZMQ.context(1);
// This is where the weather server sits
Socket frontend = context.socket(ZMQ.SUB);
frontend.connect("tcp://192.168.55.210:5556");
// This is our public endpoint for subscribers
Socket backend = context.socket(ZMQ.PUB);
backend.bind("tcp://10.1.1.0:8100");
// Subscribe on everything
frontend.subscribe(ZMQ.SUBSCRIPTION_ALL);
// Run the proxy until the user interrupts us
ZMQ.proxy (frontend, backend, null);
frontend.close();
backend.close();
context.term();
}
示例2: main
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
public static void main (String[] args) {
// Prepare our context and subscriber
Context context = ZMQ.context(1);
Socket subscriber = context.socket(ZMQ.SUB);
subscriber.connect("tcp://localhost:5563");
subscriber.subscribe("B".getBytes(ZMQ.CHARSET));
while (!Thread.currentThread ().isInterrupted ()) {
// Read envelope with address
String address = subscriber.recvStr ();
// Read message contents
String contents = subscriber.recvStr ();
System.out.println(address + " : " + contents);
}
subscriber.close ();
context.term ();
}
示例3: main
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
public static void main (String[] args) throws Exception {
// Prepare our context and publisher
Context context = ZMQ.context(1);
Socket publisher = context.socket(ZMQ.PUB);
publisher.bind("tcp://*:5563");
while (!Thread.currentThread ().isInterrupted ()) {
// Write two messages, each with an envelope and content
publisher.sendMore ("A");
publisher.send ("We don't want to see this");
publisher.sendMore ("B");
publisher.send("We would like to see this");
}
publisher.close ();
context.term ();
}
示例4: sendAsync
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
private static void sendAsync(int val, String account) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
Context context = ZMQ.context(1);
// Socket to talk to server
Socket requester = context.socket(ZMQ.REQ);
requester.connect("tcp://localhost:5559");
System.out.println("launch and connect client.");
ValidateRequest req = new ValidateRequest(account, new BigDecimal(val));
//send request
requester.send(objectMapper.writeValueAsString(req), 0);
//receive response
String responseStr = requester.recvStr(0);
//parse and print reply
ValidateResponse reply = objectMapper.readValue(responseStr, ValidateResponse.class);
System.out.println("Received reply for request= " + req + " reply= " + reply + "");
// We never get here but clean up anyhow
requester.close();
context.term();
}
示例5: main
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
public static void main (String[] args) {
// Prepare our context and sockets
Context context = ZMQ.context(1);
// Socket facing clients
Socket frontend = context.socket(ZMQ.ROUTER);
frontend.bind("tcp://*:5559");
// Socket facing services
Socket backend = context.socket(ZMQ.DEALER);
backend.bind("tcp://*:5560");
// Start the proxy
ZMQ.proxy (frontend, backend, null);
// We never get here but clean up anyhow
frontend.close();
backend.close();
context.term();
}
示例6: initZmq
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
protected void initZmq(int clientRequestPort, int workerResponsePort)
{
Context zmqContext = ZMQ.context(1);
clientSocket = zmqContext.socket(ZMQ.ROUTER);
workerSocket = zmqContext.socket(ZMQ.ROUTER);
clientSocket.setLinger(0);
workerSocket.setLinger(0);
clientSocket.setReceiveTimeOut(100);
workerSocket.setReceiveTimeOut(100);
clientSocket.bind("tcp://*:" + clientRequestPort);
workerSocket.bind("tcp://*:" + workerResponsePort);
poll = new Poller(2);
poll.register(clientSocket, Poller.POLLIN);
poll.register(workerSocket, Poller.POLLIN);
logger.info("Zmq proxy binds to client port [" + clientRequestPort + "] and worker port ["
+ workerResponsePort + "]");
}
示例7: testBindToRandomPort
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
/**
* Test method for {@link org.zeromq.ZMQ.Socket#bindToRandomPort(String)}.
*/
@Test
public void testBindToRandomPort() {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket sock = context.socket(ZMQ.DEALER);
// Check that bindToRandomport generate valid port number
for (int i = 0; i < 100; i++) {
sock.bindToRandomPort("tcp://127.0.0.1");
}
sock.close();
sock = context.socket(ZMQ.DEALER);
// Check that exception different of EADDRINUSE is not catched
// Invalid protocol
try {
sock.bindToRandomPort("noprotocol://127.0.0.1");
} catch (ZMQException e) {
assertEquals(e.getErrorCode(), ZMQ.EPROTONOSUPPORT());
}
}
示例8: testReqRep
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
@Test
public void testReqRep() {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket in = context.socket(ZMQ.REQ);
in.bind("inproc://reqrep");
ZMQ.Socket out = context.socket(ZMQ.REP);
out.connect("inproc://reqrep");
for (int i = 0; i < 10; i++) {
byte[] req = ("request" + i).getBytes();
byte[] rep = ("reply" + i).getBytes();
assertTrue(in.send(req, 0));
byte[] reqTmp = out.recv(0);
assertArrayEquals(req, reqTmp);
assertTrue(out.send(rep, 0));
byte[] repTmp = in.recv(0);
assertArrayEquals(rep, repTmp);
}
}
示例9: testSetOption
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
/**
* Test method for various set/get options.
*/
@Test
public void testSetOption() {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket sock = context.socket(ZMQ.REQ);
if (ZMQ.getFullVersion() >= ZMQ.makeVersion(3, 2, 0)) {
sock.setIPv4Only(false);
assertEquals(false, sock.getIPv4Only());
sock.setIPv4Only(true);
assertEquals(true, sock.getIPv4Only());
}
sock.close();
context.term();
}
示例10: testRouterMandatory
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
/**
* Test method for Router Mandatory
*/
@Test
public void testRouterMandatory() {
if (ZMQ.getFullVersion() < ZMQ.makeVersion(3, 2, 0))
return;
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket sock = context.socket(ZMQ.ROUTER);
boolean ret = sock.sendMore("UNREACHABLE");
assertEquals(true, ret);
sock.send("END");
sock.setRouterMandatory(true);
try {
sock.sendMore("UNREACHABLE");
assertFalse(true);
} catch (ZMQException e) {
assertEquals(ZMQ.EHOSTUNREACH(), e.getErrorCode());
}
sock.close();
context.term();
}
示例11: testPollerUnregister
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
@Test
public void testPollerUnregister() {
Context context = ZMQ.context(1);
Socket socketOne = context.socket(ZMQ.SUB);
Socket socketTwo = context.socket(ZMQ.REP);
Poller poller = new ZMQ.Poller(2);
poller.register(socketOne, ZMQ.Poller.POLLIN);
poller.register(socketTwo, ZMQ.Poller.POLLIN);
socketOne.setLinger(0);
socketOne.close();
socketTwo.setLinger(0);
socketTwo.close();
poller.unregister(socketOne);
poller.unregister(socketTwo);
context.term();
}
示例12: main
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
public static void main (String[] args) throws InterruptedException {
Context context = ZMQ.context(1);
Socket sink = context.socket(ZMQ.ROUTER);
sink.bind("inproc://example");
// First allow 0MQ to set the identity, [00] + random 4byte
Socket anonymous = context.socket(ZMQ.REQ);
anonymous.connect("inproc://example");
anonymous.send ("ROUTER uses a generated UUID",0);
ZHelper.dump (sink);
// Then set the identity ourself
Socket identified = context.socket(ZMQ.REQ);
identified.setIdentity("Hello".getBytes (ZMQ.CHARSET));
identified.connect ("inproc://example");
identified.send("ROUTER socket uses REQ's socket identity", 0);
ZHelper.dump (sink);
sink.close ();
anonymous.close ();
identified.close();
context.term();
}
示例13: main
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
public static void main (String[] args) {
Context context = ZMQ.context(1);
// Socket to talk to server
Socket requester = context.socket(ZMQ.REQ);
requester.connect("tcp://localhost:5559");
System.out.println("launch and connect client.");
for (int request_nbr = 0; request_nbr < 10; request_nbr++) {
requester.send("Hello", 0);
String reply = requester.recvStr (0);
System.out.println("Received reply " + request_nbr + " [" + reply + "]");
}
// We never get here but clean up anyhow
requester.close();
context.term();
}
示例14: run
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
public void run()
{
Context context = ZMQ.context(1);
// Prepare our context and sockets
Socket client = context.socket(ZMQ.REQ);
ZHelper.setId (client); // Set a printable identity
client.connect("ipc://frontend.ipc");
// Send request, get reply
client.send("HELLO");
String reply = client.recvStr ();
System.out.println("Client: " + reply);
client.close();
context.term();
}
示例15: main
import org.zeromq.ZMQ.Context; //导入依赖的package包/类
public static void main (String[] args) {
Context context = ZMQ.context(1);
// Bind to inproc: endpoint, then start upstream thread
Socket receiver = context.socket(ZMQ.PAIR);
receiver.bind("inproc://step3");
// Step 2 relays the signal to step 3
Thread step2 = new Step2 (context);
step2.start();
// Wait for signal
receiver.recv(0);
receiver.close ();
System.out.println ("Test successful!");
context.term ();
}