Sep 27, 2013

FlightRecorder in Java 7u40

Flightrecorder in JDK 7u40 - Come on Oracle


I visited two sessions at the JavaOne 2013 about the brand new flightrecorder in Java 7u40 for the hotspot JVM.

What is flightrecorder?

Flightrecorder is a sampling based profiler in combination with an event collection engine built into the JVM. Most of the code is  orginally from the JRocket virtual machine which Oracle got from the BEA/Weblogic aquisition. It is a comercial feature you have to unlock by using command line JVM startup parameters. For use in production you have to have a valid license. For development this is free to use.
The intresting thing is, that flightrecorder can run on production systems with very little overhead. The data can constantly collected in circular memory buffers and there are several policies available how to dump the data to disk which makes an offline analysis possible.
The profiler is sampling based and collects start/end times and an estimated (sampling) call count. It has a constant pool to record stack-traces for this calls very efficient. To do so, Oracle has modified the internal representation (in C) of classes and added a Class-Id to each class. Calls to their internal API (undocumented and unsupported) are build direct in the JVM. Other data which is collected by flightrecorder already exists like GC statistics or heap metrics. Yes you have read right: Oracle changes the JVM internally for support of their proprietäry products.

Oracle has build on top of that a eclipse based tool which is called "mission control". If you download an Oracle JDK 7u40 the tool is located in the jdk/bin folder.

The tool looks nice but for professional performance analysis we need much more.
  • flightrecorder is currently not open for application based events. For example: In a banking application we have to know to which bank money is transferd. Otherwise the profiler statistic says nothing because we need to know which bank causes the problem in our transaction. So events have to be enriched witch context information. 
  •   flightrecorder has this capabilities but there is currently no public API for application events or context enrichment.
  • flightrecorder has to have a remote interface to poll events. That is needed if you want to record constantly.
  • The dataformat of the dumpfile is not open nor there is any API to get the data. So you can not write you own analysis tool for a flightrecorder dump. This is needed when you want to do own statistics on the data.


Healthcenter for the IBM JVM

The IBM JVM has the same concept build in their own Virtual Machine. But they have a public API for tool developers. It enables developers to access all profiling or monitoring data remotly. So you
can write you monitoring tool which constanly consumes the data from the IBM JVM. That is currently not possible with flightrecorder and the Oracle hotspot JVM.
The businesscase of IBM seems to be based on a support model. If you have a support contract with IBM for you JVM you get the API for free.  I saw a presentation at the JavaOne where IBM presented the healthservices and their API. One thing I am absolute agree with them is, you can not write the one and only monitoring, profiling, analysis tool. Without open APIs the hole thing gets worthless.

Come on Oracle

What we really need is a public API to collect our own events and to implement our own storage policy for long term evaluation our datacenters, cluster or distributed applications.

Johannes Weigend


CompletableFuture in Java 8

Fork/Join Application Programming with CompletableFuture in Java 8


Today at the JavaOne I saw a very inspiring session about reactive programming with Java 8. The idea is quite simple. Build a pipeline of later running actions (Futures) which can run asnchron and provide synchronisation mehtods to join (combine) or split parallel actions. It will us help to parallize our code to make a application more reactive and powerful.

Why we should do so is written here: http://www.reactivemanifesto.org/

Look at some code:

final CompletableFuture<ArrayList<User>> users = 
        CompletableFuture.supplyAsync(() -> {
            //lookup all users ... this can run for a long time
            return new ArrayList<User>(Arrays.asList(new User("u1")));
        });

final CompletableFuture<ArrayList<Product>> products = 
        CompletableFuture.supplyAsync(() -> {
            //lookup all products ...  this can run for a long time
            return new ArrayList<Product>(Arrays.asList(new Product("p1")));
        });

 final CompletableFuture<String> report = users.thenCombine(products, (u, p) -> {
            return u.toString() + p.toString(); // demo here
        });

 System.out.println(report.join()); -> "{u1}{p1}"

The clue lies in the supplyAsync factory method. It returns a Future which has as parameter the return value of the parallel executed action. Here in this example we want the lookup for products and users and then combine the results to a string. 
The searching for products and the searching for users runs parallel. This class is a very useful wrapper over the ForkJoinPool of JDK 7. You can set up easily parallel workflow pipelines. 
You can also use another executor to run in a JEE server (Glassfish 4 has concurrency APIs to execute things parallel). Look at the JSR 236.

See also:
http://download.java.net/lambda/b88/docs/api/java/util/concurrent/CompletableFuture.html
http://java.dzone.com/articles/java-8-definitive-guide
https://java.net/projects/concurrency-ee-spec


Sep 26, 2013

Lambdas and Streams in JDK 8

Lambdas and Streams at the JavaOne 2013

Lambda

The new Lamda expressions in combindation with the Stream-API in Java 8 leads to the biggest change in Java Programming since the introduction of Generics in JDK 5.
Labmda Functions remove tons of boilerplate code from anonymous inner classes and make the code more compact and readable.  

