Browse code

BM-14897 Fix: async flush

Thomas Fricker authored on 06/06/2019 15:08:02
Showing 1 changed files
... ...
@@ -52,41 +52,34 @@ public class WriteFile implements Handler<HttpServerRequest> {
52 52
 	private void writeFile(final FileSystem fs, final HttpServerRequest req, final String path) {
53 53
 		// we don't really care about truncate result.
54 54
 		new File(path).getParentFile().mkdirs();
55
-		fs.open(path, new Handler<AsyncResult<AsyncFile>>() {
56
-
57
-			@Override
58
-			public void handle(AsyncResult<AsyncFile> ar) {
59
-				if (!ar.succeeded()) {
60
-					logger.error("[" + path + "]: " + ar.cause().getMessage(), ar.cause());
61
-					req.resume();
62
-					req.response().setStatusCode(500).end();
63
-				} else {
64
-					final AsyncFile af = ar.result();
65
-					final Pump pump = Pump.createPump(req, af);
66
-					req.endHandler(new Handler<Void>() {
67
-
68
-						@Override
69
-						public void handle(Void v) {
70
-							af.flush();
71
-							af.close(new Handler<AsyncResult<Void>>() {
72
-
73
-								@Override
74
-								public void handle(AsyncResult<Void> event) {
75
-									if (!event.succeeded()) {
76
-										logger.error("Fail to write file {}", path);
77
-										req.response().setStatusCode(500).end();
78
-									} else {
79
-										logger.info("PUT {} completed, wrote {}bytes.", path, pump.bytesPumped());
80
-										req.response().end();
81
-									}
55
+		fs.open(path, ar -> {
56
+			if (!ar.succeeded()) {
57
+				logger.error("[" + path + "]: " + ar.cause().getMessage(), ar.cause());
58
+				req.resume();
59
+				req.response().setStatusCode(500).end();
60
+			} else {
61
+				final AsyncFile af = ar.result();
62
+				final Pump pump = Pump.createPump(req, af);
63
+				req.endHandler(v -> {
64
+					af.flush(fh -> {
65
+						if (!fh.succeeded()) {
66
+							logger.error("Failed to write file {}", path);
67
+							req.response().setStatusCode(500).end();
68
+						} else {
69
+							af.close(event -> {
70
+								if (!event.succeeded()) {
71
+									logger.error("Failed to write file {}", path);
72
+									req.response().setStatusCode(500).end();
73
+								} else {
74
+									logger.info("PUT {} completed, wrote {}bytes.", path, pump.bytesPumped());
75
+									req.response().end();
82 76
 								}
83 77
 							});
84 78
 						}
85 79
 					});
86
-					req.resume();
87
-					pump.start();
88
-				}
89
-
80
+				});
81
+				req.resume();
82
+				pump.start();
90 83
 			}
91 84
 		});
92 85
 	}