ZeroMQ通信:复杂请求-应答模式 - ROUTER,DEALER代理

ZeroMQ通信:复杂请求-应答模式 - ROUTER,DEALER代理_第1张图片
再REQ和REP中间加入代理:ROUTER和DEALER

client

package com.dsf.adapter.ZZtest;

import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Socket;
import org.zeromq.ZContext;

public class Client {
    public static void main(String[] args) {
        System.out.println("client begin");

        ZContext zContext = new ZContext(1);
        Socket socket = zContext.createSocket(ZMQ.REQ);
        socket.connect("tcp://*:5559");

        for (int i = 0; i < 10000; i++) {
            String request = "hello";
            socket.send(request.getBytes());
            byte[] response = socket.recv();
            System.out.println("receive : " + new String(response));
        }

    }
}

server

package com.dsf.adapter.ZZtest;

import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Socket;
import org.zeromq.ZContext;

public class Server {
    public static void main(String[] args) {
        System.out.println("server begin");

        ZContext zContext = new ZContext(1);
        Socket socket = zContext.createSocket(ZMQ.REP);
        socket.connect("tcp://*:5560");

        while (!Thread.currentThread().isInterrupted()) {
            byte[] request = socket.recv();
            System.out.println("receive : " + new String(request));
            String response = "world";
            socket.send(response.getBytes());
        }
        socket.close();
    }
}

proxy

package com.dsf.adapter.ZZtest;

import org.zeromq.ZContext;
import org.zeromq.ZMQ.Socket;
import org.zeromq.ZMQ;
import org.zeromq.ZMsg;
import zmq.SocketBase;
import zmq.poll.PollItem;

import java.nio.channels.Selector;

public class Proxy {
    public static void main(String[] args) {
        System.out.println("proxy begin");

        ZContext zContext = new ZContext(1);
        Socket frontend = zContext.createSocket(ZMQ.ROUTER);
        Socket backend = zContext.createSocket(ZMQ.DEALER);
        frontend.bind("tcp://*:5559");
        backend.bind("tcp://*:5560");
    
        PollItem[] items = new PollItem[2];

        items[0] = new PollItem(frontend.base(), zmq.ZMQ.ZMQ_POLLIN);
        items[1] = new PollItem(backend.base(), zmq.ZMQ.ZMQ_POLLIN);
        PollItem[] itemsout = new PollItem[2];

        itemsout[0] = new PollItem(frontend.base(), zmq.ZMQ.ZMQ_POLLOUT);
        itemsout[1] = new PollItem(backend.base(), zmq.ZMQ.ZMQ_POLLOUT);

        Selector selector = zContext.createSelector();

        while (!Thread.currentThread().isInterrupted()) {
            zmq.ZMQ.poll(selector, items,1L);
            zmq.ZMQ.poll(selector, itemsout, 1L);

            if (process(items[0], itemsout[1], frontend.base(), backend.base())) {
                ZMsg msg = ZMsg.recvMsg(frontend);
                System.out.println("client to server:"+msg);
                msg.send(backend);
            }

            if (process(items[1], itemsout[0], frontend.base(), backend.base())) {
                ZMsg msg = ZMsg.recvMsg(backend);
                System.out.println("server to client"+msg);
                msg.send(frontend);
            }
        }
        frontend.close();
        backend.close();
    }

    private static boolean process(PollItem read, PollItem write, SocketBase frontend, SocketBase backend) {
        return read.isReadable() && (frontend == backend || write.isWritable());
    }
}

或者直接用ZMQ.proxy

package com.dsf.adapter.ZZtest;

import org.zeromq.ZContext;
import org.zeromq.ZMQ.Socket;
import org.zeromq.ZMQ;
import org.zeromq.ZMsg;
import zmq.SocketBase;
import zmq.poll.PollItem;

import java.nio.channels.Selector;

public class Proxy {
    public static void main(String[] args) {
        System.out.println("proxy begin");

        ZContext zContext = new ZContext(1);
        Socket frontend = zContext.createSocket(ZMQ.ROUTER);
        Socket backend = zContext.createSocket(ZMQ.DEALER);
        frontend.bind("tcp://*:5559");
        backend.bind("tcp://*:5560");
        ZMQ.proxy(frontend,backend,null);

        frontend.close();
        backend.close();
    }

    }
}

ZeroMQ通信:复杂请求-应答模式 - ROUTER,DEALER代理_第2张图片 ZeroMQ通信:复杂请求-应答模式 - ROUTER,DEALER代理_第3张图片
ZeroMQ通信:复杂请求-应答模式 - ROUTER,DEALER代理_第4张图片

你可能感兴趣的:(ZeroMQ,Java)