当前位置: 首页>>代码示例>>Java>>正文


Java ZContext.destroySocket方法代码示例

本文整理汇总了Java中org.zeromq.ZContext.destroySocket方法的典型用法代码示例。如果您正苦于以下问题:Java ZContext.destroySocket方法的具体用法?Java ZContext.destroySocket怎么用?Java ZContext.destroySocket使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.zeromq.ZContext的用法示例。


在下文中一共展示了ZContext.destroySocket方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: 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);
}
 
开发者ID:zeromq,项目名称:jeromq3-x,代码行数:19,代码来源:espresso.java

示例2: tryRequest

import org.zeromq.ZContext; //导入方法依赖的package包/类
private static ZMsg tryRequest (ZContext ctx, String endpoint, ZMsg request)
{
    System.out.printf("I: trying echo service at %s...\n", endpoint);
    Socket client = ctx.createSocket(ZMQ.REQ);
    client.connect(endpoint);

    //  Send request, wait safely for reply
    ZMsg msg = request.duplicate();
    msg.send(client);
    PollItem[] items = { new PollItem(client, ZMQ.Poller.POLLIN) };
    ZMQ.poll(items, REQUEST_TIMEOUT);
    ZMsg reply = null;
    if (items[0].isReadable())
        reply = ZMsg.recvMsg(client);

    //  Close socket in any case, we're done with it now
    ctx.destroySocket(client);
    return reply;
}
 
开发者ID:zeromq,项目名称:jeromq3-x,代码行数:20,代码来源:flclient1.java

示例3: run

import org.zeromq.ZContext; //导入方法依赖的package包/类
@Override
public void run() {
    ZContext ctx = new ZContext();

    //  Frontend socket talks to clients over TCP
    Socket frontend = ctx.createSocket(ZMQ.ROUTER);
    frontend.bind("tcp://*:5570");

    //  Backend socket talks to workers over inproc
    Socket backend = ctx.createSocket(ZMQ.DEALER);
    backend.bind("inproc://backend");

    //  Launch pool of worker threads, precise number is not critical
    for (int threadNbr = 0; threadNbr < requestHanlderPoolSize; threadNbr++) {
        try {
            new Thread(new server_worker(ctx)).start();
        } catch (Exception e) {
            // Does nothing as a warning was already issued.
        }
    }

    //  Connect backend to frontend via a proxy
    ZMQ.proxy(frontend, backend, null);

    LOG.info("ZMQNBIServerPool - Exiting MAIN server thread");
    frontend.unbind("tcp://*:5570");
    backend.unbind("inproc://backend");
    ctx.destroySocket(frontend);
    ctx.destroySocket(backend);
    ctx.destroy();
}
 
开发者ID:opendaylight,项目名称:fpc,代码行数:32,代码来源:ZMQNBIServerPool.java

示例4: main

import org.zeromq.ZContext; //导入方法依赖的package包/类
public static void 
main (String[] args) throws Exception
{
    Random random = new Random ();
    //  Initialize context for talking to tasks
    ZContext ctx = new ZContext ();
    ctx.setLinger (100);

    //  Get number of interfaces to simulate, default 100
    int max_interface = 100;
    int nbr_interfaces = 0;
    int max_tries = -1;
    int nbr_tries = 0;
    if (args.length > 0)
        max_interface = Integer.parseInt (args [0]);
    if (args.length > 1)
        max_tries = Integer.parseInt (args [1]);

    //  We address interfaces as an array of pipes
    Socket [] pipes = new Socket [max_interface];

    //  We will randomly start and stop interface threads
    while (!Thread.currentThread ().isInterrupted ()) {
        int index = random.nextInt (max_interface);
        //  Toggle interface thread
        if (pipes [index] != null) {
            pipes [index].send ("STOP");
            ctx.destroySocket (pipes [index]);
            pipes [index] = null;
            System.out.printf ("I: Stopped interface (%d running)\n", --nbr_interfaces);
        }
        else {
            pipes [index] = ZThread.fork (ctx, new InterfaceTask ());
            System.out.printf ("I: Started interface (%d running)\n", ++nbr_interfaces);
        }
        if (max_tries > 0 && ++nbr_tries >= max_tries)
            break;
        //  Sleep ~750 msecs randomly so we smooth out activity
        Thread.sleep (random.nextInt (500) + 500);
    }
    System.out.printf ("I: Stopped tester (%d tries)\n", nbr_tries);
    ctx.destroy ();
}
 
开发者ID:zeromq,项目名称:jyre,代码行数:44,代码来源:ZreTester.java

示例5: main

