Docker.io & Redis on DigitalOcean in 5mins

It all started when I casually spotted a sponsored tweet offering me $10 worth of virtual computing power from digital ocean. I had seen their ads on everything from Facebook to Gmail and thought I’d check out what the fuss was about.

I had been planning to try docker.io on something other than my macbook for a while and was tempted to run up another linode server. Instead I decided to see if I could get docker running on digitalocean.

Docker turned 0.6 over the weekend and is heading full speed towards a production grade container platform. If you are reading this post you probably already interested in docker but for the uninitiated docker allows you to ship applications as containers running in what appears to be self contained linux environments. Its is based upon linux container magic and runs within same operating system as its host.

So here is my guide to get docker 0.6.1 running on digital ocean vm (5 minutes starts now!)

First create a new droplet based upon the Ubuntu 13.04 x64 image

ImageCreate the droplet and within a minute or so you should be able to interact with your new machine. Your root password is emailed to the registered email address

Once it is created ssh directly on to the box as root, for me it was as simple as 

 ssh root@192.241.174.17

Now following docker’s install guide I ran

apt-get update
apt-get install linux-image-extra-`uname -r`

sh -c "curl http://get.docker.io/gpg | apt-key add -"

sh -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"

apt-get update
apt-get install lxc-docker

Which allowed me to run docker for the first time 

root@blog:~# docker version
Client version: 0.6.1
Server version: 0.6.1
Git commit: 5105263
Go version: go1.1.2
Last stable version: 0.6.1

Now to run something exciting, lets run redis

docker run -d johncosta/redis 

Install redis-cli on the host machine

apt-get install redis-server

redis-cli -h 192.241.174.17 -p 6379

Or connect from your macbook

brew install redis

redis-cli -h 192.241.174.17 -p 6379

Now time to play with redis

redis localhost:6379> set docker magic
OK
redis localhost:6379> get docker
"magic" 

5 minutes must be up by now!

Grails create link to homepage

After reading the Zeroturnaround’s bake off of modern jvm web framework I thought I would give Grails one last go. After all, in the report it beat the likes of Wicket, Spring, Play, etc

Intellij 12 made quick work of setting up a project and soon I had a functioning website. All good so far, I remembered my basic Rails/Grails knowledge from before and have to admit I was able to do a lot in a very short period of time. 

However one of the simplest links stopped me in my tracks and it took a disproportionate about of googling to figure it out. 

How to link to the homepage/root?

After much searching the best I got was

 <a href=”${createLink(uri: ‘/’)}”>home</a>

Easy once you see it!! Hopefully I will save you some the time too

Java Collections API Enhancements: Thanks to Closures – Lambda Expressions

Originally posted on Amit Phaltankar:

Thank you for visiting this site.

The blog and the content has been moved to a new location and will be maintained from there.

The new address is : www.amitph.com

The same post can be read at

Java Collections API Enhancements: Thanks to Closures – Lambda Expressions 

You are requested to visit the blog at new address and we will continue from there.

Thanks!

View original

Open Source Application Monitoring: Catching Exceptions

Imagine if you will, you are working on a new critical application and you need to write the output of your process calculations to a file. Simple stuff, a few lines of Java later you have

File file = new File("myfile.txt");
try {
  boolean fileCreated = file.createNewFile();
  log.debug("fileCreated = " + fileCreated);
} catch (IOException e) {
  log.error("Could not create file",e);
}

You even remembered to log the exception to the logs just in case there was a problem in production. A few weeks later the code ships and works perfectly for weeks until one day the network mount disappears and the application starts to throw exceptions.

Your applications logs then fill up with the exception message and stacktrace but no one realizes there is an issue until an angry customer rings up complaining that they never received their report.

A far worse scenario is that the exception occurs in production but the development staff decide that it is a “good exception” and that the best course of action is to ignore it. Forever! Well until the new guy starts and they have to explain that it is a “good exception”, and so are the following 600 exceptions.

I remember when I first heard the term “Good Exception”, I was working for startup in London over ten years ago. I was new to the company and the first phase of the application was already in production as part of a critical beta phase of the product. Each morning a developer would have to be in the office ready to deal with any issues that may arise from 6am.

