1.  
有一个模块,有如下需求:a) 和外部系统通过socket连接,外部系统为server,模块为client。b) 模块与外部系统通过socket进行"模块->外部系统"的单向命令发送,命令模式为"请求->响应",命令是同步的,在一个socket中,前一个命令没有执行完前,不允许发送第二个命令。 c) 模块可以和一个外部系统建立多个连接,一个外部系统的多个连接之间采用负载平衡。 d) 模块可以同时连接两个外部系统,两个外部系统之间采用主备方式。 e) 模块需要增加统计功能,要能统计与一个外部系统的所有连接的消息总数。 i. 模块需要增加监控功能,要能在线跟踪与所有外部系统发送的请求和接收的响应。 要求: 1) 请设计此模块,写出出主要的类和接口。类不要求细化到方法级,但要能说明接口和类之间的关系和每个类/接口的主要职能。 2) 从模块调用者角度,写出如何实例化1中相关的类,如何调用。 3) 要求设计时要考虑系统的灵活性、可扩展性和可维护性。鼓励采用适当的设计模式,并请说明你使用的设计模式以及简单说明使用这些模式的目的。

解决方案 »

  1.   

    Vector<ServerThread_2> m_threads;
    Socket m_socket = null;
    DataInputStream m_in = null;
    DataOutputStream m_out = null;
    InputStream m_in_in = null;
    String m_nid; /**
     * 
     */
    public ServerThread_2(Socket s, Vector<ServerThread_2> threads) {
    // TODO Auto-generated constructor stub
    m_socket = s;
    m_threads = threads;
    try {
    m_in = new DataInputStream(m_socket.getInputStream());
    // m_out = new DataOutputStream(m_socket.getOutputStream());
    m_in_in = m_socket.getInputStream();
    } catch (Exception e) { }
    } public void run() {
    BufferedReader br = null;
    InputStreamReader fr = null;
    try {
    // while(true){
    // while(m_in!=null&&m_in.read()!=-1){
    fr = new InputStreamReader(m_in);
    br = new BufferedReader(fr);
    String temp1 = null;
    StringBuffer sb1 = new StringBuffer();
    // if(br.readLine().contains("017200037")){
    // System.out.println("br.readLine()====="+br.readLine());
    // }
    while ((temp1 = br.readLine()) != null) { sb1.append(temp1);
    System.out.println("sb1====" + temp1);
    if (temp1.toString().equals("bye") || temp1.equals("ye")
    || temp1 == null) {
    // System.out.println("out from socket"+temp1);
    return;
    }
    // if(!temp1.contains("-")){
    insertIntoDatabase(temp1.split("&"));
    // }
    }
    // System.out.println("all =================="+i);
    // System.out.println("sb1===="+sb1.toString());
    // }
    } // }
    catch (Exception e) {
    e.printStackTrace();
    } finally { try {
    if (br != null) {
    br.close();
    }
    if (fr != null) {
    fr.close();
    }
    m_threads.removeElement(this);
    System.out.println(" 总线程数 = " + m_threads.size());
    m_socket.close();
    } catch (Exception e) { }
    }
    }