Pages

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, November 28, 2012

Java 8 - Closures, Lambda Expressions Demystified

Project Lambda, Closures - JSR 335 (Part 1)
Lambda Expressions are probably one of the most interesting changes of the Java Language. They will add totaly different syntactical elements like the Arrow Operator, Method References, Defender Methods etc. Java gains functional aspects to reduce verbosity and improve parallel processing and stability.

Why Lambda
We all know Java Language is an object oriented programming language and we are using it with passion. But sometimes we want to pass some code to a method instead of an object. To make this happen we are implementing Callbacks. This leads us to develop verbose code and it feels not like to be well supported by Java. An additional use case is dealing with groups of data. Sometimes it is necessary to execute a specific algorithm to a group of data. Functional programming langues support things like "forAllDo". In Java you need to implement that by yourself or have to use some utility classes from third party libraries. This might be verbose and creepy.
Java 8 Blog Series

   1. Closures, Lambda Part 1 - Demystified
   2. Closures, Lambda Part 2 - Deep dive
   3. Project Jigsaw
   4. Date and Time API
   5. Annotiations, Checker Framework
   6. Concurrent Collection Framework

Setting Up Lambda
Because the development of Java 8 has not finished yet and the IDE Support is in an rudimental state, I recommand following environment set-up:

Download

Proceed following steps to set up your environment and run Java 8:
  1. Download NetBeans and Java 8 (use Links above)
  2. Unzip NetBeans and Java 8 JDK
  3. Run NetBeans (with command line param: --jdkhome YourJava8Dir)
  4. Check if your sources compile in java8

At the following I want to give you a short overview what is possible with Closures and Lambda Expressions. After that I will explain Lambda systematically to show up what happends behind the scenes.

Part 1: Lambda in Action

Anonymous Inner Classes

Classic approach
At the following snippet you can see how we use Anonymous Inner Classes in Java all the time. Adding an ActionListener to an UI Element is an usual use case. NetBeans shows a big warning: This anonymouse inner class creation can be turned into a lambda expression. If you press Alt+Enter to fix it, NetBeans will convert the source code into a lambda expression.

The Lambda way
By using lambda the source code looks like this: Notice that our code is les verbose. Furthermore it is not necessary to import "java.awt.event.ActionListener" any more. Moreover it is possible to get rid of class casting and some verbosity by using type inference: The Type of "ae" depends on the context in which it appears (Target Typing). That allows us to delete the import of "java.awt.event.ActionEvent" and we are happy to see a little less dependency. I explain more about the SAM-Types and what happens there later.
 
Method References

Lambda Expressions provide far more than dealing with anonymous methods. It is possible to adress the invocation of static and non static methods by using "::" operator.

Static methods
Non static methods
Arrays.sort will invoke the compareTo method of myObj because it has the reference of that method.
 
Virtual Extension Methods

Extension Methods alias Defender Methods alias Default Methods exists to integrate the new previous introduced features of Java 8 into Java 7 and grant backwards compatibility. All the functional features would have no use if we could not find them as a part of Java langauge core, so there has to be a way to change things like the Collection-Framework but ensure same behavior in legacy code without compile errors. Virtual Extension Methods are highly discussed because of an insane impact to the language. (e. g. interfaces with method bodys)
List is an well known interface and it became again an victim of Java language extensions. Besides some confusing abstract wildcard generic types, we can find method bodys within the interface definition. So what tells us the code now? We now have an optional implementation contract between interfaces and classes, like we have in abstract classes with implemented methods. Every implementation of List has the opportunity to overwrite "sort" but it is not obligatory. This is by far the most strange language extension of Java I have seen so far. I understand that such abominations are necessary for compatibility reasons. However I feel bad to see the highest abstraction level becomming a concrete implementation.
 
JDK integration and improvement  
 
JDK improvement starts in "java.util.functions". You can see the current content of the package at the right image. I picked some interesting interfaces that I discuss briefly.

