Anna
Anna

Reputation: 83

Multithreaded pooled server listening on multiple ports in Java -

I am trying to figure out how to create a Java multithreaded server that can listen on multiple ports and have threadpools for each of the ports it accepts requests from.

I have already implemented a fucntioning multithread pooled server listening on a singl eport, as seen here:

public void run() {
        synchronized (this) {
            this.runningThread = Thread.currentThread();
        }
        openSocketServer();
        while (!isStopped()) {
            Socket clientSocket = null;
            try {
                clientSocket = this.serverSocket.accept();
            } catch (IOException e) {
                if (isStopped()) {
                    System.out.println("Server Stopped.");
                    break;
                }
                throw new RuntimeException("Error with accepting client connection", e);
            }
            this.threadPool.submit(new HandlerRichieste(clientSocket, this));
        }
        this.threadPool.shutdown();
        System.out.println("Server stopped");
    }

And I managed to implement a server listening on multiple ports with the NIO library; the problem with this implementation is that it only uses a single thread to handle the requests coming from the different ports, therefore handling them sequentially and killing performance:

Selector selector = Selector.open();

int[] ports = {4000,4001,6000};

for (int port : ports) {
   ServerSocketChannel server = ServerSocketChannel.open();
   server.configureBlocking(false);

   server.socket().bind(new InetSocketAddress(port));
   // from here we are only interested when accept evens occur on this socket
   server.register(selector, SelectionKey.OP_ACCEPT); 
}

while (selector.isOpen()) {
   selector.select();
   Set readyKeys = selector.selectedKeys();
   Iterator iterator = readyKeys.iterator();
   while (iterator.hasNext()) {
      SelectionKey key = (SelectionKey) iterator.next();
      if (key.isAcceptable()) {
         SocketChannel client = server.accept(); //SERVER CANNOT BE RESOLVED!!!!
         Socket socket = client.socket();
         // create new thread to deal with the connection coming from that port (closing both socket and client when done)
      }
   }
}

How can I merge them - resulting in a server listening on multiple ports that has a threadpool for every port? Is it even possible to make a multithread pooled server that is listening to multuple ports without using the NIO library? If so, can anybody show me the mechanism for having a threadpool for each port without Java NIO?

Upvotes: 1

Views: 755

Answers (1)

queeg
queeg

Reputation: 9372

You are already able to create a server with one thread waiting for connections on one port and serving incoming connections with threads from one specific pool.

What stops you from creating this pattern three times? Then you are listening on three ports and serving incoming connections with threads from three specific thread pools. The only thing you need to watch out: Waiting for a connection on one socket may block the thread.

As a solution just create three threads that are listening for one port each.

Upvotes: 1

Related Questions