本文整理汇总了Java中org.zeromq.ZContext类的典型用法代码示例。如果您正苦于以下问题:Java ZContext类的具体用法?Java ZContext怎么用?Java ZContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ZContext类属于org.zeromq包,在下文中一共展示了ZContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ZMQProtobufClient
import org.zeromq.ZContext; //导入依赖的package包/类
ZMQProtobufClient(String zurl) {
System.out.println("Starting zmq client to " + zurl);
ctx = new ZContext();
// We use ROUTER rather than DEALER because we want ZMQ to drop packets
// (rather than blocking)
// if we are not connected to the server.
socket = ctx.createSocket(ZMQ.ROUTER);
socket.setHWM(20);
socket.setLinger(200); // in msecs
// Use the following to get better client ids (must be unique)
String identity = UUID.randomUUID().toString();
socket.setIdentity(identity.getBytes(ZMQ.CHARSET));
socket.connect(zurl);
items = new PollItem[] { new PollItem(socket, Poller.POLLIN) };
}
示例2: main
import org.zeromq.ZContext; //导入依赖的package包/类
public static void main(String[] args)
{
ZContext context = new ZContext();
Socket subscriber = context.createSocket(ZMQ.SUB);
if (args.length == 1)
subscriber.connect(args[0]);
else
subscriber.connect("tcp://localhost:5556");
Random rand = new Random(System.currentTimeMillis());
String subscription = String.format("%03d", rand.nextInt(1000));
subscriber.subscribe(subscription.getBytes(ZMQ.CHARSET));
while (true) {
String topic = subscriber.recvStr();
if (topic == null)
break;
String data = subscriber.recvStr();
assert(topic.equals(subscription));
System.out.println(data);
}
context.destroy();
}
示例3: run
import org.zeromq.ZContext; //导入依赖的package包/类
@Override
public void run(Object[] args, ZContext ctx, Socket pipe)
{
// Subscribe to "A" and "B"
Socket subscriber = ctx.createSocket(ZMQ.SUB);
subscriber.connect("tcp://localhost:6001");
subscriber.subscribe("A".getBytes(ZMQ.CHARSET));
subscriber.subscribe("B".getBytes(ZMQ.CHARSET));
int count = 0;
while (count < 5) {
String string = subscriber.recvStr();
if (string == null)
break; // Interrupted
count++;
}
ctx.destroySocket(subscriber);
}
示例4: run
import org.zeromq.ZContext; //导入依赖的package包/类
public void run() {
ZContext ctx = new ZContext();
Socket subscriber = ctx.createSocket(ZMQ.SUB);
subscriber.connect("tcp://localhost:5556");
subscriber.subscribe(ZMQ.SUBSCRIPTION_ALL);
while (true) {
kvsimple kvMsg = kvsimple.recv(subscriber);
if (kvMsg == null)
break;
clonecli1.kvMap.put(kvMsg.getKey(), kvMsg);
System.out.println("receiving " + kvMsg);
sequence.incrementAndGet();
}
ctx.destroy();
}
示例5: Session
import org.zeromq.ZContext; //导入依赖的package包/类
public Session(final String userName,
final Protocol protocol,
final int receiveTimeoutMillis,
final int zmqIoThreads)
{
id = UUID.randomUUID().toString();
this.userName = userName;
this.protocol = notNull(protocol, "protocol can't be null");
Validate.isTrue(receiveTimeoutMillis >= -1,
"receiveTimeoutMillis must be -1, 0 or a positive integer");
this.receiveTimeoutMillis = receiveTimeoutMillis;
Validate.isTrue(zmqIoThreads >= 1, "zmqIoThreads must be a positive integer");
zmqContext = new ZContext(zmqIoThreads);
channels = new HashSet<>();
executor = Executors.newCachedThreadPool(new SessionThreadFactory());
scheduler = Executors.newScheduledThreadPool(1, new SessionThreadFactory());
LOGGER.info("Created session ID is: {}", id);
}
示例6: main
import org.zeromq.ZContext; //导入依赖的package包/类
/**
* @param args
*/
public static void main( String[] args )
{
final ZContext ctx = new ZContext();
final Socket client = ctx.createSocket( ZMQ.PAIR );
//client.connect( "ipc:///tmp/zmqtest" );
client.connect( "tcp://localhost:5556" );
String msg = "";
for ( int i = 0; i < 10; ++i )
{
client.send( "Hello " + i );
}
client.send( "exit" );
ctx.destroy();
}
示例7: main
import org.zeromq.ZContext; //导入依赖的package包/类
/**
* @param args
*/
public static void main( String[] args )
{
final ZContext ctx = new ZContext();
final Socket server = ctx.createSocket( ZMQ.PAIR );
server.bind( "ipc:///tmp/zmqtest" );
// server.bind( "tcp://*:5556" );
System.out.println( "bind complete" );
String msg = "";
while ( !msg.equals( "exit" ) )
{
msg = server.recvStr();
System.out.println( msg );
}
ctx.destroy();
}
示例8: main
import org.zeromq.ZContext; //导入依赖的package包/类
public static void main(String args[]) {
initLogger();
InstrumentCmd instrumentCmd = new InstrumentCmd();
instrumentCmd.setDescription("This command clears the scanner memory");
instrumentCmd.setHaltOnErr(true);
instrumentCmd.setInstrument(Instrument.SCANNER);
instrumentCmd.setOpcode(0);
theContext = new ZContext();
createSubscriber();
sendControlCommand(instrumentCmd);
System.out.println("P: exit");
System.exit(0);
}
示例9: run
import org.zeromq.ZContext; //导入依赖的package包/类
@Override
public void run() {
ctx = new ZContext();
frontend = ctx.createSocket(ZMQ.ROUTER);
frontend.bind("tcp://*:5570");
backend = ctx.createSocket(ZMQ.DEALER);
backend.bind("inproc://backend");
// Launch pool of worker threads, precise number is not critical
for (int threadNbr = 0; threadNbr < 2; threadNbr++) {
new Thread(new JeroMQServerWorker(ctx)).start();
}
// Connect backend to frontend via a proxy
ZMQ.proxy(frontend, backend, null);
}
示例10: run
import org.zeromq.ZContext; //导入依赖的package包/类
@Override
public void run() {
context = new ZContext();
clientSocket = context.createSocket(ZMQ.PAIR);
try {
clientSocket.bind("tcp://*:" + incomingPort);
logger.info("ComputationalServer UP");
while (!Thread.currentThread().isInterrupted())
receiveRequest();
} catch (Exception e) {
e.printStackTrace();
} finally {
context.destroy();
logger.info("ComputationalServer DOWN");
}
}
示例11: handShake
import org.zeromq.ZContext; //导入依赖的package包/类
public void handShake() throws Exception {
context = new ZContext();
Socket server = context.createSocket(ZMQ.REQ);
server.connect(connectionString);
server.send(request.toMsg());
String reply = new String(server.recv());
context.destroy();
if (reply.startsWith("ERROR"))
throw new Exception(reply);
String[] parts = reply.split(" ");
port = Integer.parseInt(parts[0]);
recordsHint = Integer.parseInt(parts[1]);
}
示例12: Agent
import org.zeromq.ZContext; //导入依赖的package包/类
private Agent (ZContext ctx, Socket pipe, Socket inbox,
ZreUdp udp, int port)
{
this.ctx = ctx;
this.pipe = pipe;
this.inbox = inbox;
this.udp = udp;
this.port = port;
host = udp.host ();
uuid = UUID.randomUUID ();
identity = uuidStr (uuid);
endpoint = String.format ("%s:%d", host, port);
peers = new HashMap <String, ZrePeer> ();
peer_groups = new HashMap <String, ZreGroup> ();
own_groups = new HashMap <String, ZreGroup> ();
headers = new HashMap <String, String> ();
log = new ZreLog (endpoint);
}
示例13: newAgent
import org.zeromq.ZContext; //导入依赖的package包/类
protected static Agent newAgent (ZContext ctx, Socket pipe)
{
Socket inbox = ctx.createSocket (ZMQ.ROUTER);
if (inbox == null) // Interrupted
return null;
ZreUdp udp = new ZreUdp (PING_PORT_NUMBER);
int port = inbox.bindToRandomPort ("tcp://*", 0xc000, 0xffff);
if (port < 0) { // Interrupted
System.err.println ("Failed to bind a random port");
udp.destroy ();
return null;
}
return new Agent (ctx, pipe, inbox, udp, port);
}
示例14: testBroadcast
import org.zeromq.ZContext; //导入依赖的package包/类
@Test
public void
testBroadcast () throws Exception
{
ZContext ctx = new ZContext ();
ctx.setContext (ZMQ.context (1));
assert (ctx != null);
UdpAgent udp1 = new UdpAgent (ctx);
udp1.start ();
Thread.sleep (1000);
DatagramSocket sock = new DatagramSocket(null);
sock.setBroadcast (true);
for (int i = 0; i < MAX_REQUESTS; i++) {
DatagramPacket packet =
new DatagramPacket ("Hello".getBytes (), 0, 5, InetAddress.getByName ("255.255.255.255"), PORT);
sock.send (packet);
}
Thread.sleep (1000);
ctx.destroy ();
udp1.join ();
sock.close ();
}
示例15: main
import org.zeromq.ZContext; //导入依赖的package包/类
public static void main(String[] args)
{
if (args.length < 1) {
System.out.printf("I: syntax: flserver1 <endpoint>\n");
System.exit(0);
}
ZContext ctx = new ZContext();
Socket server = ctx.createSocket(ZMQ.REP);
server.bind(args[0]);
System.out.printf ("I: echo service is ready at %s\n", args[0]);
while (true) {
ZMsg msg = ZMsg.recvMsg(server);
if (msg == null)
break; // Interrupted
msg.send(server);
}
if (Thread.currentThread().isInterrupted())
System.out.printf ("W: interrupted\n");
ctx.destroy();
}