本文整理汇总了Java中org.zeromq.ZMQ.Socket.recv方法的典型用法代码示例。如果您正苦于以下问题:Java Socket.recv方法的具体用法?Java Socket.recv怎么用?Java Socket.recv使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.zeromq.ZMQ.Socket
的用法示例。
在下文中一共展示了Socket.recv方法的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: 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]);
}
示例3: recv
import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
/**
* Internal method to call recv on the socket.
* Does not trap any ZMQExceptions but expects caling routine to handle them.
* @param socket
* 0MQ socket to read from
* @return
* byte[] data
*/
private byte[] recv(Socket socket, int flags) {
if (socket == null)
throw new IllegalArgumentException("socket parameter must not be null");
data = socket.recv(flags);
more = socket.hasReceiveMore();
return data;
}
示例4: dump
import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
/**
* Receives all message parts from socket, prints neatly
*/
public static void dump (Socket sock)
{
System.out.println("----------------------------------------");
while(true) {
byte [] msg = sock.recv (0);
boolean isText = true;
String data = "";
for (int i = 0; i< msg.length; i++) {
if (msg[i] < 32 || msg[i] > 127)
isText = false;
data += String.format ("%02X", msg[i]);
}
if (isText)
data = new String (msg, ZMQ.CHARSET);
System.out.println (String.format ("[%03d] %s", msg.length, data));
if (!sock.hasReceiveMore ())
break;
}
}
示例5: run
import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
@Override
public void run(){
// Bind to inproc: endpoint, then start upstream thread
Socket receiver = context.socket(ZMQ.PAIR);
receiver.bind("inproc://step2");
Thread step1 = new Step1 (context);
step1.start();
// Wait for signal
receiver.recv(0);
receiver.close ();
// Connect to step3 and tell it we're ready
Socket xmitter = context.socket(ZMQ.PAIR);
xmitter.connect("inproc://step3");
xmitter.send("READY", 0);
xmitter.close ();
}
示例6: main
import org.zeromq.ZMQ.Socket; //导入方法依赖的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 ();
}
示例7: 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();
}
示例8: runKernelDebug
import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
public static void runKernelDebug(Session session) throws FileNotFoundException,
InvalidKeyException,
UnsupportedEncodingException,
IOException {
Session._DEBUG_ = true;
ZContext ctx = new ZContext();
Socket channel = ctx.createSocket(ZMQ.REP);
channel.bind("tcp://127.0.0.1:2222");
byte[] msg = channel.recv();
String sArgs = new String(msg, StandardCharsets.UTF_8);
String[] newArgs = sArgs.split(" ");
channel.send("ok");
runKernel(session, newArgs);
}
示例9: receiveRequest
import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
private void receiveRequest() {
try {
// TODO create a connection directly to the client (ROUTER/DEALER)
String topologyName = "topology-" + (++topologyCounter);
JoinerTopology topology = new JoinerTopology(topologyName, joinerParallelism, numWorkers);
for (DataServerConnector connector: receiveConnectors()) {
int port = connector.handShake();
logger.info("Assigned port: {} => {}", connector.getConnectionString(), port);
topology.addSpout(connector.getConnectionString().replaceFirst(":\\d+", ":" + port), spoutParallelism);
}
topology.start();
// TODO to be removed when we have a connection directly to the client
Socket topologySocket = context.createSocket(ZMQ.PAIR);
topologySocket.bind("ipc://" + topologyName);
while (true) {
byte[] message = topologySocket.recv();
clientSocket.send(message);
if (message.length == 0)
break;
}
} catch (Exception e) {
clientSocket.send("ERROR: " + e.getMessage());
}
}
示例10: run
import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
@Override
public void run() {
context = new ZContext();
Socket killerSocket = context.createSocket(ZMQ.PAIR);
killerSocket.bind(killerSocketString);
// wait for the job to be completed
killerSocket.recv();
kill();
context.destroy();
}
示例11: done
import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
public void done() {
context = new ZContext();
Socket server = context.createSocket(ZMQ.REQ);
server.connect(connectionString);
server.send("DONE " + port);
server.recv(); // ACK
context.destroy();
}
示例12: recv
import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
/**
* Internal method to call recv on the socket. Does not trap any ZMQExceptions but expects caling routine to handle
* them.
*
* @param socket 0MQ socket to read from
* @return Byte array
*/
private byte[] recv(Socket socket, int flags) {
if (socket == null)
throw new IllegalArgumentException("socket parameter must not be null");
data = socket.recv(flags);
more = socket.hasReceiveMore();
return data;
}
示例13: main
import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
/**
* While this example runs in a single process, that is just to make
* it easier to start and stop the example. Each thread has its own
* context and conceptually acts as a separate process.
*/
public static void main (String[] args) throws Exception {
Context context = ZMQ.context(1);
Socket broker = context.socket(ZMQ.ROUTER);
broker.bind("tcp://*:5671");
for (int workerNbr = 0; workerNbr < NBR_WORKERS; workerNbr++)
{
Thread worker = new Worker ();
worker.start ();
}
// Run for five seconds and then tell workers to end
long endTime = System.currentTimeMillis () + 5000;
int workersFired = 0;
while (true) {
// Next message gives us least recently used worker
String identity = broker.recvStr ();
broker.sendMore (identity);
broker.recv (0); // Envelope delimiter
broker.recv (0); // Response from worker
broker.sendMore ("");
// Encourage workers until it's time to fire them
if (System.currentTimeMillis () < endTime)
broker.send ("Work harder");
else {
broker.send ("Fired!");
if (++workersFired == NBR_WORKERS)
break;
}
}
broker.close();
context.term();
}
示例14: recv
import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
public static kvmsg recv(Socket socket)
{
// This method is almost unchanged from kvsimple
// .skip
assert (socket != null);
kvmsg self = new kvmsg(0);
// Read all frames off the wire, reject if bogus
int frameNbr;
for (frameNbr = 0; frameNbr < KVMSG_FRAMES; frameNbr++) {
//zmq_msg_init (&self->frame [frameNbr]);
self.present[frameNbr] = true;
if ((self.frame[frameNbr] = socket.recv(0)) == null) {
self.destroy();
break;
}
// Verify multipart framing
boolean rcvmore = (frameNbr < KVMSG_FRAMES - 1)? true: false;
if (socket.hasReceiveMore() != rcvmore) {
self.destroy();
break;
}
}
// .until
self.decodeProps();
return self;
}
示例15: main
import org.zeromq.ZMQ.Socket; //导入方法依赖的package包/类
public static void main(String[] argv)
{
// First argument is this broker's name
// Other arguments are our peers' names
//
if (argv.length < 1) {
System.out.println("syntax: peering1 me {you}\n");
System.exit(-1);
}
String self = argv[0];
System.out.println(String.format("I: preparing broker at %s\n", self));
Random rand = new Random(System.nanoTime());
ZContext ctx = new ZContext();
// Bind state backend to endpoint
Socket statebe = ctx.createSocket(ZMQ.PUB);
statebe.bind(String.format("ipc://%s-state.ipc", self));
// Connect statefe to all peers
Socket statefe = ctx.createSocket(ZMQ.SUB);
statefe.subscribe(ZMQ.SUBSCRIPTION_ALL);
int argn;
for (argn = 1; argn < argv.length; argn++) {
String peer = argv[argn];
System.out.printf("I: connecting to state backend at '%s'\n", peer);
statefe.connect(String.format("ipc://%s-state.ipc", peer));
}
// The main loop sends out status messages to peers, and collects
// status messages back from peers. The zmq_poll timeout defines
// our own heartbeat:
while (true) {
// Poll for activity, or 1 second timeout
PollItem items[] = {new PollItem(statefe, Poller.POLLIN)};
int rc = ZMQ.poll(items, 1000);
if (rc == -1)
break; // Interrupted
// Handle incoming status messages
if (items[0].isReadable()) {
String peer_name = new String(statefe.recv(0), ZMQ.CHARSET);
String available = new String(statefe.recv(0), ZMQ.CHARSET);
System.out.printf("%s - %s workers free\n", peer_name, available);
} else {
// Send random values for worker availability
statebe.send(self, ZMQ.SNDMORE);
statebe.send(String.format("%d", rand.nextInt(10)), 0);
}
}
ctx.destroy();
}