One cold December morning I was in the office and as part of the morning grind I was going through a checkout of the application. Checkpoint number 27 was “Check application logs”. No more detail, so I jumped on to the application server and started to tail the logs and to my dismay hundreds of exceptions were being logged in realtime.

I spent the next hour trying to work out what was wrong with the application and what had changed to cause such an exception storm in production. At around 8:00am one of the developers who had the longest tenure in the team arrived in and calmly pointed out “oh those, those are good exceptions, you can ignore them. They occur the day after a billing cycle due to a bug in one the core components”.

Key lesson; Exceptions should be exceptional, if you get an exception in production you need to deal with it.

Exceptional Workflow

Exceptions are part of both the development process and the application monitoring process. An ideal flow is that once an alert is generated in production it is fed back into the development process as a potential bug fix or improvement. The key is to provide adequate monitoring of exceptions in production and to provide sufficient feedback into the development team.

How many of the applications you have worked on have had anything more than log level or log scrapping exception monitoring?

How many development processes have you seen that link production exception to bug fix and strive to fix as many exceptions as possible?

How many “good exceptions” were written to your logs in production since you started reading this post?

Baking Exception Monitoring

Personally I think one of the reasons for poor infrastructure in critical areas like this is down to the way different parts of the organizations are structured. In many large teams people are dedicated to different function of the application lifecycle. Developers are generally focused on the application business requirements and have unforgiving deadlines. Support teams have deadlines of a different type. They also tend to support many applications across a range of functions.

With the advancement of the DevOps movement these communities are starting to join forces and work on the infrastructure behind the applications. So one problem is certainly being addressed and will start to become more and more widespread in the next 2-3 years The other major factor is tool support. How many good modern tools are available for application monitoring that are quick to use and onboard? There are a number of interesting commercial startups in this space at the moment, AirBrake for example is used by a number of corporation to add monitoring support to their application.

Airbrake offers rich functionality but also supports almost all popular languages in it’s API arsenal. However it is hosted on their servers and this deployment configuration will not suit a large majority of application developers who build bespoke software for internal clients and are forbidden to publish information external regardless of content. Interestingly enough there is an open source alternative to AirBrake called ErrBit which is compatible with the AirBrake API.

It’s a ruby on rails application that can be easily installed on your local server or for the purpose of this blog I put it up on Heroku mainly for ease of use. Once you have installed ErrBit you can quickly post exceptions and stacktraces to the server and it has some basic workflow for your support staff to monitor and deal with the exception. It also has integration with some of the most popular Issue Trackers however there is currently no Jira support.

Installing ErrBit

This was the first time I used Heroku for anything even though I had heard great things. I had an account but it was unverified something that I over looked when I did my first installation. ErrBit needs MongoDB and to use MongoDB with Heroku you need to verify your account with a credit card. This surprisingly stop my application working for a while and it took me ages to notice the small error message in the install script. You have been warned!

To install the application you need to follow the simple steps from the github page https://github.com/errbit/errbit (you will need git and ruby installed locally)

Clone the repository

git clone http://github.com/errbit/errbit.git

Create & configure for Heroku

gem install heroku
heroku create example-errbit --stack cedar
heroku addons:add mongolab:starter
cp -f config/mongoid.mongolab.yml config/mongoid.yml
git add -f config/mongoid.yml
git commit -m "Added mongoid config for Mongolab"
heroku addons:add sendgrid:starter
heroku config:add HEROKU=true
heroku config:add ERRBIT_HOST=some-hostname.example.com
heroku config:add ERRBIT_EMAIL_FROM=example@example.com
git push heroku master

Seed the DB

heroku run rake db:seed

Pretty quick, well once you have a validated Heroku account. Once completed simply type

heroku open

And your new ErrBit install should be running. My instance is at ebit.herokuapp.com and you can use anyone@anyone.com/password to login

 

 

Once you have installed ErrBit you will need to configure your users and whatever applications you plan to monitor. Again straightforward, clicking “Add a new app” button will bring you to configuration screen And once you create the application record you will get the important application id You will need this later when publishing exceptions

