sebastiandaschner news


tuesday, june 20, 2017

Buenos dias de Barcelona!

After coming back from the long trip in Japan I had a week in Munich to prepare future topics. I gave a vJUG presentation on the topic of Event sourcing, distributed systems and CQRS with Java and now it’s sunny Barcelona. Yesterday, I spoke on JBCNConf that has a quite impressive speaker lineup this year — as if I needed more reasons to visit that city again :-)

On Thursday I will be speaking at Voxxed Days Luxembourg and this will be the first time in Luxembourg for me. In the weeks after I will hold client workshops on the topic of Docker, Kubernetes & OpenShift.

 

What’s new

 

Java EE 8 sneak peek

This and the following newsletters will be a sneak peek what is about to come with the next Java EE version 8. The following are some of my personal highlights.

 

CDI async observers

CDI events are a practical way how to decouple your code functionality. Per default these events are handled synchronously. If we want async events, for now we need an EJB method as event handler, annotated with @Asynchronous. See my newsletter issue #1 for an example of that.

CDI 2.0 will include asynchronous observers without even requiring the use of EJBs.

@Inject
Event<NewCoffee> newCoffees;

public void createCoffee(Coffee coffee) {
    // do something
    newCoffees.fireAsync(new NewCoffee(coffee.toString()));
}

The asynchronous behavior must be specified on the publisher side, using fireAsync and on the observer side, using @ObservesAsync.

public void onNewCoffee(@ObservesAsync NewCoffee newCoffee) {
    System.out.println("this will run for a while, but nobody's waiting for us...");
    // this will block for 2 seconds
    LockSupport.parkNanos(2_000_000_000L);
    System.out.println(newCoffee);
}

 

CDI observer priority

More about CDI observers in 2.0. In the new version we will have the possibility to order event observers with @Priority, a concept well known to the Java EE world already.

public void onNewCoffee(@Observes @Priority(100) NewCoffee newCoffee) {
    System.out.println("first: " + newCoffee);
}

public void alsoOnNewCoffee(@Observes @Priority(200) NewCoffee newCoffee) {
    System.out.println("second: " + newCoffee);
}

Just make sure when using this feature that you didn’t violated the loose coupling and single point of responsibility principle by needing to order your event handlers.

 

JSON-B & JAX-RS integration

Java API for JSON binding (JSON-B) is a completely new JSR to complement declarative mapping from POJOs to JSON and back. Therefore JSON-B is for JSON what JAXB is for XML. This API will also be usable similarly to JAXB, so if you’re familiar with the latter already, you will experience a low barrier to entry.

public class CoffeeOrder {

    @JsonbTransient
    private long id;

    @JsonbProperty("type")
    private String coffeeType;

    @JsonbDateFormat("dd.MM.yyyy")
    private LocalDate dueDate;

    // getters & setters ...

}

JSON-B transforms an example CoffeeOrder instance to the following JSON object:

{
    "type": "Espresso",
    "dueDate": "21.06.2017"
}

Where JSON-B will especially shine is when combined with JAX-RS. JAX-RS 2.1 seamlessly integrates JSON-B to make the JSON mapping as convenient as JAXB with XML is today.

Of course JSON-B also includes adapters and serializers to customize the output. Please have a look into the JSR 367 specification for all the features.

 

Bonus: what is 0xCAFEBABE?

Now a bonus topic. Have you ever heard of the reference to 0xCAFEBABE in the Java world and wondered what that means?

0xCAFEBABE is the so called magical file header of a Java class file, the 4 bytes in hexadecimal notation with which each and every .class file begins.

The name was chosen on purpose since a unique identifier was needed to classify Java class files. Since the naming options are limited if you want to “write words” in hex (you only have characters A to F), “cafebabe” was a creative choice :-)

If you’re interested in that, see what Wikipedia says about it.

 

Stay tuned for more updates on Java EE 8 in the next issues, 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.