Java Lobby Announcements

Subscribe to Java Lobby Announcements feed
Recent posts in Java on
Updated: 14 hours 46 sec ago

Introducing Picocog: A Lightweight Code Generation Library

Mon, 2018-01-08 07:01

Picocog is a lightweight and new open-source Java library created to make programmatic code generation quite easy.

Code generation is the process by which source code can be automatically generated based on inputs.

Categories: Java

Java Memory Management

Sun, 2018-01-07 08:01

You might think that if you are programming in Java, what do you need to know about how memory works? Java has automatic memory management, a nice and quiet garbage collector that quietly works in the background to clean up the unused objects and free up some memory.

Therefore, you as a Java programmer do not need to bother yourself with problems like destroying objects, as they are not used anymore. However, even if this process is automatic in Java, it does not guarantee anything. By not knowing how the garbage collector and Java memory is designed, you could have objects that are not eligible for garbage collecting, even if you are no longer using them.

Categories: Java

7 Things to Know Getting Started With Spring Boot

Sat, 2018-01-06 08:01

Spring Boot is a tricky framework to understand. In this tutorial for beginnners with Spring Boot, we will look at the basics of Spring Boot and help you understand the important concepts — Starter Projects, Auto Configuration, and Starter Parents.

Understanding Spring Boot's Architecture

The building blocks of Spring Boot are:

Categories: Java

Kotlin: The Tuple Type

Fri, 2018-01-05 23:01

It is very easy to write a Tuple type with the expressiveness of Kotlin. My objective, expressed in tests, is the following:

  1. Be able to define a Tuple of up to 5 elements and be able to retrieve the elements using an index like a placeholder in a test expressed with 2 elements, like this:
  2. val tup = Tuple("elem1", "elem2") assertThat(tup._1).isEqualTo("elem1") assertThat(tup._2).isEqualTo("elem2")

Categories: Java

Binding Properties From External Sources in Spring Boot Apps

Fri, 2018-01-05 13:01

As per Spring's documentation, Spring Boot allows us to externalize configurations, so you can work with the same application code in different environments. You can use property files, YAML files, environment variables, and command-line arguments to externalize configurations. But in this article we will mostly check how to read configurations from property or yml files. True externalization requires reading property or YAML files from external cloud sources like Consul, where Consul properties like consul host, port, and keys are provided to the application via environment variables. Then our same application code can run in different environments. We will cover that in some other article.

Here, we will discuss how configuration keys are bound to actual objects in Spring Boot applications. The most basic way to bind your configurations is from property or YAML files to POJO classes, which we can use later in the lifecycle of our application.

Categories: Java

Run Code on Startup With Play and Scala

Fri, 2018-01-05 10:01

Depending on various projects, sometimes there is a need to execute some actions on initialization, just before our application starts to serve requests.

It was a common practice to call the functions that we wanted to get executed through GlobalSettings, but that was not recommended.

Categories: Java

Playing Sround With Spring Bean Configuration

Fri, 2018-01-05 07:01

In this tutorial, we will have a look at something more basic on the Spring spectrum — beans. But, as with most things, the fundamentals can sometimes be forgotten, and bean creation is something that I personally have not looked at properly since switching from XML to Java configuration.

The creation of beans is pretty important to the use of Spring (probably more like very important), allowing us to have Java classes that live within the application context that can be used within other beans/classes without constantly creating new instances every time we need to use one of their methods.

Categories: Java

This Week in Spring: The 7th Anniversary Edition

Thu, 2018-01-04 19:01

Hi, Spring fans! Welcome to another installment of This Week in Spring! I hope you had a wonderful, safe, and fruitful new year celebration.

This is the first roundup of the new year and so it is also the 7th year anniversary of my starting this column. Every Tuesday, without fail (come holiday, sick-day, flights, or frights!), since the first week of January 2011, I’ve put together this roundup. It has been, and continues to be, an honor. As usual, I’m always happy to have feedback and suggestions from you, the most amazing community ever! Don’t hesitate to reach me on Twitter (@starbuxman).

Categories: Java

Java Quiz 8: Upcasting and Downcasting Objects

Thu, 2018-01-04 13:01

