Client Socket connects to multiple sockets and balances load towards a free socket

I am being asked to design the following programming problem.

Previously when a client sent a request to my application I was required to route it towards a specific port on another server. The server would response in a timely manner and a response would be sent from their server to my server and I would send the information to my client. This worked fine until the load increased and we realized that the other server did not handle multi threaded calls.

I have been given a limited set of ports by the other app so that whenever a load of client requests enters my application I must balance the load among those ports in such a manner that if port A is not free I send the client request to port B and if B is not free it goes to port C. [I know that this is not a correct solution, management wants it to work that way]

All calls handled are synchronous and all ports (requirement from application X) must always be kept open.

My biggest problems at the moment are:

  1. Knowing when a port is free. (not waiting for a response)
  2. How to push the load to the other ports.

I need some pointers to where to head to. Could you please help?

What I have done so far is load multiple sockets into an array of SocketAddress.

     Selector socketSelector = SelectorProvider.provider().openSelector();         int interestSet = SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE | SelectionKey.OP_READ;         List<SocketAddress> socketAddresses = FactorySockectConnection.getSocketInstances();          for (SocketAddress socketAddress : socketAddresses) {             SocketChannel socket = SocketChannel.open(socketAddress);             socket.configureBlocking(false);             socket.register(socketSelector, interestSet);         }         System.out.println("Start");          while (true) {              try {                 socketSelector.select();                  Set<SelectionKey> keys = socketSelector.selectedKeys();                 Iterator<SelectionKey> keyIterator = keys.iterator();                 while (keyIterator.hasNext()) {                      SelectionKey selectedKey = keyIterator.next();                      if (selectedKey.isConnectable()) {                         SocketChannel connectChannel = (SocketChannel) selectedKey.channel();                         connectChannel.finishConnect();                     }                     if (selectedKey.isWritable() && requestMessageByte != null) {                         SocketChannel writeChannel = (SocketChannel) selectedKey.channel();                         ByteBuffer buf = ByteBuffer.wrap(requestMessageByte.getBytes());                         while (buf.hasRemaining()) {                             writeChannel.write(buf);                         }                         requestMessageByte = null;                     }                      if (selectedKey.isReadable() && responseMessage == null) {                         SocketChannel readChannel = (SocketChannel) selectedKey.channel();                          ByteBuffer readBuffer = ByteBuffer.allocate(1024);                         readChannel.read(readBuffer);                         responseMessage = new String(readBuffer.array());                     }                     keyIterator.remove();                 } 

Replay

Category: java Time: 2016-07-28 Views: 0
Tags: java sockets

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.139 (s). 12 q(s)