Ola Marcio, bom dia.
O medo é que desta forma eu fique com um erro ininterrupto.
Pensei em algo do tipo:
while(running){
try{
this.selector.select();
Set<SelectionKey> selectedKeys = this.selector.selectedKeys();
Iterator<SelectionKey> it = selectedKeys.iterator();
while(it.hasNext()){
SelectionKey key = it.next();
it.remove();
if (!key.isValid()) {
continue;
}
if (key.isConnectable()) {
//FINISH CONNECTION IN CASE OF AN ERROR
SocketChannel keyChannel = (SocketChannel) key.channel();
if (keyChannel.isConnectionPending()) {
keyChannel.finishConnect();
}
}
if(key.isAcceptable()){
accept(key);
} else if(key.isReadable()){
read(key);
} else if (key.isWritable()) {
write(key);
}
}
} catch(Exception a){
running = false;
try {
channelServer.close();
channelServer = null;
} catch (IOException ex) {
Util.logError("ERRO RUN - CHANNELSERVER: " + ex.getMessage() + " " + ex.toString());
}
try {
selector.close();
selector = null;
} catch (IOException ex) {
Util.logError("ERRO RUN - SELECTOR: " + ex.getMessage() + " " + ex.toString());
}
service = null;
try {
startService(); //AQUI EU MANDO REINICIAR TUDO, TIPO COMEÇAR DO ZERO
} catch (IOException ex) {
Util.logError("ERRO RUN - STARTSERVICE: " + ex.getMessage() + " " + ex.toString());
}
Util.logError("ERRO RUN - " + a.getMessage() + " " + a.toString());
}//end try
}
O meu maior problema é que não consigo gerar um erro em laboratorio para ver como vai reagir.
Desde já agradeço a ajuda.
Luciano
De: java-br@yahoogrupos.com.br [mailto:java-br@yahoogrupos.com.br] Em nome de Marcio Ghiraldelli
Enviada em: segunda-feira, 17 de fevereiro de 2014 09:25
Para: java-br@yahoogrupos.com.br
Assunto: [Bulk] Re: [java-br] Tratamento de erros threads
É justamente o try dentro do while que fará o próximo laço ser executado quando acontecer um erro.
Em 15 de fevereiro de 2014 12:42, LvcTecno <lvctecno@yahoo.com.br> escreveu:
Boa tarde pessoal, estou migrando do vb para o Java.
Estou com dúvidas quanto ao tratamento de erro de uma thread sendo executada.
Preciso que mesmo que seja gerado um erro a thread continue sendo executada, tipo um on error resume next do vb6
Outra duvida: melhor o while(true) dentro do try ou fora?
Algum colega poderia me ajudar com isso?
public void run() {
try{
while(true){
this.selector.select();
Set<SelectionKey> selectedKeys = this.selector.selectedKeys();
Iterator<SelectionKey> it = selectedKeys.iterator();
while(it.hasNext()){
SelectionKey key = it.next();
it.remove();
if (!key.isValid()) {
continue;
}
if (key.isConnectable()) {
SocketChannel keyChannel = (SocketChannel) key.channel();
if (keyChannel.isConnectionPending()) {
keyChannel.finishConnect();
}
}
if(key.isAcceptable()){
accept(key);
} else if(key.isReadable()){
read(key);
} else if (key.isWritable()) {
write(key);
}
}
}
} catch(Exception a){
Util.logError("ERRO RUN - " + a.getMessage() + " " + a.toString());
}
}
Luciano
Responder através da web | através de email | Adicionar um novo tópico | Mensagens neste tópico (4) |
Para upload/download de arquivos: http://www.yahoogroups.com/files/java-br
0 comentários:
Postar um comentário