sebastiandaschner news


sunday, march 12, 2017

Welcome to my very first newsletter!

The idea of this newsletter is to publish small, helpful tricks and snippets from all across Java, Java EE and computer science and keep the longer blog posts to my blog.

I’m writing this sitting in rather cold MontrĂ©al after the ConFoo conference. That was the third conference for me this year — after Jfokus and jDays, both in Sweden — and it has been a rather quiet start into 2017. Currently working on new client projects and elaborating new material, on topic that I find are to be addressed while working in client projects.

 

Latest activity

 

Java EE 8

Java EE 8 is heavily moving forward. JAX-RS has advanced impressively with reactive client and SSE support and Oracle seems to take the deadlines for EE 8 — as set on last JavaOne — pretty seriously. One has to rush to keep up with all the news hitting the expert group mailing lists.

As always: I encourage everybody to peek into the JCP and the standards how Java and Java EE will evolve in the future. And not only that but: provide feedback! I can guarantee you all of us expert group members highly appreciate as much feedback from “out-of-the-field” as possible.

 

Asynchronous CDI event handling

Staying in the Java EE topic here is a handy way of how to handle asynchronous CDI events in a Java EE application. Although the CDI spec doesn’t support @Asynchronous behavior yet, we can use this — by declaring an EJB. This will enable us to declare the public business method of the EJB as asynchronous — that will also be applied to event handling.

@Path("coffees")
public class CoffeesResource {

    @Inject
    Event<NewCoffee> newCoffees;

    @POST
    public void createCoffee(Coffee coffee) {
        // do something
        newCoffees.fire(new NewCoffee(coffee.toString()));
    }
}
@Singleton
public class NewCoffeeListener {

    @Asynchronous
    public void onNewCoffee(@Observes NewCoffee newCoffee) {
        System.out.println("long running process...");
        // this will block for 2 seconds
        LockSupport.parkNanos(2_000_000_000L);
        System.out.println(newCoffee);
    }

}

The coffee will be created via HTTP POST and the response 204 No Content will immediately be sent — two seconds later the log Coffee{type=ESPRESSO, size=MEDIUM, beanOrigin='Colombia'} will appear in the log.

I showed this and other “lightweight” Java EE 7 examples at my ConFoo talk. Also see the uploaded code-snippets.

 

Remove collection element that match a criteria

Since JDK 1.8 there is the handy new Collection#removeIf method that takes a Predicate<T> and removes all elements in the collection that matches these:

List<String> coffeeShops = new ArrayList<>();
coffeeShops.add("Blue Bottle Coffee");
coffeeShops.add("Four Barrel Coffee");
coffeeShops.add("Coffee Collective");
coffeeShops.add("Obscura Coffee Roasters");
coffeeShops.add("About Life");

assertThat(coffeeShops.size(), is(5));

coffeeShops.removeIf(t -> t.contains("Coffee"));

assertThat(coffeeShops.size(), is(1));

 

IntelliJ navigation

As you might know I’m a big fan of developer’s productivity, minimizing overhead and maximizing my keyboard usage. Therefore I’m always challenging myself to remember new application keybindings. Today’s takeaway:

In IntelliJ you can navigate to CDI injection points by Ctrl + Alt + Home. Just place the cursor over the @Injected item and jump to the type declaration, producer method or field, respectively. Also works vice versa.

 

Thanks a lot for reading and see you next time!

 

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