Boa noite,
Como eu consigo identificar o que está fazendo meu programa chegar a 98% de uso da CPU? No meu computador o programa usa menos de 3% sempre. Quando inicio ele no servidor, em 15 min ele começa a usar 98%.
Esse ThreadPool faz o que? Corre o risco dele está criando “infinitas” threads?
Parece que há algum erro no recebimento dos bytes.
O programa fica em loop, trazendo muitas mensagens vazias, sem bytes, travando o processador
while (true) {
selector.select();
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> it = selectedKeys.iterator();
while (it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();
if ((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) {
// Aceitar nova conexão
ServerSocketChannel sscNew = (ServerSocketChannel) key.channel();
SocketChannel sc = sscNew.accept();
sc.configureBlocking(false);
// Adicionar nova conexão no seletor
sc.register(selector, SelectionKey.OP_READ);
it.remove();
} else if ((key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) {
try {
// Leitura dos dados
SocketChannel conexao = (SocketChannel) key.channel();
// Captura todos os bytes
conexao.read(echoBuffer);
byte byteArray[] = new byte[echoBuffer.position()];
echoBuffer.flip();
echoBuffer.get(byteArray);
echoBuffer.clear();
if (byteArray.length > 0) {
//byteArray tem os bytes
}
}
it.remove();
} catch (Exception e) {
// Cancelar chave
key.cancel();
LOGGER.error(ExceptionUtils.getStackTrace(e));
}
}
}
}
Você não deveria encerrar a conexão em algum momento?
Fechar a conexão resolveu esse problema, mas agora eu não consigo enviar mensagens, sempre sempre sobe java.nio.channels.ClosedChannelException, há alguma forma de não fechar a conexão e parar de receber bytes vazios?
Acho que resolvi o problema
while(echoBuffer.hasRemaining()){
echoBuffer.get(byteArray);
}
EDIT: Não adiantou, demorou mais para apresentar o problema =/