?
Solved

Java Lambda expression help

Posted on 2016-07-25
6
Medium Priority
?
62 Views
Last Modified: 2016-07-26
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
Comment
Question by:zolf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 36

Accepted Solution

by:
mccarl earned 1200 total points
ID: 41728836
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
 

Author Comment

by:zolf
ID: 41728927
thanks for your comments, what about the r parameter.can you also please tell me what that r is
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 800 total points
ID: 41728950
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
Independent Software Vendors: 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!

 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 1200 total points
ID: 41728961
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
 

Author Closing Comment

by:zolf
ID: 41729002
cheers
0
 
LVL 36

Expert Comment

by:mccarl
ID: 41729516
You're welcome
0

Featured Post

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question