Publishing Exceptions from Java

As I mentioned earlier ErrBit is compatible with all the language APIs that AirBrake provide and luckily for me there is an actively developed API for Java available at http://github.com/airbrake/airbrake-java. This will allow you to send Exceptions from you Java Server Appications, Mobile Applications and Desktop Clients. To start using it with maven add the following dependencies to your pom file

<project>
    <dependencies>
        <dependency>
        <groupId>io.airbrake</groupId>
        <artifactId>airbrake-java</artifactId>
        <version>2.2.0</version>
        </dependency>
    </dependencies>
</project>

Once I imported the libraries I saw a slight problem in how to override the url for communicating with the backend server. In the AirBrakeNotifier class, which is responsible for calling the server side rest api, the URL for AirBrake is hardcoded whereas I needed to override it for ErrBit. A quick solution was to create a new ErrBitNotifier class which takes the base url a construction argument.

import airbrake.AirbrakeNotice;
import airbrake.NoticeXml;

import java.io.*;
import java.net.*;

public class ErrBitNotifier {

    private final String baseUrl;

    public ErrBitNotifier(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    private void addingProperties(final HttpURLConnection connection) throws ProtocolException {
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-type", "text/xml");
        connection.setRequestProperty("Accept", "text/xml, application/xml");
        connection.setRequestMethod("POST");
    }

    private HttpURLConnection createConnection() throws IOException {
        return (HttpURLConnection) new URL(String.format("http://%s/notifier_api/v2/notices", baseUrl)).openConnection();
    }

    private void err(final AirbrakeNotice notice, final Exception e) {
        e.printStackTrace();
    }

    public int notify(final AirbrakeNotice notice) {
        try {
            final HttpURLConnection toairbrake = createConnection();
            addingProperties(toairbrake);
            String toPost = new NoticeXml(notice).toString();
            return send(toPost, toairbrake);
        } catch (final Exception e) {
            err(notice, e);
        }
        return 0;
    }

    private int send(final String yaml, final HttpURLConnection connection) throws IOException {
        int statusCode;
        final OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(yaml);
        writer.close();
        statusCode = connection.getResponseCode();
        return statusCode;
    }

}

Perhaps the AirBrake API could potentially allow for custom configuration of the URL in the next revision. Once you have created a new ErrBitNotifier you can start publishing exceptions. Going back to our previous example

import airbrake.AirbrakeNotice;
import airbrake.AirbrakeNoticeBuilder;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;

public class TestException {

    private static org.apache.log4j.Logger log = Logger
            .getLogger(TestException.class);

    public static void main(String[] args) {

        File file = new File("h://myfile.txt");
        try {
            boolean fileCreated = file.createNewFile();
            System.out.println("fileCreated = " + fileCreated);
        } catch (IOException e) {
            log.error("Could not create file",e);
            AirbrakeNotice notice = new AirbrakeNoticeBuilder("b4f7cb2020b2972bde2f21788105d645", e, "prod").newNotice();
            ErrBitNotifier notifier = new ErrBitNotifier("ebit.herokuapp.com");
            notifier.notify(notice);
        }

    }
}

This code will throw an IOException (well at least on my computer, since I don’t have a h drive!) and the exception will be seen on the ErrBit console It has the ability to spot duplication of exceptions and you can set it up to email you when the exception is generated.

Also the AirBrake API has log4j appender support but it is tied to the AirBrake public URL and I have left it out of the post. However it can be turned on by the following log4j configuration example

log4j.rootLogger=INFO, stdout, airbrake

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d,%p] [%c{1}.%M:%L] %m%n

log4j.appender.airbrake=airbrake.AirbrakeAppender
log4j.appender.airbrake.api_key=YOUR_AIRBRAKE_API_KEY
#log4j.appender.airbrake.env=development
#log4j.appender.airbrake.env=production
log4j.appender.airbrake.env=test
log4j.appender.airbrake.enabled=true

Conclusion

Application Exception Monitoring is an important part of your application lifecycle.

Exceptions should be easily visible to the support and development teams and your development process should look to address all exceptions in forthcoming sprints.

Exceptions should be used for exceptional cases only, any exception that is not acted upon in production is noise and creates confusion.

