0

Can the dealer socket created in the same class be used to send messages in other methods of the same class? If yes, then how is it supposed to be used? Example code:

private static class client_task implements Runnable
{

    @Override
    public void run()
    {
        ZContext ctx = new ZContext();
        Socket client = ctx.createSocket(ZMQ.DEALER);

        //  Set random identity to make tracing easier
        String identity = String.format("%04X-%04X", rand.nextInt(), rand.nextInt());
        client.setIdentity(identity.getBytes(ZMQ.CHARSET));
        client.connect("tcp://localhost:5570");

        Poller poller = ctx.createPoller(1);
        poller.register(client, Poller.POLLIN);

        int requestNbr = 0;
        while (!Thread.currentThread().isInterrupted()) {
            //  Tick once per second, pulling in arriving messages
            for (int centitick = 0; centitick < 100; centitick++) {
                poller.poll(10);
                if (poller.pollin(0)) {
                    ZMsg msg = ZMsg.recvMsg(client);
                    msg.getLast().print(identity);
                    msg.destroy();
                }
            }
            client.send(String.format("request #%d", ++requestNbr), 0);
        }
        ctx.close();
    }
}

So, I want to use this client.send(String.format("request #%d", ++requestNbr), 0); in other methods within same class.

Imran Ali
  • 2,223
  • 2
  • 28
  • 41
CuriQ
  • 61
  • 2
  • 5
  • 1
    Yes, of course you can: just make it an instance member of the class and don't close it here. This is trivially obvious. – user207421 Jun 16 '17 at 23:45
  • I am new to zeromq, so I need more clarity on this. So, client.send needs to be within while(!Thread.currentThread().isInterrupted()) or can it be used directly within the other methods in the class?Example code will be more helpful. Thanks. – CuriQ Jun 17 '17 at 00:11
  • No *( cit.:) "**Example code will be more helpful.***" **never works** if one's mind has not undertaken the insights of concept. **There is no bridge raised that has been started from the middle of nothing**. The best next step is not to seek for a few SLOCs from some irresponsible schoolbook example, the less from web/blogs, the best next step for anyone, who is serious into a distributed computing, is to **take due care to start understand, before sketching any code** ( May like for this purpose **a must read book** mentioned in https://stackoverflow.com/a/37260714 ) **Worth time & efforts** – user3666197 Jun 17 '17 at 12:18

1 Answers1

0

Yes, can.


But:

Users ought pay more attention on real-world instantiation & dismantling costs of the ZeroMQ infrastructure elements & their associated operation overheads ( ZContext, Socket, service & kernel buffer-extensions, IO-thread-pool affinity-mapping .bind(), Poller, .register(), etc., many of which do happen invisibly "under the hood" ) and rather avoid using all these ZeroMQ messaging / signaling services as some one-time consumables / disposables.

Every setup/tear-down takes also some other resources' time, so rather instantiate such infrastructure as a (semi)-persistent layer, on which other members connect and disconnect from using the present, layer-persistent messaging-/signaling-services.

Anyway, enjoy this powerful framework for smart & fast distributed computing!

user3666197
  • 1
  • 6
  • 50
  • 92
  • Is it totally necessary to use sockets within runnable/ extending thread class in Java? – CuriQ Jun 17 '17 at 00:18