Eddú Meléndez

News About Java 8

Java 8 was released in March and many cool features have arrived in a functional programming style.

Lambdas

Lambdas has been used in other programming languages. In java, it’s the new way to call anonymous classes.

If you have been Swing developer, you may have seen lot of this code. Example of anonymous class:

1
2
3
4
5
6
File[] directories = new File(System.getProperty("user.home")).listFiles(new FileFilter() {
           @Override
           public boolean accept(File pathname) {
               return pathname.isDirectory();
           }
       });

Below, you can see three approaches to use lambdas:

1
2
3
4
5
6
7
8
9
10
() -> System.out.println("Hello lamdba");

//pass an object
customer -> customer.getLastname();

//using blocks
() -> {
  String greeting = "Hello lambda block";
  System.out.println(greeting);
}

Now, let write our first example using lambdas:

1
2
File[] directories = new File(System.getProperty("user.home"))
                .listFiles((File pathname) -> pathname.isDirectory());

In this example, to move from anonymous class to lambdas there are two steps to do. As you can see the difference is inside of listFiles method. The first part of the lambda is the parameter of the method accept (File pathname) next the arrow (->) and finally the return statement (pathname.isDirectory()). If you want to add extra functionality inside of the lambda you can use block statements.

Streams API

The power to manage collections with readable code is here.

I will work in this example with my favourite books. So, we will print in the console only books that contains “o”

Old fashionable way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
List<Book> books = new ArrayList<>();
books.add(BookBuilder.defaultValues().withId(1l).withTitle("Angel & Demons").withIsbn("0-671-02735-2").build());

books.add(BookBuilder.defaultValues().withId(2l).withTitle("The Da Vinci Code").withIsbn("0-385-50420-9").build());

books.add(BookBuilder.defaultValues().withId(3l).withTitle("The Lost Symbol").withIsbn("978-0-385-50422-5").build());

books.add(BookBuilder.defaultValues().withId(4l).withTitle("Inferno").withIsbn("978-0-385-53785-8").build());

for(Book book : books){
  if (book.getTitle().contains("o")) {
    System.out.println(book.getTitle());
  }
}

Now we will use Stream API to rewrite the last part:

1
2
3
4
List<String> bookNames = books.stream()
.filter(book -> book.getTitle().contains("o"))
.map(Book::getTitle).collect(Collectors.toList());
System.out.println(bookNames);

Both are doing the same thing but stream offers another great cool features like parallelStream or group function. Additionally, it is readable code and the ciclomatic complexity is reduced.

There are eager and lazy methods inside of Streams API.

  • Eager, return some value or void. Ex: foreach, collect.
  • Lazy, return another stream. Ex: filter, map.

Note: Streams can be read just once. If you try to work with the same stream twice you will se an exception.

For more infomation about Streams visit the official documentation.

Default Methods

In java 8, there are big changes like this. Do you remember interfaces? The place where you define what to do and the class implementation define how.

Default methods has been used in the Java 8 API in order to add new functionality without break existing code. Take a look inside Collection.java you will se that stream() method is a default method.

If you have take a look inside of the API you can see that default method has body or define an action.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public interface Greeting {

  public default void welcome() {
    System.out.println("Hi, I am a default method.")
  }

}

class SpanishGreeting implements Greeting {

  @Override
  public void welcome() {
    System.out.println("Hola, soy un método por defecto.")
  }

}

class EnglishGreeting implements Greeting {

}

If we call welcome method from SpanishGreeting “Hola, soy un método por defecto.” will be printed but if you call the method from EnglishGreeting “Hi, I am a default method.” will be printed.

Method References

Everything can be well used or not, in the case of lambdas does nothing, just call the method you can use method references as a easy-to-read approach.

1
Customer::getFirstname //instead  customer -> customer.getFirstname()

Introduction to Gradle

Gradle is called the next generation of build automation and dependency management. If you have used Ant or Maven you know what that mean.

Gradle is based on Groovy and has compatibility with existing tools like Ant and Maven.

The company behind this project is Gradleware.

Build Automation

As you know, manual tasks are error-prone and tedious. Take in mind that you are working in a project and you have to compile your code… manually!!! It’s boring and you are spending time if you are doing this, Can you imaging running javac command for all your project and dependencies? and after that you need to package the artifact in a jar or war. Ant is the first tool which help developers to write a xml file with all the step to compile, copy, package artifacts. Currently, Maven have standardized all of this and it is charge of to run all the previous tasks and manage the dependencies.

Dependency Management

All the projects have dependencies with other projects, years ago and even now is a headache when you are copying and pasting jars inside your projects. To avoid this pain maven and ivy are tools to manage this.

