چند وقت پیش بعد از انتشار جاوا 8 بود که امید با لامبادا حال کرده بود و درموردش تویت می کرد. بگذریم.
vertx یک سکوی توسعه عالی هست که مزایای همچون چند زبانی و ... به ارمغان میاره که در موردش می توانید به سایتش مراجعه کنید.
یک مثال ساده راه گشا خواهد بود.
public class SimpleFormUploadServer extends Verticle {
public void start() {
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
public void handle(final HttpServerRequest req) {
if (req.uri().equals("/")) {
// Serve the index page
req.response().sendFile("simpleformupload/index.html");
} else if (req.uri().startsWith("/form")) {
req.expectMultiPart(true);
req.uploadHandler(new Handler<HttpServerFileUpload>() {
@Override
public void handle(final HttpServerFileUpload upload) {
upload.exceptionHandler(new Handler<Throwable>() {
@Override
public void handle(Throwable event) {
req.response().end("Upload failed");
}
});
upload.endHandler(new Handler<Void>() {
@Override
public void handle(Void event) {
req.response().end("Upload successful, you should see the file in the server directory");
}
});
upload.streamToFileSystem(upload.filename());
}
});
} else {
req.response().setStatusCode(404);
req.response().end();
}
}
}).listen(8080);
}
}
حال اگر همین مثال را با جاوا8 بنویسیم به این شکل خواهد بود.
public class SimpleFormUploadServer extends Verticle {
public void start() {
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
public void handle(final HttpServerRequest req) {
if (req.uri().equals("/")) {
// Serve the index page
req.response().sendFile("simpleformupload/index.html");
} else if (req.uri().startsWith("/form")) {
req.expectMultiPart(true);
req.uploadHandler( upload)-> {
upload.exceptionHandler((Handler)( event) ->{
req.response().end("Upload failed");
}
});
upload.endHandler((Handler)( event) ->{
req.response().end("Upload successful, you should see the file in the server directory");
}
});
upload.streamToFileSystem(upload.filename());
}
});
} else {
req.response().setStatusCode(404);
req.response().end();
}
}
}).listen(8080);
}
}
همانطور که نا گفته پیداست لامبادا باعث کوتاهی کد و خوانایی بهتر و ... شده که یک نتیجه به دنبال دارد:
vertx که با جاوا 8 دلپذیر تر شد.