What JAX-RS Filters Are Good For

JUG CH Swiss Army KnifeI recently was asked what JAX-RS Filters are good for. Here’s is the answer: They are simply as versatile as a Swiss Army Knife.

Thanks to JUG CH for this really cool gadget!

Image | Posted on by | Tagged , ,

Thank You, JUG Switzerland!

Swiss FlagJust back from Switzerland where I lectured on “JAX-RS Done Right” at PH Zurich. The room was really jam-packed, and we had to forcefully cut the Q&A to not miss the fun at the after-event and subsequent open-air dinner! Those guys really followed the talk closely and wanted facts, and I really love that with an audience — just like the really relaxed time with the organizers at the beer garden!

Audience at JUG ZurichZurich is really a wonderful town and I loved to stroll the medieval historic centre with my wife who accompanied me to Switzerland and kindly provided graphic spice for me slides. If you ever have a chance for a visit, you should really take a stop there, it’s definitively worth a detour!

Markus Karg standing at Lake Zurich

I’d like to say “Thank You!” once more to JUG CH and particularly to Lukas Eder for the invitation and his kind support. I feel like it wasn’t the last time we met at JUG CH…

P.S.: Thanks for the army knife! I always wanted to have one since I was young!

Posted in Java, Lectures, Programming | Tagged , ,

Zurich is booked out

Just noticed that my talk tomorrow in Zurich at JUG CH is already booked out. Cool! 8-)

Aside | Posted on by | Tagged ,

Microsoft® Linux®

So after making their formerly proprietary Windows operating system POSIX compatible, integrating it into a UNIX environment by providing NFS etc., acquisition of two Linux distributors (Novell with Novell OES, and with it the subsidiary SuSE with SuSE Linux) and selling Linux (SELS) licences, putting a XEN-derived hypervisor underneath, and directly contributing to the Linux Kernel, now Microsoft Corp. did the next step and provides a native Windows Docker client able to run Linux containers on Windows, but not Windows containers!

Can’t wait for the next three logical steps: Running Windows containers on Linux, open sourcing Windows to the community to get rid of the maintenance costs for further running existing Windows applications, and finally confessing that Windows had a Linux kernel since Vista. ;-)

When can we trade-in our Windows licences for Microsoft Linux copies?

Posted in Linux, Microsoft, Politics | Tagged ,

Java Forum Stuttgart 2015 – I’m speaking!

Come to Java Forum Stuttgart 2015 and attend to my talk on “JAX-RS 2.1 New Features” (Slot C1 / 08:45-09:30)!

As a member of the JSR 370 Expert Group I’d like to give insight into the work of the expert group, the planned features of JAX-RS 2.1, and the status of the specification process.

Aside | Posted on by | Tagged , , ,

Attend to the premiere of “JAX-RS Done Right” at JUG Switzerland

Often I see people doing JAX-RS applications rather complex and cluttered with technology, while the original approach intended by Java’s REST API is to separate concerns, gaining a “clean” domain model and allowing the creation of a component market.

To spread the vision of “correctly” done JAX-RS applications, I authored a new presentation titled “JAX-RS Done Right”, which I will first serve at JUG Switzerland on April 24.

Attent to this event and you will change your view on JAX-RS! Registration is now open at: http://www.jug.ch/html/events/2015/jax-rs.html.

Aside | Posted on by | Tagged , ,

Improving performance using Iterable.forEach()

Since Java 8 there is a new Iterable.forEach() method, which allows you to perform an operation upon each item of the iterated bunch of data. One might wonder why this method was added, because you could do the same using Java’s for statement since version 5 of the JRE.

There are two answers. The first is coding style. But maybe you are more interested in the second: Performance.

While working on the Babbler XMPP library, we discussed whether there will be a measurable speed difference between the for statement and the forEach method, so a benchmark was used to give the final answer. And it was an impressive answer: Once the virtual machine was warmed up, the forEach statement outperformed the for statement by far!

If you are courious why that happens, just follow our steps…:

Benchmark using for statement

public static final void main(final String[] arguments) {
  for (int j = 0; j < 10; j++) {
    final List<String> list = new ArrayList<>();
    for (int i = 0; i < 5000000; i++)
       list.add(UUID.randomUUID().toString());
    final long start = System.currentTimeMillis();
    for (final String s : list)
      s.length();
    System.out.println("Foreach loop: " + (System.currentTimeMillis() - start));
  }
}

Benchmark using forEach method

public static final void main(final String[] arguments) {
  for (int j = 1; j <= 10; j++) {
    final List<String> list = new ArrayList<>();
    for (int i = 0; i < 5000000; i++)
      list.add(UUID.randomUUID().toString());
    final long start = System.currentTimeMillis();
    list.forEach(String::length);
    System.out.println("Foreach loop: " + (System.currentTimeMillis() - start));
  }
} 

Compiler options

javac -g:none -O Benchmark.java

Decompiler options

javap -c Benchmark.java

Look at the decompiler’s output for both versions of the benchmark, and you’ll understand what’s going on…!

One part of the magic  comes from the fact that the JVM applies several optimizations to repeatedly executed code, like inlining, native compilation, etc. The use of the for statement in this code, and particularly its custom body, exist solely for the sake of this Benchmark, while the forEach method are common code, likely to be used by other methods of the same application (does not apply to this Benchmark, obviously). That increases likelihcood of such optimizations.

The other part of the magic (which does apply to this Benchmark) comes from the fact that even when there is language support for iterating Iterables, there is no support built into the JVM. What the JVM actually has to do when you do a for statement, is to invoke several methods of the Iterable interface again and again. The forEach method on the other hand may be (and actually is) implemented by each Iterable in a different way, allowing to access private members natively (without going through the published Iterable methods). And that is exactly what actually happens in ArrayList.forEach!

Conclusion

Sometimes we can learn how to write superior Java code if we take a look into the compiled result using javap. Thanks to Rafael Winterhalter who delivered this message in his inspiring JavaLand talk! In the particular case, we learned that forEach results in less bytecode calling one very fast method, which explains the measured performance benefit. Due to its nature of being a method of a central part of the JRE (the collections framework), it is highly optimized and will always run considerably faster than any custom for statement.

Posted in Allgemein, Java, Programming | Tagged ,