BinaryOperator.class - combines two operands
Mapper.class - maps from class a to class b (within a chain)
Predicate.class - determines if the input object matches some criteria
Besides new packages and classes Lambda Expressions will improve existing code. Probably the collection framework will change a lot. A functional way of handling groups of data could be a big advantage. Less verbose code leads to more stability and functional processing without sideeffects makes parallel processing much easier.

Example: Collection.removeAll (with filter) Lambda way to use That's it for now. I hope you enjoyed my first part of my Java 8 Blog series. Please notice that "Lambda Part 2: Deep Dive" is currently in work. I will publish as soon as I can. I will show you some technical specifications, rules and compiler behavior to make the demystification perfect. So long.

Java 8 Blog Series

Saturday, October 27, 2012

Dispatching Web Service calls (Async, Sync, OneWay)

If you decide to work with Web Services as a possible SOA implementation, you have to read and understand very much specifications. In Java you may start with JAX-WS and JAX-RS as a SOAP and RESTful way to get your things done. In this Blog I jump deep into JAX-WS to use the Dispatch API. You can see the different synchronous and asynchronous ways to send and recieve messages from a Web Service. Most important goal is to unterstand how "OneWay" dispatching works.


Setting up the environment


To keep things as simple as possible I build up a small Web Service Endpoint and Web Service Client using Java SE. (No EE Container needed) A simple JAXB annotation binded message class will be the communication artifact. This leads us to simple Szenario:
  • Java SE Web Server (Endpoint, JAX-WS)
  • Java SE Client (Dispatch, JAX-WS)
  • JAXB compatible Message Class


JAXB compatible Message Class


First we create a class that can deliver our messages. It has to be JAXB friendly, so we use  corresponding annotations to declare Java and XML Binding.

MyJaxbMessage.java


Java SE Web Server (Endpoint, JAX-WS)


Then we create a simple server instance that will respond to our client dispatcher. The server will wait 3 seconds to simulate some work.

MessagingServer.java


Java SE Client (Dispatch, JAX-WS)


Now we build up our simple client that calls the server in four different ways.

MessagingClient.java - Part 1


Dispatch a synchronous call. Blocking and waiting for server response 3 seconds.

MessagingClient.java - Part 2

Result Log

1 - Normal - Start: Sat Oct 27 20:42:44 CEST 2012
1 - Normal - End: Sat Oct 27 20:42:47 CEST 2012 response: Hello, Foo 1 from server: Sat Oct 27 20:42:47 CEST 2012



Dispatch a OneWay call. Important: The call is synchronous!

MessagingClient.java - Part 3

Result Log

2 - OneWay - Start: Sat Oct 27 20:42:47 CEST 2012
2 - OneWay - no response End: Sat Oct 27 20:42:50 CEST 2012


As shown in result log the OneWay call was synchronous. The server responded just an empty message. This is usefull to reduce network communication but will NOT make a call asynchronous. Same counts for the @OneWay annotation.


Asynchronous call with Response

MessagingClient.java - Part 4

Result Log

3 - Asynchronous - Start: Sat Oct 27 20:42:50 CEST 2012
3 - Asynchronous - End: Sat Oct 27 20:42:50 CEST 2012
 - 3.1 simulating async work
 - 3.1 simulating async work
 - 3.1 simulating async work
 - 3.1 simulating async work
 - 3.2 Asynchronous request respondet at: Sat Oct 27 20:42:54 CEST 2012 with Response: Hello, Foo 4 from server: Sat Oct 27 20:42:53 CEST 2012



Asynchronous call with Future

MessagingClient.java - Part 5

Result Log

4 - Asynchronous with Future Start: Sat Oct 27 20:42:54 CEST 2012
4 - Asynchronous with Future End: Sat Oct 27 20:42:54 CEST 2012
 - 4.1 simulating async work
 - 4.1 simulating async work
 - 4.1 simulating async work
 - Future responded asynchronous Hello, Foo 3 from server: Sat Oct 27 20:42:57 CEST 2012
 - 4.1 simulating async work
