进程挂起我的应用程序

时间:2013-12-20 15:36:52

标签: java multithreading

我正在完成一项大学任务,我必须共享3台服务器,这些服务器处理客户端和服务器组的领导者,这里是场景:

  1. 领导者开始。
  2. leader运行3个服务器(必须使用Runtime.getRuntime()运行它们作为jar文件.exec();
  3. 向服务器发送激活消息以开始为客户提供服务
  4. 客户端开始与服务器通信。
  5. 问题是在服务器挂起的客户端发出3或4个请求之后,只有在领导者终止时才完成其工作。 如果我从cmd手动运行jar文件一切正常,只有当领导者通过进程处理jar文件时问题才会出现。

    这是一些代码 - 服务器主节点,它接受任何新的连接,来自组的领导者或要服务的客户端:

    public void startServer () throws IOException{
            //new LogSetup ( "logs/server/server.log" , Level.ALL );
            this.serverStatus = ServerStatuses.UNDER_INITIALIZATION;
            running = initializeServer ();
            if ( serverSocket != null ) {
                while ( isRunning () ) {
                    try {
                        Socket client = serverSocket.accept ();
                        ConnectionThread connection = new ConnectionThread (
                                client , this );
                        new Thread ( connection ).start ();
    
                        logger.info ( "new Connection: Connected to "
                                + client.getInetAddress ().getHostName ()
                                + " on port " + client.getPort () );
                    } catch ( IOException e ) {
                        logger.error ( "Error! "
                                + "Unable to establish connection. \n" , e );
                    }
                }
            }
            logger.info ( "Server stopped." );
        }
    

    新连接的线程:

    public void run () {
            try {
                output = clientSocket.getOutputStream ();
                input = clientSocket.getInputStream ();
    
                while ( isOpen ) {
                    try {
                        AbstractMessage msg = receiveMessage ();
                        handleRequest ( msg ); // to determine the connection type
                        /*
                         * connection either terminated by the client or lost due to
                         * network problems
                         */
                    } catch ( IOException ioe ) {
                        logger.error ( "Error! Connection lost!" +ioe.getStackTrace ());
                        isOpen = false;
                    }
                }
    
            } catch ( IOException ioe ) {
                logger.error ( "Error! Connection could not be established!" , ioe );
    
            } finally {
    
                try {
                    if ( clientSocket != null ) {
                        input.close ();
                        output.close ();
                        clientSocket.close ();
                    }
                } catch ( IOException ioe ) {
                    logger.error ( "Error! Unable to tear down connection!" , ioe );
                }
            }
            logger.info ( "exit the thread" );
        }
    private void handleRequest ( AbstractMessage msg ) throws IOException {
            logger.info ( "inside handling abstract message" );
            if ( msg.getMessageType ().equals ( MessageType.CLIENT_MESSAGE ) ) {
                handleClientRequest ( ( ClientMessage ) msg );
            } else if ( msg.getMessageType ().equals ( MessageType.SERVER_MESSAGE ) ) {
                handleServerRequest ( ( ServerMessage ) msg );
            } else if ( msg.getMessageType ().equals ( MessageType.ECS_MESSAGE ) ) {
                handleECSRequest ( ( ECSMessage ) msg );
            }
            logger.info ( "finish handling " );
    
        }
    

1 个答案:

答案 0 :(得分:1)

Leader流程必须处理来自服务器的输入/输出/错误。当服务器记录某些内容时,会将其写入System.out(这是大多数日志框架的默认行为)。由于您的领导者将服务器进程启动,因此输出将写入管道。如果管道缓冲区已满,则日志块和服务器“挂起”。