下面列出了io.netty.channel.ChannelHandlerContext#fireExceptionCaught ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
// uuid
byte[] uuidBytes = new byte[36];
in.readBytes(uuidBytes);
UUID id = UUID.fromString(new String(uuidBytes));
// op
byte[] opBytes = new byte[4];
in.readBytes(opBytes);
Message.Op op = Message.Op.fromBytes(opBytes);
// payloadSize
byte[] payloadSizeBytes = new byte[4];
in.readBytes(payloadSizeBytes);
int payloadSize = Ints.fromByteArray(payloadSizeBytes);
if (in.readableBytes() < payloadSize) {
ctx.fireExceptionCaught(new DecoderException("Not enough bytes available to decode payload"));
}
out.add(in.readRetainedSlice(payloadSize));
out.add(new Message(id, op));
}
private void onHttp2GoAwayFrame(ChannelHandlerContext ctx, final Http2GoAwayFrame goAwayFrame) {
try {
forEachActiveStream(new Http2FrameStreamVisitor() {
@Override
public boolean visit(Http2FrameStream stream) {
final int streamId = stream.id();
final DefaultHttp2StreamChannel childChannel = ((Http2MultiplexCodecStream) stream).channel;
if (streamId > goAwayFrame.lastStreamId() && connection().local().isValidStreamId(streamId)) {
childChannel.pipeline().fireUserEventTriggered(goAwayFrame.retainedDuplicate());
}
return true;
}
});
} catch (Http2Exception e) {
ctx.fireExceptionCaught(e);
ctx.close();
} finally {
// We need to ensure we release the goAwayFrame.
goAwayFrame.release();
}
}
@Override
@SuppressWarnings("unchecked")
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) {
Encoder<Message> encoder = (Encoder<Message>) ENCODERS.get(msg.getClass());
buffer.clear();
ByteBuffer msgbuf = buffer;
try {
while (true) {
try {
encoder.write(msg, msgbuf, encoding);
break;
} catch (BufferOverflowException overflow) {
// large clob/blob, resize buffer aggressively
msgbuf = ByteBuffer.allocate(msgbuf.capacity() * 4);
}
}
msgbuf.flip();
out.writeBytes(msgbuf);
} catch (Throwable t) {
// broad catch as otherwise the exception is silently dropped
ctx.fireExceptionCaught(t);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
recorder.incrementErrorsCount(ctx.channel().remoteAddress(),
path != null ? path : resolveUri(ctx));
ctx.fireExceptionCaught(cause);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (logger.isEnabled(internalLevel)) {
logger.log(internalLevel, format(ctx, "EXCEPTION", cause), cause);
}
ctx.fireExceptionCaught(cause);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
this.exception = cause;
if (!(cause instanceof ReadTimeoutException)) {
logger.error("Not a read timeout", cause);
}
ctx.fireExceptionCaught(cause);
ctx.close();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
try {
ctx.fireChannelActive();
} catch (Throwable t) {
ctx.fireExceptionCaught(t);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (cause instanceof SpdyProtocolException) {
issueSessionError(ctx, SpdySessionStatus.PROTOCOL_ERROR);
}
ctx.fireExceptionCaught(cause);
}
@Override
public final void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (finished) {
ctx.fireExceptionCaught(cause);
} else {
// Exception was raised before the connection attempt is finished.
setConnectFailure(cause);
}
}
private void handleSslState(ChannelHandlerContext ctx, SslState state) {
switch (state) {
case BRIDGING:
logger.debug("SSL event triggered, enable SSL handler to pipeline");
MySqlSslConfiguration ssl = this.ssl;
this.ssl = null;
if (ssl == null) {
ctx.fireExceptionCaught(new IllegalStateException("The SSL bridge has used, cannot build SSL handler twice"));
return;
}
SslProvider sslProvider = buildProvider(ssl, context.getServerVersion());
SslHandler sslHandler = sslProvider.getSslContext().newHandler(ctx.alloc());
this.sslEngine = sslHandler.engine();
ctx.pipeline().addBefore(NAME, SSL_NAME, sslHandler);
break;
case UNSUPPORTED:
// Remove self because it is useless. (kick down the ladder!)
logger.debug("Server unsupported SSL, remove SSL bridge in pipeline");
ctx.pipeline().remove(NAME);
break;
}
// Ignore another custom SSL states because they are useless.
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
final ChannelConfig config = ctx.channel().config();
if (config.isAutoRead()) {
// stop accept new connections for 1 second to allow the channel to recover
// See https://github.com/netty/netty/issues/1328
config.setAutoRead(false);
ctx.channel().eventLoop().schedule(enableAutoReadTask, 1, TimeUnit.SECONDS);
}
// still let the exceptionCaught event flow through the pipeline to give the user
// a chance to do something with it
ctx.fireExceptionCaught(cause);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof Message) {
Message message = (Message) msg;
if (currentPayload == null) {
log.error("No response payload received for request id '{}': {}", message.getId(), message);
ctx.fireExceptionCaught(
new RuntimeException(
"No response payload received for request id '" + message.getId() + "'"));
reset();
return;
}
if (error) {
log.error(
"Multiple response payloads received for request id '{}': {}",
message.getId(),
message);
ctx.fireExceptionCaught(
new RuntimeException(
"Multiple response payloads received for request id '" + message.getId() + "'"));
reset();
return;
}
Request request = getRequest(getMapping(ctx.channel()), message);
if (request == null) {
log.error("Unexpected response received for request id '{}': {}", message.getId(), message);
ctx.fireExceptionCaught(
new RuntimeException(
"Unexpected response received for request id '" + message.getId() + "'"));
reset();
return;
}
Response response = new Response(message.getId(), currentPayload);
request.getResponsePromise().set(response);
reset();
} else {
if (currentPayload != null) {
error = true;
return;
}
currentPayload = msg;
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cancel();
ctx.fireExceptionCaught(cause);
}
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.fireExceptionCaught(cause);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
LOGGER.error("Error: ", cause);
ctx.channel().close();
ctx.fireExceptionCaught(cause);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.fireExceptionCaught(cause);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
LOGGER.error("Error: ", cause);
ctx.channel().close();
ctx.fireExceptionCaught(cause);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.fireExceptionCaught(cause);
}
private void onRstStreamRead(Http2ResetFrame resetFrame, ChannelHandlerContext ctx) throws Http2Exception {
ctx.fireExceptionCaught(new Http2ResetException(resetFrame.errorCode()));
}
/**
* Invoked when an incoming request exceeds the maximum content length. The default behvaior is to trigger an
* {@code exceptionCaught()} event with a {@link TooLongFrameException}.当传入请求超过最大内容长度时调用。默认的behvaior用一个TooLongFrameException触发一个exceptionCaught()事件。
*
* @param ctx the {@link ChannelHandlerContext}
* @param oversized the accumulated message up to this point, whose type is {@code S} or {@code O}
*/
protected void handleOversizedMessage(ChannelHandlerContext ctx, S oversized) throws Exception {
ctx.fireExceptionCaught(
new TooLongFrameException("content length exceeded " + maxContentLength() + " bytes."));
}