4 - Asynchronous request with Future respondet at: Sat Oct 27 20:42:57 CEST 2012 with Response: de.hinkel.jaxws.MyJaxbMessage@ee51b2c



As shown in the result logs we sucessfully made 4 diffierent kinds of Web Service calls just by invoking different methods an the Dispatch class. 1. Synchronous with response, 2. Synchronous without response, 3. Asynchronous with response, 4. Asynchronous with future. Moreover we could see how Java SE enables advanced Web Service Endpoint- and Client testing by a programmatically approach of JAXB and JAX-WS usage.

Tuesday, December 13, 2011

Spring Web and SmartGWT integration

While working on my current open source project called "openKanban", I got surprised how easy and smooth integration between SmartGWT and Spring Web was.

Server (Spring Web)

I use standard Spring Web Dispatcher Servlet with some controller on server side, which I register by using a component scan. Hence, this is all I need except a scan entry in the applicationContext.xml:

BoardController.java (class header)

To get into action I need a simple Method which is configured by the standard Spring Request Mapping and Request Path Parameter:

BoardController.java (POST method)

There is nothing special about "@RequestMapping" and "@PathVariable". In combination with the class header mapping annotation it means:

Listen to all HTTP-POST requests that address the rescource:
/service/board/[0-n]/activity/add
take the Parameter [0-n] out of the Uri and put it into a variable called parentBoardId.

A little bit more magic comes into play when you watch that method returning a DTO-Object serialized as a JSON String. And that's what SmartGWT or any other requesting client that is calling this method gets. There is no need to configure JSON at any application context. Spring itself checks the classpath and if it finds a JSON serializer, @ResponseBody will return JSON.


I do not use Spring Security in openKanban. To keep it small and simple I decided to use the "JSR-299 CDI Interceptors for Spring Bean"-Implementation and run a self-made @SecurityInterception on any controller method to check if the user is authenticated. I will provide more information about this interceptor in a further posting. For now it's enough to know, that every controller method will be checked for authentication.


In order to demonstrate SmartGWT DataBinding integration I add another HTTP-GET method example:


BoardController.java (GET method)

Summary

There are two available rescources:
  1. /service/board/[0-n]/activity/add (on POST)
  2. /service/board/[0-n]/contact/get (on GET)
Both will call a method that is returning an "automagically" serialized JSON String.

Client (SmartGWT)

SmartGWT provides a very versatile communication. The following example will show you what worked really great in the scope of my project. In this context I preferred using two different kinds of request usage:
  1. Configurated DataBinding on SmartGWT
  2. Using standard GWT RequestBuilder

Configurated DataBinding on SmartGWT

Any SmartGWT widget that implements the interface DataBoundComponent provides a databinding mechanism. In this case it is necessary to define a DataSource, corresponding DataSourceFields and wire it together:

BoardContactTileGrid.java (constructor)

TileGrid is a DataBoundComponent. That's why it is possible to add a DataSource. As usual SmartGWT offers you some configurations. This widget will automatically fetch data when the object of the widget is rendered at the client. That means that it is necessary to have a correct DataSource configured at object creation time.
Let's see what the DataSource looks like:

BoardContactDS.java (constructor)

This is a simple DataSource configuration. After rendering the corresponding widget, an autofetch will trigger a HTTP-GET request to the configured URL. The server response has to be serialized as JSON. We discussed that already in the server section of this posting. It is possible to change the required response to REST, XML or some other custom format. Because I did not need a wsdl contract I decided to use a simple JSON format.

Using standard GWT RequestBuilder

The com.google.gwt.http.client.RequestBuilder seems powerful but some lines of code are necesarry to get data serialized and send to the server. A request is built in four steps:
  1. Creation of RequestBuilder object and passing the HTTP-Method and URL
  2. Configure HTTP-Header
  3. Configure HTTP-Parameters
  4. Send Requests and add a Callback
As there are many different requests needed and as they look pretty much the same, abstraction and generic are very helpful in order to reduce duplicate lines of code. So instead of having around 40 LoC on every call I encapsulated it down to this:

Two lines of Code for a request

This leads us to the following request:
  1. uri: service/user/remove/
  2. parameters: contactMail=stop@hammertime.de&id=42

This works for self defined content as well as for whole objects.


Two lines of code for a request

As you can see I used two ways to wrap all the HTTP stuff. It is either possible to pass static URL parameters or to pass complete Data Transfer Objects (DTO's) to the PreparedRequest to get things done. This has been enabled by using generic typedef, varargs and polymorphism:

PreparedRequest conctructor for static parameters

Java 5 varargs provides flexibility to add as much parameter as I like. The generic DTO to string serialization looks like:

PreparedRequest conctructor for DTO's

The PreparedRequest parameter needs to be typed as a subclass of RequestParameterMap. This relationship enables a polymorphic call to the method "valuesToMap()" which has to be implemented by every DTO.

RequestParameterMap

For a better understanding find below a simple example of an implementation:

ActivityDTO valuesToMap implementation

Although I implemented some level of abstraction, some weak points still remain. If there is a new DTO attribute, it must be added to the valuesAsMap method and there is no "compiletime safety" to make sure it has not been forgotten. Because I could not use Java Reflection API on client side code (remember I am talking about code that is converted to JavaScript), things may get a little bit harsh. Anyway I am still positive about an easy, robust and practicable integration between Spring Web and SmartGWT over HTTP.

Saturday, November 19, 2011

Ceylon - Eine neue Sprache für die JVM

Ceylon befindet sich aktuell in der Entwicklung aber ein Blick auf den Sprachumfang gewährt einem jetzt schon tiefe Einblicke in Konzepte und Funktionsweisen. Außerdem sind Diskussionen und Entscheidungen zu konkreten Elementen der Sprache transparent. Die Ziele der Entwickler von Ceylon sind hoch gesteckt. Nach eigenen Angaben liegt der Fokus von Ceylon auf:

  1. Eleganz
  2. Lesbarkeit
  3. Typsicherheit
  4. Meta-Programmierung

Die Programmierung soll wesentlich einfacher sein als mit Java. Besonders in Bereichen von generischer Typsicherheit räumt Ceylon auf. Die Sprache ist mit funktionalen Bestandteilen angereichert, verliert sich aber nicht in verketteten Operationen ohne Seiteneffekte sondern versucht das Beste aus den Welten der Objektorientierung und der funktionalen Programmierung zu vereinen.


Entwicklung

Aktuell existiert ein Plug-In für Eclipse, sodass diese IDE jetzt schon für Ceylon zur Verfügung steht:

Obwohl man bereits einfache Applikationen schreiben kann, stolpert man früh auf kleinere Bugs in der Sprache und Ungereimtheiten in der IDE. Beispielsweise funktioniert das Autoboxing und -unboxing derzeit nicht konsistent.
Derzeit wird kein offizieller Release-Termin für Ceylon veröffentlicht. Konkrete zeitliche Angaben über die Erreichung von Meilensteinen fehlen ebenso. Inhaltlich ist jedoch ausreichend Transparenz vorhanden um sich ausmalen zu können wie zukünftig Ceylon aussehen wird:

Wörterbuch

Java               Ceylon
implements         satisfies
public             shared (kontextabhängig)
protected          - 

private            -
abstract           formal (Attribute/Methoden)


Merkmale (Ceylon vs Java)

Im Folgenden habe ich die aus meiner Sicht interessantesten Merkmale von Ceylon zusammengefasst.

Keine NullPointerException:
  • Typsicherheit bei null
  • Nullwerte sind Inkarnationen der Klasse Nothing
  • Mögliche null-Werte müssen den Datentyp bei der Deklaration erweitern:  
    • String? vorname  = "Frank"
    • String|Nothing vorname = "Frank";
  • Korrespondierendes exists-Konstrukt
    • if (exists vorname) {...}

Nur ein Zugriffsmodifikator: shared
  • Gültigkeitsbereich von shared ist dynamisch
  • Kontextabhängig Funktionsweise
  • Klassen sind standardmäßig Paketlokal, shared muss explizit deklariert werden
  • Eine shared Klasse kann keine Paketlokalen Attribute besitzen
  • Protected kann nicht abgebildet werden

Enumerierte Ableitungen:
  • Es kann über enumerierte Subtypen eine direkte Verknüpfung der Basisklasse zu der Anzahl der entsprechenden Subtypen hergestellt werden
  • Dadurch wird zur Compile-Zeit sichergestellt, dass alle Ableitungen in polymorphen Aufrufen explizit im Code behandelt werden 
Beispiel:
abstract class Tier() of Hund | Katze | Maus { ... }
...
void sagHallo(Tier tier) {
    switch (tier)
    case (is Hund) { //wuff... }
    case (is Maus) { //pief... }
    // compile Error: Katze muss ebenfalls behandelt werden
}
 

Attribute:
  • Können einfache Zustände halten
  • Können getter/setter implizieren ohne sie explizit zu deklarieren 
  • Sind polymorph und überschreibbar
  • Können in Verbindung mit Closures verwendet werden

Initialisierung:

  • Fokus liegt auf einem einheitlichen Initialisierungs Look-And-Feel von Klassenattributen und lokalen Variablen. (anders als in Java)
  • Keine Konstruktoren, Initialisierung wird aufgeteilt in:
    • Initialisierungsparameter (werden hinter den Klassennamen geschrieben)
    • Initialisierungscode wird direkt in die Klasse geschrieben

Frage: Wie führe ich unterschiedliche Initialisierungen durch z.B. für Testklassen die in Java über den Good Citizen Konstruktor abgebildet werden?
"Am Easy to test- all dependent object I use can be passed to me, often in my constructor (typically as Mock Objects)."

Funktionale Ausrichtung:
  • Klassendeklarationen ähneln eher Methodendeklarationen
  • Eine Klasse kann auch als eine Funktion verstanden werden welche ein Closure mit den eigenen lokalen Variablen liefert.

Type Aliase und Type Inferenzen
  • Für lokale Variablen kann der Typ geschlussfolgert werden
    • value tiere = LinkedList { "Hund", "Katze", "Maus" }; 
  • Typedefs sind möglich, sollten aber sparsam verwendet werden
    • interface tiere = List<Tier>;

High-Order functions
  • Funktionen können andere Funktionen als Parameter entgegen nehmen
    • void fuettern(Essen essen, void fressen())

Vereinfachte Generics
  • Keine Java Wildcards
  • Keine Raw-Types
  • Vergegenständlichte Deklaration

Operanden Polymorphismus
  • Es existiert eine feste Zuordnung von Operanden zu Interfaces
    • == zu Equality
    • < zu Comparable
    • * zu Numeric
  • Alle Klassen, welche Equality implementieren können mit == verglichen werden


Fazit

Die Typsicherheit bei Nullwerten und die Wertsicherheit bei Subklassen sind auf den ersten Blick sehr interessant. Ob die Generics wesentlich einfacher geworden sind, ist meiner Ansicht nach fraglich, da die "declaration-site variance" von Ceylon - Generics um eine neues Konzept erweitert.
Den Ansatz shared als einzigen Zugriffsmodifikator zu verwenden, betrachte ich ebenfalls skeptisch. In Java ermöglichen public, protected, default und private das Setzen von feineren Nuancen und damit Designabsichten ausdrucksstark zu untermalen. Deshalb geht die gewonnene Dynamik von shared auf kosten der ausdrucksstarken Lesbarkeit. Der Reflection API von Java stellt Ceylon ein typsicheres Meta-Programmiermodell entgegen, welches ich als weiteres Highlight sehe und für mich die größte Stärke der Sprache bedeutet. So ist es beispielsweise möglich Objektinstanziierungen oder Methodenaufrufe zu "intercepten" ohne Code zu implementieren welcher nach entsprechenden Interecption Annotationen sucht. Das Metamodell von Ceylon erledigt das "out of the box" zur Class-Loading-Time.