Getting Started

It is really cool to see what amazing things you can do with Gradle. It allow us to work with java, groovy and scala projects too.

I really like the easy way to add tasks and manage the dependencies between them. Another good thing is the capability to extend the functionality in Java or Groovy.

Gradle have the power run tasks in a new way: Gradle task name abbreviation. For example, if you have created a task called deployInLocalhost… run gradle dIL instead ofgradle deployInLocalhost but make sure you have not another task with the same abbreviation.

Gradle Wrapper

I remember when I started using maven, I had downloaded some examples but that was not enough. I had to install maven in my local machine. To avoid install new runtime manually like I did, Gradle Wrapper has arrived to the rescue!!!

As a best practice, it is a good idea to have a wrapper in your project in order to avoid compatibility troubles or any other issues. To apply the wrapper configuration you just need to execute this command gradle wrapper and changes to your project will be applied, your wrapper will have the same version as your gradle runtime but you can change it.

Gradle Daemon

Gradle take time to up and run when task is executed. If you want to improve the performance, Gradle daemons is here to the rescue… to active it gradle –daemon and finalize the process gradle –stop. This is useful when you run unit test many times.

Note: expire after 3 hours.

Moving from Maven to Gradle

Currently, you may are working with maven so if you want to try gradle without lost all your maven configuration you can run this command gradle maven2gradle. It will create the build.gradle and settings.gradle files with all the dependencies and configuration. Looks great, right? but as a new build automation tool, gradle is growing so if you have additional plugin configuration in your pom.xml this will not be migrated. Your project can live with both.

Gradle Commands

I will describe some useful commands but to get more information visit online documentation.

All those commands start with gradle: gradle <command>

Commands:

-v or –version -> display gradle version.

tasks -> display tasks related to the project.

-q -> show tasks output.

-x or gradle –exclude-task -> exclude the execution of tasks defined.

dependencies -> display project dependencies and how dependencies were solved.

projects -> list all the project and subprojects.

properties -> list all available properties in the project.

-a or –no-rebuild -> it’s used when you won’t to rebuild a projects that you didn’t change. It works if you are only changing files in a single project.

–refresh-dependencies -> refresh project dependencies.

-b or –build-file -> give the option to choose another build script.

-Dtest.debug test -> debug test in port 5005

Recommended Books

JavaOne 2014

This year I had the opportunity to attend at JavaOne 2014 in San Francisco, California. It has been a really good experience to me.

In the JavaHub, I could take a look about:

  • Mission Control.
  • 3D Modeling and Printing, using Java FX.
  • New features in Netbeans, which allow us to migrate from older java versions to java 8 and use Streams.
  • Internet of Things, using Java Embedded and Raspberry Pi.
  • Hackergarten, the place where you can contribute in open source projects talking with the experts.

Some companies were present in this event such as: Cloudbees, GitHub, Gradleware, Hazelcast, JetBrains, JFrog, Liferay, O'Reilly, Pivotal, RedHat, Sonatype, Vaadin, Zeroturnaround.

Also, had the chance to meet:

  • Anton Arhipov - @antonarhipov, JRebel and XRebel Product Manager at Zeroturnaround.
  • Arun Gupta - @arungupta, Director of Developer Advocacy at Red Hat and Author of Java EE 7 Essentials.
  • Benjamin Muschko - @bmuschko, Principal Engineer at Gradleware and Author of Gradle in Action.
  • John Ferguson - @wakaleo, Author of Java Power Tools, Jenkins: The Definitive Guide and BDD in Action.
  • Josh Long - @starbuxman, Spring Developer Advocate.
  • Phill Weeb - @phillip_webb, Spring Framework committer and co-lead of Spring Boot.

Below, are the sessions I found most interesting:

Sunday

  • Introduction to Java 8: JVM, Language, and Platform.
  • Starting a JUGgernaut: How to Start and Rapidly Grow Your JUG.
  • Java Strategy and Technical Keynotes.

Monday

  • Hadoop for Java Developers. http://goo.gl/f2CwNz
  • Next Step in Automation: Elastic Build Environment.
  • Programming with Streams in Java 8.

Tuesday

  • Devoxx4Kids for Parents.
  • Functional UIs with Java 8 and Vaadin.
  • Building a Continuous Delivery Pipeline with Gradle and Jenkins.

Wednesday

  • Continuous Delivery and Zero Downtime: What Your Architecture Needs to Succeed.
  • Apache TomEE, Java EE Web Profile, and More on Tomcat.
  • REST Assured: Hypermedia APIs with Spring MVC.