Tool Support is important in this area and ErrBit looks like a great multi-language tool that can help support your Exception Management workflow.

How Spring 3.1 Environments & Profiles will make your life better!

My goal of writing one technical blog post per week fell to the wayside around December mainly due to work related project time constraints. I have a four-day weekend and a sparkling new home office, which has to be used for something other than surfing hacker news. Its noon here and I have two hours to produce this blog post so here I go!

The software I write or design generally needs to be deployed in different configurations. These deployment configuration end points can be generalised into the following buckets

  1. Java Enterprise Containers (jboss, weblogic, tomcat, glassfish, etc)
  2. Standalone Java application
  3. GUI Applications
  4. Testing Frameworks

Ignoring GUI Applications for the moment (I might return to these later) the code is often the same between container, standalone and testing. This leads to a key design consideration or philosophy when designing and coding this “type” of software. The code I write needs to run perfectly and untainted in each scenario.

That’s crucial to quality and robustness! The problem is that there are environmentally aware resources that are configured depending on where the code is executed. When I am writing a unit test I will not (I know I could, but I think you are missing the point) have my datasource bound to a JNDI tree. Where as in the container I simple lookup the tree and ask can I have a datasource please.

And frameworks like Spring encourage this form of development, or at least have popularized patterns such as inversion of control. Instead of the executing code configuring the database or queue it is injected as run time and life is good again.

So a blog post to reiterate the same warbling around inversion of control? Not quite, at this point I have a piece of code

public class BusinessClazz implements SomethingReallyImportant {
    private DataSource dataSource;
    @Required
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
}

The datasource is injected and the BusinessClazz is none the wiser about the origination of the datasource. Hey, I’m not the smartest guy in the world, but I’m certainly not the dumbest. I mean, I’ve read books like “J2ee Development Without Ejb” and “Expert one-on-one J2EE design and development”, and I think I’ve understood them. They’re about girls, right? Just Kidding. I create a datasource in spring and inject it

<bean name="myBusinessClazz" class="BusinessClazz">
   <property name="dataSource" ref="dataSource"/>
</bean>

My business service now is agnostic to the datasource origin and can now happily run in any of my deployment end points. But what about the datasource, how do we configure this to run anywhere? We are focusing on the datasource but this example can be applied to any component that changes between environments or runtimes.

A datasource typically requires two pieces of configuration. The first part of the puzzle is where is the database and how should I connect to it. I need hostnames, port numbers, service names, etc.

The second piece of configuration is how is it represented. Here we have a few options. I could create a simple one single connection to the database

 <bean id="dataSource" destroy-method="close" 
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:hsql://localhost"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
 </bean>

Or I could use apache pooling to create a pool of connections

<bean id="dataSource" 
   class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
  <property name="url" value="jdbc:myserver"/>
  <property name="username" value="username"/>
  <property name="password" value="password"/>
  <property name="initialSize" value="2"/>
  <property name="maxActive" value="5"/>
  <property name="maxIdle" value="2"/>
</bean>

Or the container manages the connection and we look it up

<jee:jndi-lookup id="dataSource" jndi-name="java:mydatasource"/>

There are many other ways of configuring the datasource but this is enough for illustration purposes. Spring’s PropertyPlaceholderConfigurer class does a great job at handling properties that change between environments.

The problem is how do we seamlessly go between deployment types and pickup different styles of datasources?

Roll your own

This is not an old problem and has been solved in many different ways by many development teams over the last six or seven years. Each team would look at the problem and create a custom way of adding enough intelligence to the code to help alleviate the problem.

One of the biggest drivers behind making code run cleanly inside and outside of the container is to allow developers write unit tests that test true production code.

A typically example of a solution to this problem is to define datasources in a separate context files and create a naming convention to bring order to the chaos. For example if we had three modes of operation we would have three separate context files.

datasources-containerContext.xml
datasources-pooledConnection.xml
datasources-singleConnection.xml

Next comes custom infrastructure code that has to be used at every point of spring initialization throughout your application. It would leverage the ability to use ant style wildcard searches for context files across the deployed classpath.

