Categories: Java

A Practical Guide to Project Lombok

A Practical Guide to Project Lombok

Java is a powerful language but often criticized for being too verbose (aka boilerplate – term used to describe code that is repeated in many parts of an application with little alteration). Project Lombok came to help the lazy coder in you. It basically generates code for you. But instead of adding code to your non-compiled classes, like an IDE generally does, Lombok works during the build process by modifying the Java bytecode of your class files.

It’s very simple to configure it, their project page has all you need to help you to add it in your projects.

Lombok Annotations

1. @Getter and @Setter

The @Getter and @Setter annotations are used to generate accessor methods for fields, @Getter annotation respects the convention if the field is a boolean property. Both @Getter and @Setter take an optional parameter to specify the access level for the generated method:

Lombok code:

Equivalent Java native code:

Categories: Java

7 Useful Examples of the Java 8 Stream API

7 Useful Examples Java 8 Stream API

You probably already heard about the Java 8 Stream API (If not, you can find more information about it here). It’s basically an abstraction that allows us to process data in a simple and declarative way. Besides that, streams can leverage multi-core architectures without you having to write a single line of multi-thread code.

In this article we won’t describe each of the available methods on the Java 8 Stream API. Instead, we will focus on practical examples of how to consume this new API.

First of all, let’s present our Athlete model class:

Now, let’s initialize a List of Athletes with 6 objects…

Based on the previously presented List of Athletes, let’s now see how to easily manipulate this data and extract the information we want.

1. Getting the quantity of female athletes (filter + count)

2. Retrieving the list of American athlete names (filter + map + collect)

Categories: Java, Miscellaneous

Automatically Mapping Java Objects

Automatically Mapping Java Objects

Automatically Mapping Java ObjectsIn this article we aim to clarify why sometimes it’s important to map two different classes and how it’s possible to do it by building the simplest possible mapping manager step-by-step. Design choices / constraints often produce a considerable amount of mechanical work which we will try to minimize by automatically mapping Java objects.

By “mapping objects” we mean copy one object state to another object. Sometimes the mapping is transparent, considering that both objects have the same attributes, but often it happens between two objects with different attributes. Those who are familiar with Web API development probably already faced a situation where your resource representation is similar but not identical to the domain model or even when you don’t want to expose your domain objects to any other layer within your system. In these cases we generally create a component to abstract this mapping task, which also allow us to reuse it in other parts of the system.

An advantage of having a mapping component (e.g. to map your domain to DTO and vice versa) is not as apparent when you are only supporting a single mapping, but as the number of mappings increases, having that code isolated from the domain helps to keep the domain simpler and leaner. You won’t be cluttering your domain with a lot of extra weight.

Hands On

To clarify the mapping idea, let’s start creating two domain classes (Customer and Address) and one DTO class (CustomerDTO).

Categories: Groovy, Java, Unit Tests

Introduction to Spock Framework

introduction spock framework

What is Spock Framework anyway?

Spock Framework is an open source testing and specification framework for Java and Groovy applications. It lets you write concise, expressive tests, Behaviour Driven Development (BDD) like form, what makes your test more clear. Spock is compatible with most IDEs, build tools, and continuous integration servers. Spock is inspired from JUnit, jMock, RSpec, Groovy, Scala.

Understanding How the Spock Framework Works

Suppose we have a Publisher class that sends messages to its Subscribers:

And the respective unit test class:

In Spock Framework we don’t have tests, we have specifications. A specification is represented as a Groovy class that extends from “spock.lang.Specification”, which is actually a JUnit class.

Categories: ELK, Java

Monitoring Java applications with ELK

Monitoring Java applications with ELK

 Monitoring Java applications with ELKMonitoring Java applications with ELK (Elasticsearch, Logstash and Kibana) will show you step-by-step how to properly centralize your Java application logs by sending all its log messages to a remote ELK server. Using this approach you can have all information generated by Java applications, running along multiple servers, in a centralized place. This way you can easily create dashboards and start to analyze your applications in a more high level and practical manner.

You know it’s sad but true

Let’s think about a very common scenario in many companies: many developed Java applications running across multiple application servers, each application performing many operations per day and logging thousands and thousands of lines that generally nobody checks unless some problem occurs along the applications. It sounds familiar, doesn’t it? The biggest issue here is that, unless we are debugging a production problem, the logs have no value at all. They are not telling us anything about aspects we must care about, such as business process performance. There’s gold within these logs!

How about building a better scenario?

Think about the sad story I just told you. Now imagine all your Java applications producing the same amount of logs but then sending them to a centralized place where all received data is accordingly analyzed, modified and finally presented in a real accessible way. Would you like to know how many payments did your system realize in the last minute, day or week? What about how many times a specific exception was thrown? The possibilities are infinite.

Let’s see how to achieve this desired scenario using the ELK stack.

Proposed solution

Our proposed solution will combine one Java Application configured to use Logback (the successor of the famous Log4J), one specialized Log Appender class “LogstashTcpSocketAppender” (provided by the Logstash team) and one ELK server.

Tutorial – Monitoring Java applications with ELK

Step 1 – Setup the ELK stack

We have two detailed articles about how to setup the ELK stack on Ubuntu and Windows, please check them following the links bellow:

Step 2 – Configure Logstash to receive our logs

Within the ELK server, create a new configuration file, /etc/logstash/conf.d/logback-listener.conf for Ubuntu 16.04 and D:\ELK\logstash-2.3.4\conf.d\logback-listener.conf for Windows, inserting the following content: