[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 73
  • Last Modified:

Java Lambda expression help

Hello there,

I am going through a tutorial on Vert.x and came across this lambda code which I want to replace it with ordinary code i.e. without using lambda expression. Can somebody please help me to replace this lambda expression so i know what is going on

public class MyFirstVerticle extends AbstractVerticle
{
	@Override
	public void start(Future<Void> fut)
	{
		vertx.createHttpServer().requestHandler(r -> {
			r.response().end("<h1>Hello from my first " + "Vert.x 3 application</h1>");
		}).listen(8080, result -> {
			if (result.succeeded())
			{
				fut.complete();
			} else
			{
				fut.fail(result.cause());
			}
		});
	}
}

Open in new window

0
zolf
Asked:
zolf
  • 3
  • 2
3 Solutions
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Something like this.... (untested but should give you the idea)

public class MyFirstVerticle extends AbstractVerticle
{
	@Override
	public void start(Future<Void> fut)
	{
		vertx.createHttpServer().requestHandler(r -> {
			r.response().end("<h1>Hello from my first " + "Vert.x 3 application</h1>");
		}).listen(8080, new Handler<AsyncResult<HttpServer>>() {
                     public void handle(AsyncResult<HttpServer> result) {
			if (result.succeeded())
			{
				fut.complete();
			} else
			{
				fut.fail(result.cause());
			}
                    }
		});
	}
}

Open in new window

0
 
zolfAuthor Commented:
thanks for your comments, what about the r parameter.can you also please tell me what that r is
0
 
CEHJCommented:
I think you might find it more like this (again untested)

        HttpServer server = vertx.createHttpServer();
        server.listen(8080,
            new Handler<AsyncResult<HttpServer>>() {
                public void handle(AsyncResult<HttpServer> result) {
                    if (result.succeeded()) {
                        fut.complete();
                    } else {
                        fut.fail(result.cause());
                    }
                }
            });
    }

Open in new window

Has to be said though - if the examples use FP then if you find you're having to translate things back to 'old style' code then you're probably using the wrong API
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Oops, sorry I missed that one, although it shouldn't be too hard to apply similar logic to what I did the first time...

public class MyFirstVerticle extends AbstractVerticle
{
	@Override
	public void start(Future<Void> fut)
	{
		vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
                    public void handle(HttpServerRequest r) {
			r.response().end("<h1>Hello from my first " + "Vert.x 3 application</h1>");
		}}).listen(8080, new Handler<AsyncResult<HttpServer>>() {
                     public void handle(AsyncResult<HttpServer> result) {
			if (result.succeeded())
			{
				fut.complete();
			} else
			{
				fut.fail(result.cause());
			}
                    }
		});
	}
}

Open in new window

0
 
zolfAuthor Commented:
cheers
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You're welcome
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now