ClassPathXmlApplicationContext containerContext = 
   new ClassPathXmlApplicationContext("**/**-containerContext.xml");
ClassPathXmlApplicationContext nonContainerContext = 
   new ClassPathXmlApplicationContext("**/**-pooledContext.xml");
ClassPathXmlApplicationContext testingContextContext = 
   new ClassPathXmlApplicationContext("**/**-singleContext.xml");

In a few hours you can have a system that is tolerant of each runtime and the code is engineered to take advantage of this. This roll your own approach has worked for several years but has the major disadvantage that each team and project has a different way of tackling the problem.

Enter Spring Profiles

As of Spring 3.1 there is a solution to this problem (If you do not have an upgrade path for the libraries your project depends on then you have a much larger problem!)

Spring has introduced the notion of Environments and Profiles across the container. Each application context has an Environment object which can be accessed easily

 ClassPathXmlApplicationContext classPathXmlApplicationContext = 
    new ClassPathXmlApplicationContext();
 ConfigurableEnvironment configurableEnvironment = 
    classPathXmlApplicationContext.getEnvironment();

Each environment can have a number of active profiles. Most Spring profile examples talk about profiles being of dev or prod. However I have a Spring solution to the environment type issues my problem is the different profiles for multiple runtimes. But this is the advantages of the implementation you are free to decide how you use it.

By default your beans have no profile and are loaded into the container. So lets start with example. Imagine if this is my current single connection datasource context

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="…">
 <bean id="dataSource" destroy-method="close" 
   class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
   <property name="url" value="jdbc:hsqldb:hsql://localhost"/>
   <property name="username" value="sa"/>
   <property name="password" value=""/>
 </bean>
</beans>

As of Spring 3.0 there is a new xml application context class called GenericXmlApplicationContext which is an alternative to ClassPathXmlApplicationContext and FileSystemXmlApplicationContext.

The advantage of using GenericXmlApplicationContext is that it can be configured completely with setters rather than a single clunky constructor. Just remember to call refresh() once ready to instantiated the container.

Armed with GenericXmlApplicationContext we initialize the container with the following code snippet

GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
 ctx.getEnvironment().setActiveProfiles("standalone");
 ctx.load("*Context.xml");
 ctx.refresh();

Here I set the active profile to standalone. By convention in my project I will consider any code running outside the application container as “standalone” and anything inside the container as “container”. I can set multiple profiles here, for example I could have the following for setting it to standalone and activemq rather than MQSeries

ctx.getEnvironment().setActiveProfiles("standalone", "activemq");

Setting active profiles will have no effect on the current configuration context since I haven’t set a profile on the beans. So we change our configuration context to

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="…" profile="standalone">
  <bean id="dataSource" destroy-method="close" 
   class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
   <property name="url" value="jdbc:hsqldb:hsql://localhost"/>
   <property name="username" value="sa"/>
   <property name="password" value=""/>
 </bean>
</beans>

These beans will now only be initialized if the active profile is set to “standalone”. Profiles are an attribute of beans rather than bean therefore you can not set individual beans to select profiles. In the older versions of Spring this would still leave the problem with multiple files and ant wildcards to select the correct context at runtime.

Spring 3.1 has introduced the ability to nest <beans/> within <beans/>. With a quick refactor we can now have a single datasource context file such as

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="…">
 <beans profile="standalone">  
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
 <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> 
 <property name="url" value="jdbc:hsqldb:hsql://localhost"/> 
 <property name="username" value="sa"/> 
 <property name="password" value=""/> 
 </bean> 
 </beans> 

 <beans profile="container">
 <jee:jndi-lookup id="dataSource" jndi-name="java:mydatasource"/>
 </beans>
</beans>

And I can quickly change profile to container by

ctx.getEnvironment().setActiveProfiles("container");

How to change profiles

As shown above you can change profiles programmatically by coding something like

ctx.getEnvironment().setActiveProfiles("container");

Another way to change the profile is to pass a system parameter at run time

-Dspring.profiles.active="standalone"

But you can also set it as part of the init parameter of your ear/war