Tuesday

  • The Deploy Factory: Open Source Tools for Java Deployment.
  • Run Java Applications with Docker on the Raspberry Pi and Other Platforms.

Java 8 was the main topic during this party, everyone talked about it. As a Venkat Subramaniam said in his talk:

Java arrived later with lambdas but arrived with the desserts (Streams)

MVC 1.0 part of JEE 8 had place at JavaOne… in my opinion looks like Spring MVC but good to know that good things have been adopted.

JavaOne is the best place to meet people, authors, learn new things, get stuff like raspberry pi, books and t-shirts and drink a beer.

Duke and me

Keynote

Duke

Duke Awards

Josh Long and me

RAML: Designing an API

As a developers, every day we are working coding many APIs. But, what about design? Sometimes due to the project schedule, too much requirements and even the lack of knowledge about what is REST; developers forget about the design of API.
Months ago I was surfing on the internet, don’t remember where but I read:

RESTis more than work with xml or json.

And that’s true. So, I researched more about REST and learned lot of things.

What RAML is?

RESTful API Modeling Language allow us to design, describe, define the API. It’s human readable and it is visible to everyone.

That’s pretty cool, we can design the API and document as well.

Security

RAML supports authentication:

  • Basic authentication
  • Digest authentication
  • Oauth 1.0
  • Oauth 2.0

Book Store API

In the example below, I have designed a Books Store API working my favourite book list. The API allow me to Create, Read, Update and Delete books. A simple CRUD operation which is represents by HTTP methods.

OPERATION HTTP METHODS
CREATE POST
READ GET
UPDATE PUT
DELETE DELETE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#%RAML 0.8
---
title: Book Store API
version: 1.0
baseUri: http://localhost:8081/api
/books:
  displayName: Books Catalog
  get:
    responses:
      200:
        body:
          application/json:
            example: |
                {
                  "bookId": 1,
                  "title": "Angel & Demons",
                  "isbn": "0-671-02735-2",
                  "authors": [
                    "Dan Brown"
                  ]
                },
                {
                  "bookId": 2,
                  "title": "The Da Vinci Code",
                  "isbn": "0-385-50420-9",
                  "authors": [
                    "Dan Brown"
                  ]
                },
                {
                  "bookId": 3,
                  "title": "The Lost Symbol",
                  "isbn": "978-0-385-50422-5",
                  "authors": [
                    "Dan Brown"
                  ]
                },
                {
                  "bookId": 4,
                  "title": "Inferno",
                  "isbn": "978-0-385-53785-8",
                  "authors": [
                    "Dan Brown"
                  ]
                }
  post:
    body:
      application/json:
        example: |  
            {
             "title": "Deception Point",
             "isbn": "0-671-02738-7",
             "authors": [
              "Dan Brown"
             ]
            }
    responses:
      201:
        description: Book has been successfully created.
      409:
        description: Book already exists.
  /{bookId}:
    displayName: Book
    uriParameters:
      bookId:
        description: |
          Book Identifier
        required: true
    get:
      description: Retrieve book-related information.
      responses:
        200:
          body:
            application/json:
              example:  |
                {
                  "bookId": 1,
                  "title": "Angel & Demons",
                  "isbn": "0-671-02735-2",
                  "authors": [
                    "Dan Brown"
                  ]  
                }
    put:
      description: Update book information.
      body:
         application/json:
          example: |  
            {
             "title": "Inferno",
             "isbn": "978-0-385-53785-8",
             "authors": [
              "Dan Brown"
             ]
            }
      responses:
        204:
          description: |
            The book has been successfully updated.
        404:
          description: |
            Unable to find book with that identifier.
    delete:
      description: Remove book from the catalog.
      responses:
        204:
          description: |
            The book has been successfully removed.
        404:
          description: |
            Unable to find book with that identifier.

As you can see, the Book Store API have five services:

Learn more about HTTP Status

Platforms and Tools

  • Mulesoft provides an API Platform and APIkit.
  • raml2html, transform raml file to html.
  • jaxrs-to-raml, generate raml file to existing JAX-RS services. It has been updated today, JAX-RS 2.0 Asynchronous Responses is now supported!!!

There are more RAML projects here.

Resources

There are a lot of resources about REST, how to write a good API, some of them are:

Review: Learning Cypher

I have had the change to review another book from Packtpub.com. Now, it’s time to take a look inside the Learning Cypher.

Maybe you have heard about NoSQL (Not only SQL) databases. So, one kind is a Graph Databases and Neo4j is one of them, which is based on Nodes and Relationships.

What is Cypher? It is the Query Language used in Neo4j database.

It has a good introduction about Neo4j using embedded database and using the Java API provided by Neo4j.

