2015-02-26 3 views
2

Здесь мне удалось установить соединение между одним сервером и единственным клиентом, но теперь мое новое препятствие - это то, как достичь pool соединений TCP на стороне клиента в JAVA. Я прошел через многие сайты, но в конце не найдено ни одного плодотворного решения. Здесь class, который я использовал для получения connection к serverКак реализовать пул соединений TCP в java?

public class TCPIPCommunicator { 

private final String MODULE="TCPIPCommunicator :"; 

//private DeviceEntity deviceEntity; 
private static InetAddress inetServer = null; 
private static int devicePort = -1; 
private Socket clientSocket; 
private BufferedOutputStream outputStream; 
private BufferedInputStream inputStream; 
private static boolean isLinkActive = false; 
private String IP=""; 

public TCPIPCommunicator(String IP, int devicePort) { 
    this.IP=IP; 
    this.devicePort=devicePort; 
    initialize(); 
} 

public static boolean isLinkActive(){ 
    return isLinkActive; 
} 

public static void setLinkStatus(boolean status){ 
    isLinkActive = status; 
} 

private void initialize() { 
    System.out.println(MODULE+ "Inside initialize()"); 
    setLinkStatus(false); 
    try{ 
     inetServer = InetAddress.getByName(IP); 
    }catch (UnknownHostException uhe) { 
     System.out.println(MODULE+ "Error while creating inetaddress, Reason:"+uhe.getMessage()); 
    } 
    System.out.println(MODULE+ "Connecting to - " +inetServer.getHostAddress() + ":" + devicePort); 
    establishConnection(); 

     if(!isLinkActive()){ 
      //sendNotification(TCPIPConstants.LINK_DOWN); 
      releaseResources(); 
      //resumeLinkSanityHelper(); 
     } 
    System.out.println(MODULE+ "out of initialize(), isLinkActive :" + isLinkActive()); 
} 

public boolean establishConnection(){ 
    boolean isConnected = false; 
    //Get the Connection to PMS Server. 
    if(inetServer != null && devicePort > 0){ 
     try{ 
      clientSocket = new Socket(inetServer, devicePort); 
      clientSocket.setKeepAlive(true); 
     }catch(ConnectException ce){ 
      ce.printStackTrace(); 
      setLinkStatus(false); 
      System.out.println(MODULE+ "Exception in initialize() " + "Couldnot Connect Server. Reason:"+ce.getMessage()); 
     }catch(Exception e){ 
      e.printStackTrace(); 
      setLinkStatus(false);   
      System.out.println(MODULE+ "Exception in initialize() " + "while creating socket ,Reason: " + e.getMessage()); 
     } 

     //If got connection, Get the streams. 
     if(clientSocket != null && !clientSocket.isClosed()){ 
      System.out.println(MODULE+ "in initialize(), Got Socket Connection."); 
      try{ 
       outputStream = new BufferedOutputStream(clientSocket.getOutputStream()); 
      }catch(Exception e){ 
       e.printStackTrace(); 
       outputStream=null; 
       setLinkStatus(false); 
       System.out.println(MODULE+ "Exception in initialize() while getting socket outputStream : " + e.getMessage()); 
      } 

      if(outputStream != null){ 
       try{ 
        inputStream = new BufferedInputStream(clientSocket.getInputStream()); 
       }catch(Exception e){ 
        setLinkStatus(false); 
        System.out.println(MODULE+ "Exception in initialize() " + "while getting socket inputStream : " + e.getMessage()); 
       } 
       setLinkStatus(true); 
      } 
      if(outputStream != null && inputStream != null){ 
       isConnected = true; 
      } 
     }else{ 
      System.out.println(MODULE+ "in initialize(), Connection is closed or null."); 
      setLinkStatus(false); 
     } 
    } 
    return isConnected; 
} 



public int writeData(byte[] msg){ 
    int retValue = -1; 
    try{ 
     if(isLinkActive() && (outputStream !=null)){ 
      System.out.println(MODULE+ "Writting data ::::" + new String(msg)+ "::::"); 
      outputStream.write(msg);// ed 
      outputStream.flush(); 
      retValue = 1; 
     }else{ 
      System.out.println(MODULE+ " in writeData() link is down so status:" + retValue); 
     } 
    }catch(Exception e){ 
     e.printStackTrace(); 
     retValue = -1; 
     System.out.println(MODULE+ "Exception in write() < message to be sent was = " + new String(msg) + " > : " + e.getMessage()); 
    } 
    if(retValue == -1 && isLinkActive()){ 
     setLinkStatus(false); 
     //sendNotification(TCPIPConstants.LINK_DOWN); 
     //releaseResources(); 
     //resumeLinkSanityHelper(); 
    } 
    System.out.println(MODULE+ " in writeData() Write status for ::"+ new String(msg) + ":: -->" +retValue); 
    return retValue; 
} 

public String readData() { 
    System.out.println(MODULE+"\tInside readDAta"); 
    String response = null; 
    int bytesReceived=-1; 
    byte[] readBuffer = new byte[1024]; 
    try{ 
     long timetoread = System.currentTimeMillis(); 
     if(inputStream == null || !(isLinkActive())){ 
      System.out.println(MODULE+"Inputstream is null or link is down, returning null"); 
      return null; 
     } 

     try{ 
      System.out.println("Waiting to read data"); 
      bytesReceived = inputStream.read(readBuffer); 
      System.out.println(MODULE+"# Byte Receieved #" + bytesReceived); 
     }catch(Exception e){ 
      e.printStackTrace(); 
      System.out.println(MODULE+"Error in readData() , Reason:"+e.getMessage()); 
      if(isLinkActive()){ 
       setLinkStatus(false); 
       //sendNotification(TCPIPConstants.LINK_DOWN); 
       releaseResources(); 
       //resumeLinkSanityHelper(); 
      } 
     } 
     if(bytesReceived > 0){   
      response = new String(readBuffer,0,bytesReceived); // ed 
      timetoread = System.currentTimeMillis() - timetoread; 
      System.out.println(MODULE + "Total Bytes Received: " + bytesReceived); 
      System.out.println(MODULE+ "Total Time Taken : " + timetoread); 
      System.out.println(MODULE+ "Length of data received : " + response.length()); 
      System.out.println(MODULE+ "Data Received : ####" + response + "####"); 
     }else{ 
      ////////// HERE MEANS TCP CONNECTION STATUS WILL CLOSE_WAIT 
      ////////// SO RELEASING CONNECTION. 
      System.out.println(MODULE+ " Releasing Resource. bytesReceived is <= 0 : Total Bytes Received :"+ bytesReceived); 
      if(isLinkActive()){ 
       setLinkStatus(false); 
       //sendNotification(TCPIPConstants.LINK_DOWN); 
       releaseResources(); 
       //resumeLinkSanityHelper(); 
      } 
      System.out.println(MODULE+ " Resource has been released."); 
     } 
    }catch(Exception e){ 
     e.printStackTrace(); 
     System.out.println(MODULE+ "In catch : Data Received : ####" + response + "####"); 
     System.out.println(MODULE+ "Exception in readdata() : " + e); 
    }finally{ 
     readBuffer=null; 
    } 
    return response; 
} 



public void releaseResources(){ 
    System.out.println(MODULE+ "Releasing Resources...."); 
    try{ 
     if(clientSocket !=null) 
      clientSocket.close(); 
    }catch(Exception e){ 
     System.out.println(MODULE+ "In releaseResources() :Error closing socket."+e.getMessage()); 
    } 
    try{ 
     if(inputStream !=null) 
      inputStream.close(); 

    }catch(Exception e){ 
     System.out.println(MODULE+ "In releaseResources() :Error closing inputStream."+e.getMessage());   
    } 

    try{ 
     if(outputStream != null){ 
      outputStream.close(); 
     } 
    }catch(Exception e){ 
     System.out.println(MODULE+ "in releaseResources() :Error closing outputStream."); 
    } 
    System.out.println(MODULE + "Resources Relased...");   
} 



} 