Before we start with this week's quiz, here is the answer to Java Quiz 7: Using Unary Operators.

x++ and ++y are very similar, but not exactly the same. Both increment the value of the variable by one, but ++y increments the value by one before the current expression is evaluated, while x++ increments the value of the variable by one after the expression is evaluated. The statement MyClass mc = new MyClass(3, 3); creates the object mc. By using the statement this.x = x++; the value of x remains x, while the statement this.y = ++y; increments the value of y by one. So, y = 4. The statement System.out.println(mc.method(mc)); invokes the method. The statement mc.x += 9; increments the value of x by 9. So, x = 3 + 9 = 12. The statement mc.y += 2; increments the value of y by 2. So, y = 4 + 2 = 6. The method returns mc.x + mc.y = 12 + 6 = 18.

Categories: Java

Writing Custom AssertJ Assertions [Snippet]

Thu, 2018-01-04 10:01

AssertJ is an alternative matching library to the widely used Hamcrest matchers. For my own projects, I, in fact, have changed to solely use AssertJ — I just find the fluid interfaces and extensibility quite appealing.

You can write custom assertions as follows.

Categories: Java

Java Concurrency in Depth (Part 2)

Thu, 2018-01-04 07:01

In the first part of this series, Java Concurrency in Depth (Part 1), I discussed the internals of Java's synchronization primitives (synchronized, volatile, and atmoic classes) and the pros and cons of each of them.

In this article, I will discuss other high-level locks that are built on top of volatile, atomic classes and Compare-And-Swap.

Categories: Java

Optional... What Else?

Wed, 2018-01-03 23:01

Optional is in some ways, the Java implementation of the Maybe monad. Don’t get scared by the ‘M’ word. It is simply an encapsulation to handle a specific case of a type — in this case, the possibility of a null value. Just consider it a wrapper to force the user to check if the value is present or not.

When using an Optional in a declarative way, we are able to execute a map function on it. The lambda supplied with the map will only be executed if the Optional is filled with a value.

Categories: Java

Single-Responsibility Principle Done Right

Wed, 2018-01-03 19:01

I am a big fan of the SOLID programming principles by Robert C. Martin. In my opinion, Uncle Bob did a great job when he first defined them in his books. In particular, I thought that the Single-Responsibility Principle was one of the most powerful among these principles, yet one of the most misleading. Its definition does not give any rigorous detail on how to apply it. Every developer is left to their own experiences and knowledge to define what a responsibility is. Well, maybe I found a way to standardize the application of this principle during the development process. Let me explain how.

The Single-Responsibility Principle

As is normal for all the big stories, I think it is better to start from the beginning. In 2006, Robert C. Marting, a.k.a. Uncle Bob, collected in the book Agile Principles, Patterns, And Practices in C# a series of articles that represent the basis of clean programming — the principles are also known as SOLID. Each letter of the word SOLID refers to a programming principle:

Categories: Java

dotenv for Java and the JVM

Wed, 2018-01-03 13:01

When working within the context of a modern microservice architecture, one often encounters services written in a variety of languages using a variety of frameworks. There are numerous benefits to microservice architectures, however, configuring each service can be quite complex. Each framework may offer their own configuration method, e.g. property files, YAML, JSON, etc. As the number of microservices piles up, this quickly becomes a DevOps nightmare.

To avoid this problem, we need a single method for configuration. Fortunately, there is one, dotenv, and most languages already offer a port. dotenv is a library originating from the Ruby community. It offers a simple, consistent, 12-factor compliant method to configure an application using environment variables.

Categories: Java

Transactional Exception Handling in CDI [Snippet]

Wed, 2018-01-03 10:01
In Java EE, exceptions that are raised during the execution of a transactional business method cause the transaction to roll back. However, this is only the case for system exceptions, that is, runtime exceptions, which are not declared in the method signature.

For application exceptions, that is, checked exceptions, or any exception annotated with @ApplicationException, the transaction is not automatically rolled back. This sometimes causes confusion among enterprise developers.

For EJB business methods, the transactions can be forced to roll back on application exceptions as well by specifying @ApplicationException(rollback = true). However, this annotation is only considered if the managed bean in an EJB.

Categories: Java