本文整理汇总了Java中org.zeromq.ZMQ.Socket类的典型用法代码示例。如果您正苦于以下问题:Java Socket类的具体用法?Java Socket怎么用?Java Socket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Socket类属于org.zeromq.ZMQ包,在下文中一共展示了Socket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: receive
import org.zeromq.ZMQ.Socket; //导入依赖的package包/类
private Message receive(final Socket zmqSocket, final boolean failOnNull) throws IOException
{
byte[] frame = zmqSocket.recv();
if (frame == null)
{
if (failOnNull)
{
throw new IOException("Received null first frame after waiting " + receiveTimeoutMillis
+ "ms");
}
else
{
return null;
}
}
final List<byte[]> frames = new ArrayList<>();
do
{
frames.add(frame);
}
while (zmqSocket.hasReceiveMore() && ((frame = zmqSocket.recv()) != null));
return protocol.fromFrames(frames);
}
示例2: run
import org.zeromq.ZMQ.Socket; //导入依赖的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();
}
示例3: main
import org.zeromq.ZMQ.Socket; //导入依赖的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.Socket; //导入依赖的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.Socket; //导入依赖的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: zmq_bind
import org.zeromq.ZMQ.Socket; //导入依赖的package包/类
protected IConnection zmq_bind(boolean distributeZmq, int port) {
String url = null;
if (distributeZmq) {
if (ipc) {
url = "ipc://" + port + ".ipc";
} else {
url = "tcp://*:" + port;
}
} else {
// virtportZmq will be true
url = "inproc://" + port;
}
Socket socket = ZeroMq.socket(context, ZeroMq.pull);
ZeroMq.bind(socket, url);
ZeroMq.set_hwm(socket, maxQueueMsg);
// ZeroMq.subscribe(socket);
LOG.info("Create zmq receiver {}", url);
return new ZMQRecvConnection(socket);
}
示例7: zmq_connect
import org.zeromq.ZMQ.Socket; //导入依赖的package包/类
protected IConnection zmq_connect(boolean distributeZmq, String host, int port) {
String url = null;
if (distributeZmq) {
if (ipc) {
url = "ipc://" + port + ".ipc";
} else {
url = "tcp://" + host + ":" + port;
}
} else {
// virtportZmq will be true
url = "inproc://" + port;
}
Socket socket = ZeroMq.socket(context, ZeroMq.push);
socket = ZeroMq.set_linger(socket, linger_ms);
socket = ZeroMq.connect(socket, url);
ZeroMq.set_hwm(socket, maxQueueMsg);
LOG.info("Create zmq sender {}", url);
return new ZMQSendConnection(socket, host, port);
}
示例8: main
import org.zeromq.ZMQ.Socket; //导入依赖的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();
}
示例9: main
import org.zeromq.ZMQ.Socket; //导入依赖的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();
}
示例10: handShake
import org.zeromq.ZMQ.Socket; //导入依赖的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]);
}
示例11: createJoiners
import org.zeromq.ZMQ.Socket; //导入依赖的package包/类
private void createJoiners() {
int numJoiners = joiners.length;
int entriesHint = (int) (totalRecordsHint / numJoiners / 0.7);
for (int i = 0; i < numJoiners; ++i) {
String socketString = "ipc://joiner-" + i;
ComputationalWorker worker = new ComputationalWorker(socketString, outputString, entriesHint);
worker.start();
workers[i] = worker;
Socket socket = context.createSocket(ZMQ.PUSH);
socket.connect(socketString);
joiners[i] = socket;
}
}
示例12: connectSubSocket
import org.zeromq.ZMQ.Socket; //导入依赖的package包/类
Socket connectSubSocket() {
Socket sub = ctx.socket(ZMQ.SUB);
if (handlers.isEmpty()) {
sub.subscribe("".getBytes());
} else {
for (String event : handlers.keySet()) {
sub.subscribe(event.getBytes());
}
}
sub.connect(pub);
return sub;
}
示例13: run
import org.zeromq.ZMQ.Socket; //导入依赖的package包/类
@Override
public void run()
{
ZContext ctx = new ZContext();
Socket worker = ctx.createSocket(ZMQ.REQ);
worker.connect(String.format("ipc://%s-localbe.ipc", self));
// Tell broker we're ready for work
ZFrame frame = new ZFrame(WORKER_READY);
frame.send(worker, 0);
while (true) {
// Send request, get reply
ZMsg msg = ZMsg.recvMsg(worker, 0);
if (msg == null)
break; // Interrupted
msg.getLast().print("Worker: ");
msg.getLast().reset("OK");
msg.send(worker);
}
ctx.destroy();
}
示例14: main
import org.zeromq.ZMQ.Socket; //导入依赖的package包/类
public static void main (String[] args) {
Context context = ZMQ.context(1);
// First, connect our subscriber socket
Socket subscriber = context.socket(ZMQ.SUB);
subscriber.connect("tcp://localhost:5561");
subscriber.subscribe(ZMQ.SUBSCRIPTION_ALL);
// Second, synchronize with publisher
Socket syncclient = context.socket(ZMQ.REQ);
syncclient.connect("tcp://localhost:5562");
// - send a synchronization request
syncclient.send(ZMQ.MESSAGE_SEPARATOR, 0);
// - wait for synchronization reply
syncclient.recv(0);
// Third, get our updates and report how many we got
int update_nbr = 0;
while (true) {
String string = subscriber.recvStr(0);
if (string.equals("END")) {
break;
}
update_nbr++;
}
System.out.println("Received " + update_nbr + " updates.");
subscriber.close();
syncclient.close();
context.term();
}
示例15: main
import org.zeromq.ZMQ.Socket; //导入依赖的package包/类
public static void main (String[] args) {
Context context = ZMQ.context(1);
Socket clients = context.socket(ZMQ.ROUTER);
clients.bind ("tcp://*:5555");
Socket workers = context.socket(ZMQ.DEALER);
workers.bind ("inproc://workers");
for(int thread_nbr = 0; thread_nbr < 5; thread_nbr++) {
Thread worker = new Worker (context);
worker.start();
}
// Connect work threads to client threads via a queue
ZMQ.proxy (clients, workers, null);
// We never get here but clean up anyhow
clients.close();
workers.close();
context.term();
}