Upgrade from 2.4 to Play 2.6


(Santhoshkumar Nagulavancha) #1

HI Team,
I am new to Play and trying to learn Play Framework2.4+. Can somebody help me to understand the importance of get(0) in the line 6 below.

As far as I understood, we are returning failed-count from lambda function at line 5.
Why we need to provide extra get(0).

1:. public int sendEvent(JsonNode jsonMessage) {		
2:	return F.Promise.promise(() -> {
3:			Event record = new Event("Category1", jsonMessage);
4:			int failedCount = <<ExternalService>>.sendEvent(record);
5:			return F.Promise.pure(failedCount);
6:		}, customServiceExecContext).get(5000).get(0);
7: }

Much appreciated your help.


(Aditya Athalye) #2

You would have needed a get(0) only if the promise wrapped some collection like List and you needed to get its first element. In your code, you do not need that.


(Marcos Pereira) #3

Hi,

From where did you get that code? Blocking the Promise (with the get(5000)) is not something that you want to do, because it fails the idea of using an execution context (customServiceExecContext) where you run the blocking code. What you should do instead is to map over the Promise to do something with the result. In your case, it looks like you don’t even need to map:

public F.Promise<Integer> sendEvent(JsonNode jsonMessage) {
	return F.Promise.promise(() -> {
		Event record = new Event("Category1", jsonMessage);
		int failedCount = <<ExternalService>>.sendEvent(record);
		return failedCount;
	}, customServiceExecContext);
}

Notice how the return is now a Promise instead of a plain int.

Best.