<servlet>
 <servlet-name>dispatcher</servlet-name>
 <servlet-class>
   org.springframework.web.servlet.DispatcherServlet
 </servlet-class>
 <init-param>
   <param-name>spring.profiles.active</param-name>
   <param-value>production</param-value>
 </init-param>
</servlet>

What it means for you?

Hopefully this gives an insight into a new and powerful feature of Spring 3.1 and a feature that has been sorely missed over the 3 major revisions of Spring.

My first Java 8 Lambda Expression

My first Java Lambda Expression

Keeping up with the pace of a blog post a week I have wanted to take a closer look at Java 8 developer preview ever since it was release mid-November. Originally due to be in Java 7 but was dropped as part of the “Plan B” effort to help ship Java 7 earlier. Java 7 has now shipped and the focus is on the Java 8 which is due to be release late 2012. The syntax was finally confirmed after much debate by Brian Goetz on the lambda-dev mailing list and will adopt the C# syntax.  For more information on Lambda expressions in Java 8 check out the JSR-335

Wait, let’s start at the start. What are lambda expressions and why oh why do we need them so badly that people fall out over the syntax.

What is a lambda expression?

Put simply a lambda expression is

   Lambda is an operator used to denote anonymous functions

I just robbed that from Wikipedia, I surely can do better. In my words a lambda expression is

   A succinct way to represent an anonymous method

My worst attempt at defining lambda expression was

   Anonymous classes, but much cooler!!!

Many languages already support lambda expressions; I just noticed the Visual Basic has had support for a while now. To see our first lambda expression in action we are going to use Erlang as language of choice. I choose Erlang as I think its syntax is probably the cleanest around and can be understood by almost any developer.

F = fun(X) ->  X * X end,
F(5).

Easy peasy? Now time to confuse you all again here is a Haskell example

numLongChains :: Int  
numLongChains = length (filter (\xs -> length xs > 15) (map chain [1..100]))

If you feel modern life as a programmer is too easy and feel you want to re-write your java web application in Haskell then you should continue reading Learn You a Haskell where the above example is borrowed. I love their explanation of the lambda syntax

“Lambdas are basically anonymous functions that are used because we need some functions only once. Normally, we make a lambda with the sole purpose of passing it to a higher-order function. To make a lambda, we write a \ (because it kind of looks like the greek letter lambda if you squint hard enough) and then we write the parameters, separated by spaces. After that comes a -> and then the function body. We usually surround them by parentheses, because otherwise they extend all the way to the right.”

C#, but I only like Java

I heard an expression recently “The determined Real Programmer can write fortran programs in any language” which brings me nicely to writing our first lambda expression in C#

Since the Java syntax is based on the C# syntax it is probably a good idea to visit the extraordinary world so few Java developers venture, .net land. In general, the syntax is

(Input Parameters) => Expression

This equates to

x => x + 1

Sometimes it is impossible for the compiler to infer the input type so you have to help it out by specifying the input types as follows

(int x, string s) => s.Length > x

These are technically Expression Lambdas, a Statement Lambdas is very similar but with braces

delegate void TestDelegate(string s);
TestDelegate myDel = n => { string s = n + " " + "World"; Console.WriteLine(s); };
myDel("Hello");

Java Syntax,  Java Syntax,  Java Syntax

There has been a lot of debate on the exact specification of Java’s lambda expression syntax and I am sure it’s not over. Brian Gotez outlined the thoughts behind the decision in his email

Despite extensive searching, there was no clear winner among the alternatives (each form had some good aspects and some really not very good aspects, and there was no form that was clearly better than the others). So, we felt that it was better to choose something that has already been shown to work well in the two languages that are most like Java – C# and Scala – rather than to invent something new.

He defined the C# syntax as

lambda = ArgList Arrow Body
ArgList = Identifier
             | "(" Identifier [ "," Identifier ]* ")"
             | "(" Type Identifier [ "," Type Identifier ]* ")"
Body = Expression
             | "{" [ Statement ";" ]+ "}"

And illustrated the definition with the following examples

x => x + 1
(x) => x + 1
(int x) => x + 1
(int x, int y) => x + y
(x, y) => x + y
(x, y) => { System.out.printf("%d + %d = %d%n", x, y, x+y); }
() => { System.out.println("I am a Runnable"); }