For example: Imagine that we would like to get all the Users from the graph database. User must be an Node.

First, create an User node:

1
2
3
CREATE (a:User {username: "jroe", firstname: "Jane", lastname: "Roe"}),
(b:User {username: "cgarcia", firstname: "Carlos", lastname: "Garcia"}),
(c:User {username: "mweng", firstname: "Mei", lastname: "Weng"})

Using the query below you will get all the user information:

1
2
MATCH (u:User)
RETURN u

But, if we want some fields:

1
2
MATCH (u:User)
RETURN u.username, u.firstname, u.lastname

There are another example, using WHERE clause

1
2
3
MATCH (u:User)
WHERE u.username = 'cgarcia'
RETURN u

There are more topics using Regex, Join, Sorting and so on. Another interesting topics are: * Profiling * Migraton from SQL

Note: The author mention to spring-data-neo4j module from springframework which make the configuration and integration easy.

I really recommend this book if you want to dive into Neo4j world.

Jenkins and Liquibase Working Together

I have talked about Database Change Management using Liquibase. Now, I will talk about the integration with Jenkins CI.

As you can see in the example of my previous post. In the pom.xml there are 3 profiles: update,tag and rollback which will be used by Jenkins. The profile approach will be useful in our integration.

I am going to use Cloudbees (PaaS)

1. First, you need to have an Cloudbees account.

2. Click on Databases and then click Add Database.

3. Create a liquibase_demo database.

4. Click on Builds, Jenkins dashboard will be display.

5. Create New Job.

6. Put a name, choose Build a free-style software project option and then click OK.

7. Now, let start to configure Jenkins’s job. You will be able to see the image below

8. Check This build is parameterized option. Then, click Add Parameter. Add the following parameters: driver, url, username, password, changeLogFile, tag and action.

driver, url, username, changeLogFile, tag are String Parameter

password is a Password Parameter.

action is a Choice Parameter with the following options: update, tag and rollback

9. Now, lets configure the Source Code Management. In this example, I have published the demo in GitHub.

10. Now, go to Build section, click on Add build step and choose Invoke top-level Maven targets. Add the maven’s goal and properties. Then, Save.

Maven Goal:

1
mvn install -P$action

Maven Properties:

1
2
3
4
5
6
7
8
liquibase.driver=$driver
liquibase.username=$username
liquibase.password=$password
liquibase.url=$url
liquibase.changeLogFile=$changeLogFile
liquibase.rollbackTag=$tag
liquibase.tag=$tag
liquibase.promptOnNonLocalDatabase=false

If you remember, we have created parameters at the beginning of this post which are reusing in this part using $ in order to do a dynamic job.

11. Finally, click Build with parameters, test your job and enjoy.

Avoid risks and say good bye to manual tasks. For that reason, I love automation!

Note: Liquibase can be used inside the java application using spring integration.

Database Change Management: Liquibase

Months ago, I published my first post about Database Change Management. So now, we will review liquibase.

In this blog, I am using sql scripts but there are another ways to work with Liquibase such as XML, YAML, JSON and you have to consider which it is the best option for you.

What is Liquibase?

It is an open source project which help us to manage the scripts execution into the database avoiding headaches during the SDLC.

How liquibase works?

First of all, you need to add the liquibase dependency and the database dependency (mysql in this post) into the pom.xml After that, add scripts into the project. Then you must to register the script added into the liquibase configuration xml.

Take into account that if your script is not registered into the liquibase configuration xml, script will not be executed automatically.

Finally you can run the following maven goal liquibase:update, which will create two tables into the database. All the scripts executed against the database will be stored into the databasechangelog table.

Pre-requisites

  • Java
  • Ant
  • Maven

Liquibase’s Goals

I will mention just some of them, the list below are the most used by me:

  • rollback: run all the scripts in the rollback section inside of dbchangelog configuration file.
  • tag: mark the last script executed.
  • update: run all the scripts in the sqlFile section inside of dbchangelog configuration file.
  • updateTestingRollback: allow to test all the scripts. First, do the update and then rollback task. Make sure that all your scripts has a rollback section inside of dbchangelog configuration file. Otherwise, it doesn’t work.

Note: See all the liquibase’s goals here.

Demo

You can download the example here.

As you can see, there are param.properties which have the database connection and another values.

1. Lets explain some key properties:

changeLogFile, filename which contains scripts to be executed.

tag, the name of tag.

2. Run the ant task mvn_liquibase_update, it will create t_user table. Also, script executed will be registered into the databasechangelog table.

3. Now, lets change changeLogFile value into the params.properties by db.changelog-1.1.0.xml

