更改 netty 上的 httpresponse 状态
Changing httpresponse status on netty
api 正在传输大量数据。在执行验证并打开与后端的连接然后创建 jdbc 语句之后,我们 return httpresponse ok 状态为 header。我们看到的问题是,当流中断时,客户端不会收到错误代码,我们唯一能做的就是关闭频道。
这是我们在开始时发送回状态的方式;
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.headers().set(CONTENT_TYPE, MimeTypes.TEXT_JSON_UTF_8);
response.setChunked(true);
response.headers().set(Names.TRANSFER_ENCODING, Values.CHUNKED);
Channel ch = ctx.getChannel();
// Write the initial line and the header.
ch.write(response);
当流式传输过程中出现任何故障时,错误会被 catch 块捕获;
} catch (Exception e) {
ctx.getChannel().close();
String msg = "Error while streaming dynamic content from backend datasource " + ((DatasetDynamic) datasets[0]).getDbDataSourceName();
error(e, msg);
debug("uriNodes=" + this.uriNodes + "; params=" + this.params);
throw new Exception(msg, e);
} finally {
正如您在 catch 块中看到的那样,为了通知客户端,出了点问题,它所做的只是;ctx.getChannel().close();
我们是否可以将正确的带有错误的 httpresponse 发送回客户端?
看来您可以随时通过频道发送 httpresponse,不必像 header;
} catch (Exception e) {
// Any exception here means an error in the middle of chunk streaming, we
// send back http 500 to the client to inform the failure
ResponseErrorStatus status = new ResponseErrorStatus(ServiceErrorStatus.INTERNAL_SERVER_ERROR,
" error: " + e.getMessage() + (e.getCause() != null ? (" - caused by: " + e.getCause().getMessage()) : ""));
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status.serviceErrorStatus.httpStatus);
Channel ch = ctx.getChannel();
ch.write(response);
api 正在传输大量数据。在执行验证并打开与后端的连接然后创建 jdbc 语句之后,我们 return httpresponse ok 状态为 header。我们看到的问题是,当流中断时,客户端不会收到错误代码,我们唯一能做的就是关闭频道。
这是我们在开始时发送回状态的方式;
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.headers().set(CONTENT_TYPE, MimeTypes.TEXT_JSON_UTF_8);
response.setChunked(true);
response.headers().set(Names.TRANSFER_ENCODING, Values.CHUNKED);
Channel ch = ctx.getChannel();
// Write the initial line and the header.
ch.write(response);
当流式传输过程中出现任何故障时,错误会被 catch 块捕获;
} catch (Exception e) {
ctx.getChannel().close();
String msg = "Error while streaming dynamic content from backend datasource " + ((DatasetDynamic) datasets[0]).getDbDataSourceName();
error(e, msg);
debug("uriNodes=" + this.uriNodes + "; params=" + this.params);
throw new Exception(msg, e);
} finally {
正如您在 catch 块中看到的那样,为了通知客户端,出了点问题,它所做的只是;ctx.getChannel().close();
我们是否可以将正确的带有错误的 httpresponse 发送回客户端?
看来您可以随时通过频道发送 httpresponse,不必像 header;
} catch (Exception e) {
// Any exception here means an error in the middle of chunk streaming, we
// send back http 500 to the client to inform the failure
ResponseErrorStatus status = new ResponseErrorStatus(ServiceErrorStatus.INTERNAL_SERVER_ERROR,
" error: " + e.getMessage() + (e.getCause() != null ? (" - caused by: " + e.getCause().getMessage()) : ""));
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status.serviceErrorStatus.httpStatus);
Channel ch = ctx.getChannel();
ch.write(response);