import org.zeromq.ZContext; //导入方法依赖的package包/类
public static void main(String[] argv)
{
    ZContext ctx = new ZContext();
    System.out.println("I: connecting to server");
    Socket client = ctx.createSocket(ZMQ.REQ);
    assert (client != null);
    client.connect(SERVER_ENDPOINT);

    int sequence = 0;
    int retriesLeft = REQUEST_RETRIES;
    while (retriesLeft > 0 && !Thread.currentThread().isInterrupted()) {
        //  We send a request, then we work to get a reply
        String request = String.format("%d", ++sequence);
        client.send(request);

        int expect_reply = 1;
        while (expect_reply > 0) {
            //  Poll socket for a reply, with timeout
            PollItem items[] = {new PollItem(client, Poller.POLLIN)};
            int rc = ZMQ.poll(items, REQUEST_TIMEOUT);
            if (rc == -1)
                break;          //  Interrupted

            //  Here we process a server reply and exit our loop if the
            //  reply is valid. If we didn't a reply we close the client
            //  socket and resend the request. We try a number of times
            //  before finally abandoning:

            if (items[0].isReadable()) {
                //  We got a reply from the server, must match getSequence
                String reply = client.recvStr();
                if (reply == null)
                    break;      //  Interrupted
                if (Integer.parseInt(reply) == sequence) {
                    System.out.printf("I: server replied OK (%s)\n", reply);
                    retriesLeft = REQUEST_RETRIES;
                    expect_reply = 0;
                } else
                    System.out.printf("E: malformed reply from server: %s\n",
                            reply);

            } else if (--retriesLeft == 0) {
                System.out.println("E: server seems to be offline, abandoning\n");
                break;
            } else {
                System.out.println("W: no response from server, retrying\n");
                //  Old socket is confused; close it and open a new one
                ctx.destroySocket(client);
                System.out.println("I: reconnecting to server\n");
                client = ctx.createSocket(ZMQ.REQ);
                client.connect(SERVER_ENDPOINT);
                //  Send request again, on new socket
                client.send(request);
            }
        }
    }
    ctx.destroy();
}
 
开发者ID:zeromq,项目名称:jeromq3-x,代码行数:59,代码来源:lpclient.java

示例6: main

import org.zeromq.ZContext; //导入方法依赖的package包/类
public static void main(String[] argv) throws Exception
{
    ZContext ctx = new ZContext();

    String[] server = { "tcp://localhost:5001", "tcp://localhost:5002" };
    int serverNbr = 0;

    System.out.printf ("I: connecting to server at %s...\n", server [serverNbr]);
    Socket client = ctx.createSocket(ZMQ.REQ);
    client.connect(server[serverNbr]);

    int sequence = 0;
    while (!Thread.currentThread().isInterrupted()) {
        //  We send a request, then we work to get a reply
        String request = String.format("%d", ++sequence);
        client.send(request);

        boolean expectReply = true;
        while (expectReply) {
            //  Poll socket for a reply, with timeout
            PollItem items [] = { new PollItem(client, ZMQ.Poller.POLLIN) };
            int rc = ZMQ.poll(items, 1, REQUEST_TIMEOUT);
            if (rc == -1)
                break;          //  Interrupted

            //  .split main body of client
            //  We use a Lazy Pirate strategy in the client. If there's no
            //  reply within our timeout, we close the socket and try again.
            //  In Binary Star, it's the client vote that decides which
            //  server is primary; the client must therefore try to connect
            //  to each server in turn:

            if (items[0].isReadable()) {
                //  We got a reply from the server, must match getSequence
                String reply = client.recvStr();
                if (Integer.parseInt(reply) == sequence) {
                    System.out.printf ("I: server replied OK (%s)\n", reply);
                    expectReply = false;
                    Thread.sleep(1000);  //  One request per second
                }
                else
                    System.out.printf ("E: bad reply from server: %s\n", reply);
            }
            else {
                System.out.printf ("W: no response from server, failing over\n");

                //  Old socket is confused; close it and open a new one
                ctx.destroySocket(client);
                serverNbr = (serverNbr + 1) % 2;
                Thread.sleep(SETTLE_DELAY);
                System.out.printf("I: connecting to server at %s...\n",
                        server[serverNbr]);
                client = ctx.createSocket(ZMQ.REQ);
                client.connect(server[serverNbr]);

                //  Send request again, on new socket
                client.send(request);
            }
        }
    }
    ctx.destroy();

}
 
开发者ID:zeromq,项目名称:jeromq3-x,代码行数:64,代码来源:bstarcli.java


注:本文中的org.zeromq.ZContext.destroySocket方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。