4. Run the ant task mvn_liquibase_update again, it will create t_role table.

Now, the scripts execution can be automated using Jenkins.

Zuul: Application Configuration Management

Months ago I read the following blog Mule Meets Zuul Part I | Part II at Mulesoft’s blog and Zuul caught my attention.

What is the problem?

It is a headache when you have to lead with a lot of property files and even worst when you have many environments.

What is Zuul?

Zuul is an open source web application which centralize and manage property files configuration.

What can I do?

  • Create environments
  • Upload property files
  • Create new entries
  • Clone property files between environments
  • Group property files in folders
  • Encryption support

Steps to install Zuul

Zuul can integrate with Google, Yahoo, LDAP and Active Directory.

Here the steps to set up Zuul against LDAP.

1. Install OpenLDAP.

2. Load this file into the LDAP.

3. Instal Mysql

1
2
3
yum install-y mysql-server mysql-devel
chkconfig mysqld on
service mysqld start

4. Create zuul database.

5. Download Zuul.

6. Set this parameters at:

Unix: %TOMCAT_HOME%/bin/catalina.sh

1
export JAVA_OPTS=-Dspring.profiles.active="security-ldap"

Windows: %TOMCAT_HOME%/bin/catalina.bat

1
set JAVA_OPTS=-Dspring.profiles.active="security-ldap"

7. Add database driver into %TOMCAT_HOME%/lib.

8. Copy ldap.properties and zuul-data-config.properties from zuul/WEB-INF/classes/examples to %TOMCAT_HOME%/lib.

9. Modify ldap.properties

ldap.properties
1
2
3
4
5
6
7
8
9
10
11
ldap.url=ldap://localhost:389
ldap.username=cn=Manager,dc=example,dc=com
ldap.password=p@ssw0rd
ldap.dn.ROLE_SYSTEM_ADMIN=CN=Zuul System Admins,OU=Groups,DC=acme,DC=com
ldap.dn.ROLE_ADMIN=CN=Zuul Admins,OU=Groups,DC=acme,DC=com
ldap.dn.ROLE_USER=CN=Zuul Users,OU=Groups,DC=acme,DC=com
ldap.group.search.base=OU=Groups,DC=acme,DC=com
ldap.group.role.attribute=entryDN
ldap.group.filter=member={0}
ldap.user.search.base=OU=Users,DC=acme,DC=com
ldap.user.search.filter=uid={0}

10. Start tomcat and zuul application will create the database tables using liquibase.

Now, you have Zuul application ready to use.

Create a property in dev environment named myproperty

Download Zuul demo

Now run mvn test, you will get the value from Zuul Service.

Screenshots

Create new property option.

Create new property view.

Property created.

Property view and ready to add key and values.

Adding new property.

Property created in DEV environment.

Key Management option.

Key Management view.

Changing password.

You can also create groups.

Review: RabbitMQ Essentials

I have had the opportunity to have acces to “RabbitMQ Essentials” book published by Packtpub.com. Thanks to the author David Dossot (@ddossot)

Are you interested in Messaging or Message Queuing?

This is a book that you should to read. It has a very good introduction about Messaging concepts, Advanced Message Queuing Protocol (AMQP) description and core concepts. Differences between AMQP and another protocols.

Chapters:

  • Chapter 1: A Rabbit Springs to Life
  • Chapter 2: Creating an application Inbox
  • Chapter 3: Switching to Server-push
  • Chapter 4: Handling Application Logs
  • Chapter 5: Tweaking Message Delivery
  • Chapter 6: Smart Message Routing
  • Chapter 7: Taking RabbitMQ to Production
  • Chapter 8: Testing and Tracing Applications
  • Appendix: Message Schemas

The book has a lot of examples in different programming languages, most of them in java but also in ruby and python. It also has a section about load test. So, I think that all the topics are covered.

Link: RabbitMQ Essentials

Continuous Inspection With SonarQube

What is Continuous Inspection?

It is an Agile Practice which tell us inspect the source code in order to find bad habits avoiding issues and bad design in the future.

What is SonarQube?

SonarQube is an Open Source Project based on seven axes of quality:

  1. Potential bugs
  2. Coding rules
  3. Tests
  4. Duplications
  5. Comments
  6. Architecture and design
  7. Complexity

Sonar (the firstname of this project) or SonarQube (current name) help us to identify points mentioned above.

Did you remember this phrase?

Loose coupling and high cohesion.

Into the dashboard, you can see RFC (Response For Class) and LCOM4 (Lack of Cohesion Of Methods version 4). Those are related to Coupling and Cohesion respectively.

Code Review and Pair Programming are practices that we need to do in order to support Continuous Inspection Process.