sebastiandaschner news


friday, june 30, 2017

Good morning from Sidi Bou Saïd, Tunisia.

Last week I traveled to Luxembourg to speak on Voxxed Days about the topic of Lightweight Java EE. It was a very nice and engaged audience and I enjoyed speaking there. Surprisingly the weather in Luxembourg was quite muggy and almost as warm as in Barcelona, so it seems no matter where you’re traveling in Europe right now, you can’t escape warm temperatures ;-)

 

What’s new

Now more Java EE 8 sneak peek and my personal favorites.

 

JAX-RS 2.1 reactive clients

Since reactive programming is used more and more, the JAX-RS 2.1 client will include new functionality to make HTTP calls with directly returning reactive type. This makes working with these approaches easier and reduces boilerplate code.

CompletionStages are supported natively — other types and libraries can be added via extension.

To make reactive calls, use the rx() method of the Invocation.Builder:

CompletionStage<List<JsonObject>> forecasts = client
    .target("https://example.com/weather/forecasts/")
    .request()
    .rx()
    .get(new GenericType<List<JsonObject>>() {});

forecasts
    .thenApply(o -> o.getInt("temperature"))
    ...

 

JAX-RS 2.1 client standardized timeout

A small but very helpful improvement to remove vendor specific code are the standardized HTTP timeout methods on the JAX-RS ClientBuilder.

Client client = ClientBuilder
    .newBuilder()
    .connectTimeout(1, TimeUnit.SECONDS)
    .readTimeout(10, TimeUnit.SECONDS)
    .build();

I needed this configuration in almost every project, which for now had to be realized with vendor specific features.

 

JSONP 1.1 collectors

The JSONP 1.0 specification shipped with Java EE 7 is already very useful and well thought out. In JSONP 1.1 besides support for certain RFCs we added JsonCollectors to simplify the use of streams with JSONP types:

List<String> names = ...

JsonArray persons = names.stream()
    .map(n -> Json.createObjectBuilder().add("name", n).build())
    .collect(JsonCollectors.toJsonArray());

 

Bonus: The two unused keywords

In Java there are 53 keywords, some are very common like class or int while others appear not that frequently, like volatile or transient.

However, there are two keywords that are not used at all. Can you name those two?

The first one is relatively easy to guess, it’s goto. A long time already programmed have been advised not to write goto statements and jump to random points in the program manually. This is why Java doesn’t support that feature so we’re not even tempted :-)

The second one is quite common in C++, it’s const. In Java we obviously have the final statement to make variables and other concepts immutable, the actual behavior of const never made it into the platform.

The Java Language Specification also says that reserving but not using these words “[…​] may allow a Java compiler to produce better error messages if these C++ keywords incorrectly appear in programs”.

In that sense: happy hacking (without goto and const)!

 

Stay tuned for more updates on Java EE 8 in the next issue, thanks a lot for reading and see you next time!

 

Did you like the content? You can subscribe to the newsletter for free:

All opinions are my own and do not reflect those of my employer or colleagues.