如何在Http消息中处理服务器错误?
假设我已经发送了消息的标题,并且我正在流式传输消息正文,当遇到意外错误时该怎么办.
我也假设这个错误是在生成内容而不是连接错误时引起的.
(很棒)简化代码:
// I can define any transfer encoding or header fields i need to.
send(header); // Sends the header to the Http client.
// Using an iterable instead of stream for code simplicity's sake.
Iterable<String> stream = getBodyStream();
Iterator<String> iterator = stream.iterator();
while (iterator.hasNext()) {
String string;
try {
string = iterator.next();
catch (Throwable error) { // Oops! an error generating the content.
// What do i do here? (In regards to the Http protocol)
}
send(string); …Run Code Online (Sandbox Code Playgroud) 假设我有以下代码:
public void process() {
byte[] data = new byte[size];
... // code that uses the above data
longProcess(); // a very long running process that does not use the data.
}
Run Code Online (Sandbox Code Playgroud)
假设在程序中的数据不被引用其他地方,是JVM足够聪明,使数据成为而漫长的过程仍在运行垃圾收集?
如果没有,将添加
data = null;
Run Code Online (Sandbox Code Playgroud)
在漫长的过程之前允许这种情况发生?