Skip to content

Commit

Permalink
Trigger onClose handler when underlying http channel is closed [IMMUT…
Browse files Browse the repository at this point in the history
…ANT-573]
  • Loading branch information
tobias committed Jul 29, 2015
1 parent 5228f64 commit 433a0a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Expand Up @@ -17,6 +17,7 @@
package org.projectodd.wunderboss.web.undertow.async;

import io.undertow.server.HttpServerExchange;
import io.undertow.server.ServerConnection;
import org.projectodd.wunderboss.web.async.OutputStreamHttpChannel;

import java.io.OutputStream;
Expand All @@ -28,6 +29,12 @@ public UndertowHttpChannel(final HttpServerExchange exchange,
final OnClose onClose) {
super(onOpen, onError, onClose);
this.exchange = exchange.setPersistent(true).dispatch();
this.exchange.getConnection().addCloseListener(new ServerConnection.CloseListener() {
@Override
public void closed(ServerConnection _) {
notifyClose();
}
});
}

@Override
Expand Down
Expand Up @@ -67,6 +67,14 @@ public void notifyError(Throwable error) {
}
}

protected void notifyClose() {
if (!closeNotified &&
this.onClose != null) {
this.onClose.handle(this, null, null);
}
closeNotified = true;
}

@Override
public boolean isOpen() {
return this.open;
Expand Down Expand Up @@ -189,9 +197,7 @@ public void close() throws IOException {
this.stream.close();
this.open = false;

if (this.onClose != null) {
this.onClose.handle(this, null, null);
}
notifyClose();
}

protected Runnable closer = new Runnable() {
Expand All @@ -209,6 +215,7 @@ public void run() {
private boolean open = false;
private boolean sendQueued = false;
private boolean headersSent = false;
private boolean closeNotified = false;
private OutputStream stream;
private final OnOpen onOpen;
private final OnError onError;
Expand Down

0 comments on commit 433a0a9

Please sign in to comment.