My First Java 8 Lambda

It’s getting late, I have downloaded the Java 8 JDK preview, written three pages of fluff on Lambda expressions but have yet to write one. So here goes, you can download Java 8 from Oracle at http://jdk8.java.net/lambda/.

I had a brief look but no IDE or early beta version of an IDE supports Java 8’s Lambda Expressions so I will have to code this one on the command line. My first working lambda expression in Java 8 is a small illustration

public class FirstLambda {

    enum SoccerResult{
        WON, LOST, DRAW
    }

    interface SoccerService {
        SoccerResult getSoccerResult(Integer teamA, Integer teamB);
    }

    public void soccerResultPredictor() {

        SoccerService soccerService = (teamA, teamB) -> {
            SoccerResult result = null;
            if (teamA == teamB){
                result = SoccerResult.DRAW;
            }else if(teamA < teamB){
                result = SoccerResult.LOST;
            }else{
                result = SoccerResult.WON;
            }
            return result;
        };

        // man utd vs wolves
        SoccerResult soccerResult = soccerService.getSoccerResult(3,1);
        System.out.println("soccerResult = " + soccerResult);

    }

    public static void main(String... args) {
        FirstLambda firstLambda= new FirstLambda();
        firstLambda.soccerResultPredictor();

    }
}

In this simple example we create a lambda expression to work out the result of the team a’s match. The lambda expression itself is represented by

        SoccerService soccerService = (teamA, teamB) -> {
            SoccerResult result = null;
            if (teamA == teamB){
                result = SoccerResult.DRAW;
            }else if(teamA < teamB){
                result = SoccerResult.LOST;
            }else{
                result = SoccerResult.WON;
            }
            return result;
        };

Conclusion

I have barely scratched the surface of Lamdba Expressions in this post but as the spec matures and tooling support improves I hope to start to learn more about the more complicated inner working of this language feature.

Update!

I really recommend you to rea Brian’s Goetz’s update on the lambda syntax in java! I wish I had this over the weekend

http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html

London Java Community Open Conference 2011

Yesterday I was explaining my trend for blogging to an ex-colleague and he felt that to make any kind of impact in the tech space blogging sphere I would need to blog a thousand words a day! I don’t think I will ever come close to that, however I will try to blog at least one technical article a week until the end of the year. That’s achievable right? That is only 5 weeks away, I am sure I have 5 blog posts in me.

Back to the original point of this post, I was at the London Java Community Open Conference where I met many ex-colleagues including my friend who thinks a thousand words is a realistic target.  The conference is a yearly collection of presentations loosely linked to the daily life of a java developer. It was held at IBM’s offices on the Southbank which allowed for some great views of the Thames as we sat and listened to the eclectic mix of presenters.

I woke up late with a slightly sore head and was only able to catch the second half of the keynote speech by Ben Evans and Martijn Verburg on their insights and predictions of the Java Ecosystem and community. They interestingly made the comment that Swing was dead and Oracle was moving it into maintenance mode to be replaced by JavaFX. They didn’t give a timeline on this but I think, like ‘roaches, it will be hard to kill Swing off. It’s interesting as I always didn’t any credence to JavaFX, I had heard it was a competitor to Flash-Flex and immediately had dismissed its importance. But the guys were saying that Oracle have dropped the scripting concept and have refocused the team. (ahh that will be a blog post, a frustrated web developer’s trials and tribulations of JavaFX development)

I also learned the London Java Community had a place on the JCP. This is an amazing testament to the strength of the community in London. I remember when I first moved to London and did a quick search of technical user groups and only found the Java Special Interest Group which didn’t seem extremely active. Now there are so many well supported and exciting developer groups throughout the city and throughout the different programming languages and frameworks. Read more about their amazing election win at http://londonjavacommunity.wordpress.com/2011/05/12/the-ljc-and-the-wider-java-community-the-jcp-election-win/

As part of the JCP process LJC get to vote on ever JSR, I can only contemplate the sheer volume of paper work this produces, to encourage wider community participation they have introduced “Adopt a JSR” programme. I think the title speaks for itself, pick a JSR and get involved. I think we all these things it requires a considerable amount of effort and dedication. A quick look on the LJC website and at present they seem to be looking for help with the following JSRs

