Solved

Java Lambda expression help

Posted on 2016-07-25
6
57 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
collection output issue 9 90
jsp error 6 67
glassfish admin console 1 45
Setup GlassFish 4 53
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

738 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