3
\$\begingroup\$

I am looking for an optimal producer-consumer implementation in Java with the following aspects:

  1. Producer reads from server1 socket, and writes to a shared buffer, as well as another server 2 socket:

    Server1 -> Server2 + shared buffer

  2. Consumer reads from a shared buffer, and writes to server3 socket:

    Shared Buffer -> Server3

  3. Writes to shared buffer from Server 1 should be non-blocking (i.e. if the buffer is full it should not block the communication with Server 2).

I have considered the following options:

  1. Java NIO Pipe
  2. ConcurrentLinkedQueue<Byte> queue
  3. PipedInputStream and PipedOutputStream
  4. I assume buffer can have a pre-allocated memory to avoid dynamic memory allocation overhead? Or should that not be a concern? If so what would be a fast implementation.

What would be the fastest?

I have already tried out PipedInputStream and PipedOutputStream. My overhead compared to just forwarding data from server1->server2 was more than 12% this seemed excessive, which is why I came up with more options.

The specific part of the code already implemented, which is in the critical region (pertains to performance) is as follows in CommunicateToServerAndPipe:

 public void run() {
        try {
            String line = null;

            final byte[] request = new byte[1024];
            int bytesRead;

            //read from server 1 socket
            while (isRunning && (bytesRead = in.read(request)) != -1) {
                //write to server 2 socket
                out.write(request, 0, bytesRead);
                out.flush();
                //write to shared buffer pipe
                this.register.pos.write(request, 0, bytesRead);
            }
     register.kill();

    } catch (IOException ex) {
        if(ex instanceof SocketTimeoutException) {
            // The socket simply timed out. Kill, then exit.
            register.kill();
            return;
        }
        this.kill();
    }
}

I have made my initial code available here.

\$\endgroup\$
  • 1
    \$\begingroup\$ You seem to have quite specific communication needs. You may be interested in investigating more "full-blown" solutions like message-queue services. While those may provide less speed, they guarantee that no data is lost and are usually extensively tested \$\endgroup\$ – Vogel612 Mar 6 '17 at 11:01
  • \$\begingroup\$ @Vogel612 - could you give examples? Also I am not interested in blocking systems for data transfer from Server 1 ->shared buffer (should be non-blocking) to ensure speed for Server 1 -> Server 2 \$\endgroup\$ – tsar2512 Mar 7 '17 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.