These are key JSRs for both the JSE and JEE standards and it a great way to get involved in shaping the future of both specs. If you want to find out more go to http://londonjavacommunity.wordpress.com/2011/09/29/adopt-a-jsr/

“The really frakkin simple guide to clojure” by John Stevenson

Next up on the conference schedule was a quick introduction to Clojure by John Stevenson who happens to be the Atlassian Ambassador for the UK.

I go through a functional programming phase every 3-4 months, I pick the functional language of the day, buy a book, hack around, give up complaining that real people don’t need this and go back to procedural programming object oriented programming. I had heard of Clojure but never mustered enough interest to check it out. I am still trying to improve at Scala and really do not have the capacity to learn two pseudo-functional languages at the same time.

Clojure is a dialect of Lisp, interestingly Lisp was the second programming language I learned when I was a kid. The reason is that one of the applications that I had access to was AutoCad which had a dialect of Lisp called AutoLisp. I am not sure if the AutoLisp is around anymore but it was perfect for extending AutoCad’s primitive functions.

Back to the presentation, it was a real fly through the language and its constructs. John gave a good feel for the language and highlights the strengths of the language in a mutli-core world. Well highlighted the strength of functional languages. Enough of an introduction to bait my interest but not enough to ditch Scala right now as my “language to master”

Haskell – Emily Green

If I knew little about Clojure I knew even less about Haskell! Emily Green works for a company called Scrive who build their serverside applications in Haskell. She was a java developer in a previous life but now has been programming Haskell for the last nine months. An engaging presenter, one of the best I saw at the conference. The interesting thing is that for the majority of this talk she gave no really insight into the language itself. I was hoping for a 40mins introduction to Haskell and to live with some idea about the language. However it wasn’t until the very end before there was even the slightest hint of code.

Nonetheless the talk was very interesting and was centred on her life with Haskell. She gave a very brief tour of her likes and dislikes with the language and the community. I will not do the talk and justice by trying to list out all the points but the two that stuck in my mind were make the complier work more. Her example was that instead of writing unit tests to ensure that all code is tested, write the logic into the code and get the complier to ensure correctness at compile time. Emily noted that when working with Haskell that the complier will refuse to compile if the code is not just syntactically correct but functionally too. The other point I remember from yesterday is that she mentioned that most of the content produced around Haskell are academic papers as the language is rooted in research. Also she mentioned something about monads ;)

The Future of Java – Steve Elliott

Steve Elliott from Oracle present on the future of Java. Starting with going through some of the hilightls of the recently released version Java 7 he moved on to focus on version 8 & 9. Java 8 is expected sometime in the Summer of 2013 and will introduce some ground breaking changes. Language-level support for lambda expressions is due for this version and is hotly debated throughout the Java community. Recently a build of Java 8 with the first true preview of lambda expressions was released and syntax will be (copied and pasted from Brian Gotez’s email to the lambda dev mailing list on the final decision http://mail.openjdk.java.net/pipermail/lambda-dev/2011-September/003936.html)


x => x + 1

(x) => x + 1

(int x) => x + 1

(int x, int y) => x + y

(x, y) => x + y

(x, y) => { System.out.printf("%d + %d = %d%n", x, y, x+y); }

() => { System.out.println("I am a Runnable"); }

I need to spend some quality time with the latest complier but this is pretty much the C# syntax so for .net developers or scala developers for that matter it should be relatively straightforward to pick up.

Another key core component predicted to be released in 8 is project jigsaw or more commonly know as java modularization. Steve reiterated that this was not just adding Ivy or Osgi to the top of java but go the root core of the platform and add modularization to the java platform itself. Java has evolved over the years and the component entanglement is major problem. The task of unpicking the component and stitching them back together is currently underway.

Steve referenced Mark Reinhold’s requirement documents on the java module system as a good place to start to understand the true mammoth of the task. You can check it out at http://openjdk.java.net/projects/jigsaw/doc/draft-java-module-system-requirements-12#_1