1

Linux マシンの Tomcat で Java アプリケーションを実行しています。このアプリケーションは、約 15 台のデバイスが接続されているソケット サーバーのように機能します。デバイスが大きなメッセージを送信すると、CPU の使用率が 100% になるまで増加するようです。問題は、アプリケーションをアンデプロイしても、Java がまだ CPU の 99% を使用していることです。アプリケーションには次の 2 つの部分があります。

ソケット サーバー:

    public void iniciarSocket() {

    Runnable serverTask = new Runnable() {
        @Override
        public void run() {
            try {

                serverSocket = new ServerSocket(PORT);

                System.out.println("Waiting a connection");

                while (true) {
                    Socket socket = null;

                    try {
                        socket = serverSocket.accept();

                        System.out.println("Client connected");
                    } catch (Exception e) {
                        System.out.println("Error: " + e.getMessage());
                    }
                    new SocketThread(socket).start();
                }

            } catch (Exception e) {
                System.out.println("Error: " + e.getMessage());
            }
        }
    };
    Thread serverThread = new Thread(serverTask);
    serverThread.start();
}

各デバイスに接続する各ソケット スレッド:

    public void run() {
    try {

        //Output channel
        DataOutputStream salida;
        salida = new DataOutputStream(socket.getOutputStream());

        System.out.println("Client connected.... ");

        byte[] buff = new byte[1024];
        int bytesLeidos = 0;
        socket.setSoTimeout(300000);
        System.out.println("Timeout: " + socket.getSoTimeout());

        while((bytesLeidos = socket.getInputStream().read(buff, 0, buff.length)) > -1) {

            System.out.println("Bytes leidos: " + bytesLeidos);

            if ((bytesLeidos == 70) && (Protocolo.isStatusMessage(buff))) {
                Protocolo.decode(buff, salida);
            } else {

                int offset = 0;

                while (offset < bytesLeidos) {

                    while ((offset + 70 <= bytesLeidos) &&(!Protocolo.isStatusMessageWithOffset(buff, offset))) {
                        offset++;
                    }

                    if ((offset + 70 <= bytesLeidos) &&(Protocolo.isStatusMessageWithOffset(buff, offset))) {
                        Protocolo.decodeWithOffset(buff, offset, salida);
                        offset += 70;
                    }
                }
            }
        }
    } catch (Exception e) {
        System.out.println();
    } finally {
        System.out.println("Communication ended");
        try {
            socket.close();
        } catch (Exception e) {
            System.out.println("Socket not closed");
        }
    }
}

私は何が起こっているのか理解できず、多くのことを試していますが、問題を解決することはできません。

4

1 に答える 1

1

問題は解決したようです。EJP は正しく、メッセージが 70 の倍数でない場合、ループは終了しません。socketThread を変更するだけで済みました。

            while((bytesLeidos = socket.getInputStream().read(buff, 0, buff.length)) > -1) {

            System.out.println("Bytes leidos: " + bytesLeidos);

            if ((bytesLeidos == 70) && (Protocolo.isStatusMessage(buff))) {
                Protocolo.decode(buff, salida);
            } else {


                int offset = 0;

                // Code changed
                while (offset < bytesLeidos) {

                    if (Protocolo.isStatusMessageWithOffset(buff, offset)) {
                        // decodificar
                        Protocolo.decodeWithOffset(buff, offset, salida);
                        offset += 70;
                    } else {
                        offset++;
                    }
                }
                // End code changed
            }
        }

どうもありがとうございました。

于 2013-10-21T15:12:02.557 に答える