Skip to main content

how to get data back to the server

Please note these forums are being decommissioned and use the new and improved forums at
1 reply [Last post]
Joined: 2012-10-12

Hi! I'm working with Disruptor and ZMQ Server-Client, my classes are in the same package:

App class it has two threads one for the ZMQ Server and the other for the ZMQ Client.

public class Client extends Thread {

public void run() {
// Prepare our context and socket
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.REQ);
socket.connect ("tcp://localhost:5555");
ZMQ.Socket synclient = context.socket(ZMQ.REQ);

//send a synchronization request

//wait for synchronization reply

// Do 10 requests, waiting each time for a response
for(int request_nbr = 0; request_nbr < 100; request_nbr++) {

String requestString = " Hello World ";
byte[] request = requestString.getBytes();
request[request.length-1]=0; //Sets the last byte to 0

socket.send(request, 0);
System.out.println("prova 1");

byte[] reply = socket.recv(0);
final String value = new String(reply);
System.out.println("resposta " +value);


public class Server extends Thread {

ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket socket = context.socket(ZMQ.REP);
protected int CLIENTS_EXPECTED = 1;
private Consumer consumer;

public void run() {
ZMQ.Socket socketsyn = context.socket(ZMQ.REP);

int subscribers =0 ;
while(subscribers< CLIENTS_EXPECTED){
//wait for synchronization request

//send synchronization reply
socketsyn.send("".getBytes(), 0);

byte[] request;
//Wait for next request from client
//We will wait for a 0-terminated string (C string) from the client
request = socket.recv (0);
final String value = new String(request);

Producer producer = new Producer();

The class Producer is storing the request from the Client in a RingBuffer

public class Producer {
private static final int size = 128;
private final ExecutorService executorService;
private final Disruptor disruptor1;
private final RingBuffer ringBuffer1;

executorService = Executors.newCachedThreadPool(); // will use to execute the consumer threads
disruptor1 = new Disruptor(SimpleEvent.FACTORY,size, executorService);
disruptor1.handleEventsWith(new SimpleEventHandler());
ringBuffer1 = disruptor1.getRingBuffer();
public void requestToRingBuffer(String request) { //our producer method

final long sequence =; //claim a slot
SimpleEvent simpleEvent = ringBuffer1.get(sequence);

simpleEvent.time = System.currentTimeMillis();
simpleEvent.text = request;
simpleEvent.level = 0;

ringBuffer1.publish(sequence); //then copy our value into that slot’s entry and finally publish the slot
long cursor = ringBuffer1.getCursor(); //Get the current sequence that is published to the RingBuffer
System.out.println("cursor: " +cursor);

public void stop(){ //takes care of waiting until all consumers have processed all available entries


Then SimpleEventHandler sends the event to another class which only reverse the request from the Client and stores it in another ring buffer and the consumer will read from the second ring buffer
public class Consumer {

String reply;

public void main(String [] args){
public void setReply(String reply){


Until here everything is working!
My problem is how I can send the reply back to the Server? without create a new Server in the Consumer class neither make the Server static,
is there any option with the Disruptor that I'm skipping to do this?Thanks


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2012-10-12

I solved it adding this to server class:

private static Server _instance;
private Server() {}
public static Server getInstance() {
if(_instance == null) {
_instance = new Server();
return _instance;

It doesn't allow to create a new server form any other class except from itself, so now I can send the data to the server from the consumer doing this:

Sever.getInstance.getReply(reply); // when you call getInstance you will get the only one server in your project.