Solved

Java Lambda expression help

Posted on 2016-07-25
6
38 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
  • 3
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 300 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 200 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 300 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 35

Expert Comment

by:mccarl
ID: 41729516
You're welcome
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 60
scoreUp challenge 14 48
wordcount challenge 11 78
Error with Java/Cache JDBC Classpath 2 20
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now