The following example shows typical UI code which is needed for JavaFX in JDK 7.

 button.setOnAction(new EventHandler<ActionEvent>() {

      @Override

      public void handle(ActionEvent actionEvent) {

          ...      
      }

    });

In JDK 8 the same looks like this:

button.setOnAction(event -> {

       ...
});
The JDK has tons of new Methods even in existing interfaces like java.util.Collection which use Lambda functions. To make that possible, you can now provide a default implementations of methods in interfaces, so it is now possible to add new methods to an existing interface without breaking every implementor. The following example shows the forEach method of the java.util.Collection interface.


List<Person> persons = ...
persons.forEach(p -> p.setLastName("Doe"));

But thats all syntactic suger. But whats really makes a difference in daily programming is the new Stream API.

Streams != java.io

What is a Stream? You can think of an iterator, which can access a real collection but also think of generated data like primes or other endless sequences. 
You can filter, map, reduce, split streams and form other streams with other content or even populate new collections from a stream. Here are some examples:

1. Compute a list of adults from a collection of persons.



List<Person> greater18 = persons.stream()
        .filter(p -> p.getAge() >= 18)
        .collect(toList());

The filtering is lazy. It only executes when the stream is consumed (by the collect(toList()) method. The filter/collect methods are methods from Stream itself. It is a fluent API design.


2. Compute a set of ages of adults from a collection of persons.


Set<Integer> ages = persons.stream()
        .filter(p -> p.getAge() >= 18)
        .map(Person::getAge)
        .collect(toSet());

The new thing here is we map a Person to an Integer (the age) by referencing the getAge method. It looks a little like C++. It is legal to use existing functions as lamdas. 

3. Compute the population per age


Map<Integer, Long> population.perAge = persons.stream()
        .collect(groupingBy(Person::getAge, counting()));

The map stores the age and maps the count of people.

4. Compute the names per age


List<Person> persons = ...
Map<String, Long> population.perAge = persons.stream()
        .collect(groupingBy(Person::getAge, mapping(Person::getName, toList()));

It really makes programming different when you think of normal collection based programming. Java Programms will look like never before.

BUT: Not in every case lamdas leed to better readable and maintainable code. The following example (shown here at the JavaOne as valid example) is a abuse. 



 List<Person> pa = Arrays.asList(new Person("Peter"), new Person("Ken"));

 // a) 
 for (int i = 0; i < pa.size(); i++) {
    Person p = pa.get(i);
    if (p.getName().equals("Ken")) {
        doSomething(p);
    }
 }

 // b) Exact the same !!!
 IntStream.range(0, pa.size())
     .mapToObj(pa::get)
     .filter(p -> p.getName().equals("Ken"))
     .forEach(Main::doSomething);


The section b) is a exact port of the imperativ version with a functional syntax. That does not make sense. The example is hypothetic but if programmers are tought lamdas are cool in every way - this can be the result. 
In terms of language design it is a problem when a language allows to many different ways to express the same. C++ is such a problematic example. The complexity lead to the development of Java...

Make sure you have read the pragmatic programmer before you get lost in <<>>, -> or :: symbols. 

Johannes Weigend


Sep 25, 2013

Unlocking the Java EE Platform with HTML5 - JavaOne 2013

Unlocking the Java EE Platform with HTML5


Geertjan Wielenga, David Haefelfinger and me (Johannes Weigend) did a interessting Talk about HTML5 and JavaScript in combination with Java EE7. I will now share my thoughts here.

What I did was a typical HTML5 Application with a pure HTML/JS Client and a REST Server Backend. The Application looks like this:






This is a typical Single-Page Application with two logical Screens. After entering a search expression the logo disapears (annimated) and the result list fades in. The Architecture of this application looks like this.






As you can see on the Architecture Overview, the Client is developed with HTML5/JavaScript and CSS. The Client executes a Rest Call to a Glassfish 4 Server which is built by using JEE7 and JAXRS. On the client side I use JQuery for AJAX Interaction and DOM access. Knockout.JS for binding (The JSON result of the REST Call back to out HTML code) and bootstrap which gives me a column oriented (and responsive) layout. 

I also integrate an EJB-Session Bean to get the REST-Annotated-Code clean of business/searchlogic. This kind of Client has some benefits over a typicall JEE Java Server Faces application.
  • The Server itself can be stateless – so we can distribute free in the cloud – each call can go to a different server. That helps for scalability
  • The development is HTML5/JS/CSS for the client and pure Java on the server. We have a clean separation of concerns – and we can distribute the development easily in different teams.
  • We can use JavaScript libraries on the client – like bootstrap – where the server is PHP.

This kind of client has also some pain points
  • JS is currently not typed. So the autocomplete support is limited. Refactoring is a nightmare. Microsoft works currently on Typescipt which should make is easier for such tools. Netbeans support for typescript is on the way.
  • The current library situation is like the Wild West. Everybody claims to have the best library for application development – There are many unsolved questions if you want to build enterprise scale applications on that unsafe basis.
But nevertheless, this kind of application is a current trend. I will show you in this blog how to build this application with Netbeans 7.4 and Glassfish 4.

The video shows every step of my demo at the JavaOne.