Здесь establishConnection() метод будет установить connection на сервер. Любая помощь могла бы помочь.

Примечание: Я думал об использовании ThreadPoolExecutor, но поскольку требование является синхронным, я отказался от этой идеи его использования.

+1

Чего вы хотите достичь? Почему, по-вашему, вам нужен пул соединений? –

+1

@ThomasStets предполагает, что 5 запросов поступают, и предопределенный пул является их для подключения tcp, чем идеальный объект соединения tcp должен использоваться для отправки запроса на сервер –

ответ

0

Вы можете создать переменную Set<Socket> sockets хранить все clientSocket вы created.Every подключения к разъему сервера, вы можете поместить сокет клиента в этом set.So этого набор работает как бассейн.

+0

, как это называется пулом соединений? –

+0

созданный вами клиентский ресурс жив, и каждый раз, когда вы хотите его использовать, вы можете получить его непосредственно из набора, что сокращает время настройки соединения. – luchy0120

+1

Как вы узнаете, какой объект подключения используется, и как вы будете поддерживать один и тот же объект в 'set'? пожалуйста, покажите код, если у вас есть идея! –

-1

Пример кода, как показано ниже, я использую карту, ключ - «host-port», а значение - LinkedList, содержащий сокеты, связанные с одним и тем же URL-адресом.

public class SocketPool{ 
    Map<String,LinkedList<Socket>> sockets; 

public boolean establishConnection(String inetServer , int devicePort){ 
    boolean result = false; 
    try { 
     Socket clientSocket = new Socket(inetServer, devicePort); 
     String socketKey = inetServer + "-" + devicePort; 
     if(clientSocket.isConnected()&&sockets.containsKey(socketKey)){ 
      sockets.get(socketKey).add(clientSocket); 
     }else if(clientSocket.isConnected()&&!sockets.containsKey(socketKey)){ 
      LinkedList<Socket> socketSet = new LinkedList<Socket>(); 
      socketSet.add(clientSocket); 
      sockets.put(socketKey, socketSet); 
     } 
     result = true; 
    } catch (UnknownHostException e) { 
     result = false; 
    } catch (IOException e) { 
     result = false; 
    } 
    return result; 
} 

public Socket fetchSocket(String inetServer , int devicePort){ 
    String socketKey = inetServer + "-" + devicePort; 
    Socket clientSocket =null; 
    if(sockets.containsKey(socketKey)&&!sockets.get(socketKey).isEmpty()){ 
     clientSocket = sockets.get(socketKey).removeLast(); 
     int size = sockets.get(socketKey).size(); 
     if(size==0){ 
      sockets.remove(socketKey); 
     } 
    } 
     return clientSocket; 
} 
//other functions 
} 
+0

Не так много смысла в тестировании 'isConnected()' здесь. Это не волшебство становится ложным, вопреки довольно широко распространенному городскому мифу. – EJP

+0

то, как вы думаете, java api нужен этот метод? Если сервер не работает, вы не проверяете статус соединения? Если вы не думаете, что это правильный путь, то покажите мне свой код. Метод getInputStream в Socket сначала проверяет соединение. Не могли бы вы рассказать мне, почему? public InputStream getInputStream() throws IOException { if (isClosed()) throw new SocketException («Socket is closed»); if (! IsConnected()) throw new SocketException («Сокет не подключен»); – luchy0120

Смежные вопросы