Pages

Showing posts with label Technologies. Show all posts
Showing posts with label Technologies. Show all posts

Wednesday, October 23, 2013

Get Random Documents from MongoDB

While finalizing my first draft of nodecards, I stumbled on retrieving random documents from MongoDB. Nodecards is a small open source project which I use to try out some new technologies. It is a simple Flashcard application, written in node.js. It is kinda quick'n'dirty coded stuff, so don’t expect clean lines of code, yet. Because it was not so easy and I am not really sure that I have taken the right way I want to present you my implementation of retrieving random documents from MongoDB.

Get random within the range of a collection


To get the random document i just call count() at the collection.
After that I used skip() within the find to skip a number of documents. The skip is based on Math.random() and the previous estimated max range. Moreover I added a limit(1) to retrieve just one document. In a nutshell: My way to get random documents is by skipping documents based on a random number and limit the size to one. If you have some better and more efficient ways to do, please let me know.

Get random within a custom scope


Furthermore I had the idea to reduce the number on documents by adding a specific user defined condition. I passed in a max value that has been chosen by a user. Based on that max value I count how much elements are in the db.coll.correct array and retrieve only documents where the number of containing elements is lower than the max value. Cheers, Frank

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

Tuesday, November 27, 2012

My Top 5 Books of 2012

This is one of my favorite post of the year to write. It gives me the opportunity to look back on some of the great books I read during the past year. Most of them are highly coupled to my current job as a Software Engineer within an architecture deparment. Nevertheless I have chosen one book into the top that is more management driven, because it is the best thing I have read so far (about project management).

No. 1  The Passionate Programmer 
Creating a Remarkable Career in Software Development
by Chad Fowler

Chad Fowler is a great "Programming Lifestyle Engineer". He tells us what we need to do to be successfull as an IT-Spezialist. The book contains tons of stunning wisdoms like the "Broken Window Theory" or statements like "You can't win if you just try not to lose." It is so common for developers to be unsatisfied because they only try not to lose. They do not push thier own skill and never try to win a knowledge race. I recommend this book to everyone who is working at the IT.


No. 2  A Guide to the Project Management Body of Knowledge 
4th Edition
by Project Management Institute

A Guide to the Project Management Body of Knowledge (PMBOK Guide) is a book which presents a set of standard terminology and guidelines for project management. The Fourth Edition (2008) is the document resulting from work overseen by the Project Management Institute (PMI). The Guide recognizes 42 processes that fall into five basic process groups and nine knowledge areas that are typical of almost all projects. By that the PMBOK Guide is a very complete systematical approach to handle project specific complexity. Moreover PMI has an practical orientation. It is made by practitioner for practitioner. And that is what I appreciate. "Hands-on" project management.





No. 3  Patterns of Enterprise Application Architecture
Martin Fowler Signature Series
by Martin Fowler

This outstanding collection of Enterprise Patterns leads into the topic of Enterprise Architecture and describes some handpicked design patterns. Each pattern describes details of how it works and when to use it, together with code examples. The main topic areas are: how to layer an enterprise application, how to organize domain logic, how to tie that logic to a relational database, how to design a web based presentation, some important principles in distributed design, and handling of what we call "offline concurrency" - concurrency that spans transactions. This book helped me a lot to understand unknown frameworks as well as designen and developing enterprise software.




No. 4  Java Puzzlers: Traps, Pitfalls, and Corner Cases
by Joshua Bloch, Neal Gafter

The topic tells everthing about the book. It is just a crazy collection of traps, pitfalls and corner cases of the Java Language 2.0. For me as an big Java Fan it has been a lot of fun to read this book and try to solve or figure out the snippets. It is as an brainteaser almost as good as the concurrency courses of Heinz Kabutz.






 


No. 5  Java Web Services in der Praxis
Realisierung einer SOA mit WSIT, Metro und Policies
by Oliver Heuser, Andreas Holubek

This is a german book that I have to mention. I used it to prepare for my "Oracle Certified Expert - Java EE 6 Web Services Developer" Exam. I was looking for informativ stuff, that could help me to learn all the things that I needed. And this is by far the best thing I found. It covers almost everything you need to know if you take it serious with Web Services and you want to use them in an enterprise environment.


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.

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.