Java Lobby Announcements

Subscribe to Java Lobby Announcements feed
Recent posts in Java on DZone.com
Updated: 1 hour 12 min ago

Announcing Java EE Microprofile

3 hours 37 min ago

The Java community has likely been the greatest factor in Java’s success and influence over the past 20+ years. This community has rallied and collaborated to improve the language and its capabilities through JSRs, libraries, APIs, frameworks, and more. Now Java EE has become “the dominant standard for companies building business-critical multi-tier enterprise applications.”

With that in mind, and with microservices architectures becoming ever more popular as monolithic applications hinder the advances of continuous delivery and integration, minimize performance, and lead to downtimes that are unacceptable for today’s technologies, there’s a new hurdle for the Java community to jump. Yesterday, the MicroProfile initiative was announced to inspire the community to overcome that hurdle together.

Categories: Java

Singletons Must Die

10 hours 10 min ago

I think it's too obvious to say that a singleton is an anti-pattern as there are tons of articles about that (singleton being an anti-pattern). However, more often than not, the question is how to define global things without a singleton; and the answer to that is not obvious for many of us. There are several examples: a database connection pool, a repository, a configuration map, etc. They all naturally seem to be "global"; but what do we do with them?

Perdita Durango (1997) by Álex de la Iglesia

I assume you already know what a singleton is and why it's an anti-pattern. If not, I recommend you read this StackOverflow thread: What is so bad about singletons?

Categories: Java

Design Patterns in the Real World: Strategy

11 hours 11 min ago

Quite some software engineers think that design patterns are some overly complicated, mythical, abstract things that bring no practical value to software development. This is unfortunate. In order to prove they are indeed something real, in this (and some upcoming) post(s) we are going to take a look at a few examples on how real software products implement some of the GoF design patterns. Today, we are going to visit Strategy, from HotSpot’s point of view. (See the previous post about Flyweight here).

Strategy Defined

Wikipedia defines Strategy as follows:

Categories: Java

Groovy Goodness: Represent Map as String

12 hours 10 min ago
Suggested zone: Java

Groovy adds to Map objects the toMapString method. With this method we can have a String representation of our Map. We can specify an argument for the maximum width of the generated String. Groovy will make sure as many key/value pairs as possible are added as a pair, before adding three dots (...) if the maximum size is exceeded.

def course = [ name: 'Groovy 101', teacher: 'mrhaki', location: 'The Netherlands'] assert course.toMapString(15) == '[name:Groovy 101, ...]' assert course.toMapString(25) == '[name:Groovy 101, teacher:mrhaki, ...]'

As mentioned in a previous post we can use the toListString method to represent a List as a String:

Categories: Java

Comparing Lombok and Kotlin

13 hours 10 min ago

I've known about Lombok for a long time, and I even wrote on how to create a new (at the time) @Delegate annotation. Despite this and even though I think it’s a great library, I’ve never used it in my projects. The reason for this is mostly because I consider setting up the Lombok agent across various IDEs and build tools too complex for my own taste in standard development teams.

In comes Kotlin, which has support for IDEs and build tools right out-of-the-box, plus seamless Java interoperability. So, I was wondering whether Lombok would still be relevant. In this article, I’ll check if Kotlin offers the same feature as Lombok and how. My goal is not to downplay Lombok’s features in any way, but to perform some fact-checking to let you decide what’s the right tool for you.

Categories: Java

Convert a List to a Comma-Separated String in Java 8

13 hours 10 min ago

Converting a List<String> to a String with all the values of the List comma separated in Java 8 is really straightforward. Let’s have a look how to do that.

In Java 8

We can convert the List to a stream and then use the StringJoiner as follows:

Categories: Java

Functional Programming in Pure Java: Functor and Monad Examples

Tue, 2016-06-28 09:01

This article was initially an appendix in our Reactive Programming with RxJava book. However, an introduction to monads, albeit very much related to reactive programming, didn't suit that very well. So I decided to take it out and publish this separately as a blog post. I am aware that "my very own, half correct and half complete explanation of monads" is the new "Hello, world" on programming blogs. Yet the article looks at functors and monads from a specific angle of Java data structures and libraries. Thus I thought it's worthwhile to share.

RxJava was designed and built on top of very fundamental concepts like functors, monoids, and monads. Even though Rx was modeled initially for imperative C# language and we are learning about RxJava, working on top of a similarly imperative language, the library has its roots in functional programming. You should not be surprised after you realize how compact the RxJava API is. There are pretty much just a handful of core classes, typically immutable, and everything is composed using mostly pure functions.

Categories: Java

@MockBean—Spring Boot's Missing Ingredient

Tue, 2016-06-28 08:01
The problem

I really liked Spring Boot’s concept, since I first saw it. The only thing, I felt, it was missing was better support for testing in general.

The Problem

It all started when I wanted to have a way to test 'current date' logic in my application. It was supposed to be a reusable, easy-to-use feature (via an annotation) in a custom Spring Boot Starter. The starter is based on Java 8, hence JSR-310 Date / Time API is a natural pick. Current date is only one of several things I want to make "mockable" in integration tests. There are other areas of functionality that are good candidates for mocking out. Keeping that in mind, I will use the ZonedDateTime class as a mocking example across the article.

Categories: Java

Overview of Spring Aspect Oriented Programming (AOP)

Tue, 2016-06-28 07:01

In this blog post, Java web development professionals give a descriptive view about Spring Aspect Oriented Programming (AOP) for general readers. You will read about advantages, disadvantages, uses, and important terminologies with examples.

Advantages of Spring AOP
  1. AOP is non-invasive:
  • Service or Domain classes get advice by the aspects (cross-cutting concerns) without adding Spring AOP related classes or interfaces into the service or domain classes.
  • Allows the developers to concentrate on the business logic, instead of the cross-cutting concerns.

2. AOP is implemented in pure Java:

Categories: Java

Accurest Becomes Part of the Spring Cloud Contract

Tue, 2016-06-28 06:01

I’m extremely happy to announce that we have successfully rebranded the Accurest project. It’s officially become part of the Spring Cloud Contract initiative. Ladies and Gentlemen please welcome the new projects:

A Little Bit of History

Accurest was created because of the lack of an easy-to-use tool for doing Consumer Driven Contracts. From our production experience, the biggest problem was the lack of verification that the defined contract actually does what it says it does. We wanted to ensure that tests are automatically generated from the contract so that we can have a proof that the stubs are reliable. Since there was no such tool, the first commit of Accurest took place on 12/2014. This idea and its implementation was initially set by Jakub Kubrynski and me. The last available version of Accurest was 1.1.0 released on 06/2016 (the docs for the old version are available here). During these 19 months, a lot of feedback has been gathered. The tool has received very good reception, and that made us want to work even harder. Many times we have missed out on good sleep, so as to fix a bug or develop a new feature in Accurest... it's been hard but rewarding work!

Categories: Java

Installing a Java Application as a Windows Service

Mon, 2016-06-27 10:01

It sounds like something you’d never need, but sometimes, when you distribute end-user software, you may need to install a java program as a Windows service. I had to do it because I developed a tool for civil servants to automatically convert and push their Excel files to the opendata portal of my country. The tool has to run periodically, so it’s a prime candidate for a service (which would make the upload possible even if the civil servant forgets about this task altogether, and besides, repetitive manual upload is a waste of time).

Even though there are numerous posts and StackOverflow answers on the topic, it still took me a lot of time because of minor caveats and one important prerequisite that few people seemed to have – having a bundled JRE, so that nobody has to download and install a JRE (would complicate the installation process unnecessarily, and the target audience is not necessarily tech-savvy).

Categories: Java

Unit Testing JPA... Stop Integration Testing!

Mon, 2016-06-27 09:01

I want to start by asking a simple question.

"How do you unit test your JPA classes?"

Categories: Java

Revisiting the Advanced Theories of ‘Java Garbage Collection’

Mon, 2016-06-27 08:01

JVM or the ‘Java Virtual Machine’ resorts to Garbage Collection for automatically housekeeping memory while an application propels in the hindsight. Without the garbage collector, the programmer would have to undertake memory deallocation—explicitly for each and every application. This can leverage productivity in most cases. With a Garbage Collector in place, the program can solely dedicate its core to problem solving, letting the JVM handle issues pertaining to memory management. While this happens to be a complicated process, a detailed analysis is actually appropriate when dealing with the advanced concepts of JVM and the Java Garbage Collection. This post enumerates each aspect while detailing APIs in a lucid yet self-explanatory manner:

Figure 1: Simplifying Garbage Collection

Categories: Java

Overriding Dependency Versions with Spring Boot

Mon, 2016-06-27 07:01

This article explains some of the dependency management tricks that can be used to create libraries and apps that depend on newer versions of a transitive dependency than those managed by a platform like Spring Boot or the Spring IO Platform. The examples below use Reactor as an example of such a dependency because it is nearing a major new release (2.5.0) but existing dependency management platforms (Spring Boot 1.3.x) declare a dependency on older versions (2.0.7). If you wanted to write an app that depended on a new version of Reactor through a transitive dependency on a library, this is the situation you would be faced with.

It is a reasonable thing to want to do this, but it should be done with caution, because newer versions of transitive dependencies can easily break features that rely on the older version in Spring Boot. When you do this, and apply one of the fixes below, you are divorcing yourself from the dependency management of Spring Boot and saying “hey, I know what I am doing, trust me.” Unfortunately, sometimes you need to do this in order to take advantage of new features in third party libraries. If you don’t need the new version of Reactor (or whatever other external transitive dependency you need), then don’t do this; just stick to the happy path and let Spring Boot manage the dependencies.

Categories: Java

To Interface or Not to Interface

Sun, 2016-06-26 07:01

How do you decide if a class should be hidden behind an interface or not? What has to happen to make you think "Oh, I should create an interface here"? Quick Googling and we land on a StackOverflow page that gives us a handful of hints.

First thing there that I hear, read, or see most often is that an interface can have multiple implementations with different behaviours and stuff. I've even read a few times that you shouldn't use an interface if there aren't multiple possible implementations. Nonsense! Firstly, interfaces (abstractions in general) give us the power of Dependency Inversion. That's probably even more important than polymorphism. It's obvious that many of such "inverting" interfaces will have only one implementation and that's not a problem! Secondly, there are cases when the possibility of multiple implementations leads to an unnecessary interface. Look and suffer:

Categories: Java

Managing Secrets With Vault

Sat, 2016-06-25 08:01

Passwords, API keys, and confidential data fall into the category of secrets. Storing secrets the secure way is a challenge with limiting access and a true secure storage. Let’s take a look at Hashicorp Vault and how you can use it to store and access secrets.

How Do You Store Secrets?

Passwords, API keys, secure Tokens, and confidential data fall into the category of secrets.
That’s data which shouldn’t lie around. It mustn’t be available in plain text in easy to guess locations. In fact, it must not be stored in plaintext in any location.

Categories: Java

Java 8 Hashmaps, Keys and the Comparable Interface

Sat, 2016-06-25 08:01
/** * This post is intended to be a 101 quickie for the less experienced. * It does not provide new or innovative ways of solving a certain problem, * just summarizes a topic the way I see it. * **/

Java 8 is coming with a lot of improvements/enhancements compared to the previous version. There are pretty many classes that have been updated, HashMap—as one of the most used data structure—is no exception. In this post, we are going to discover a new, important feature that Java 8 brings to us in case of hash collisions.

First of all, what is the easiest way to create collisions in a HashMap? Of course, let’s create a class that has its hash function messed up in the worst way possible: a hashCode() implementation that returns a constant value. I usually ask people during technical interviews what happens in such case. Very many times the candidates think that the map will contain one and only one entry, as an older entry will always be overwritten by the newer one. That, of course, is not true. Hash collisions do not cause a HashMap to overwrite entries, that only happens if we try to put two entries with keys equal based on their equals() method. Entries with non-equal keys and the same hash code will end up in the same hash bucket in some kind of data structure. See below an example for Java 7:

Categories: Java

Create DynamoDB Tables With Java

Sat, 2016-06-25 07:01

In this post, we will create Tables on a DynamoDB Database the Java way.

Before getting started, we need to have local DynamoDB installed since we want to avoid any costs for DynamoDB usage. There was a previous post on local DynamoDB.

Categories: Java

Groovy Goodness: Turn a Map or List as String to Map or List

Fri, 2016-06-24 10:01

In a previous post we learned how to use the toListString or toMapString methods. With these methods we create a String representation of a List or Map object. With a bit of Groovy code we can take such a String object and turn it into a List or Map again.

In the following code snippet we turn the String value [abc, 123, Groovy rocks!] to a List with three items:

Categories: Java

Binding Map to XML: Dynamic Tag Names with JAXB

Fri, 2016-06-24 10:01

To convert Map to XML , the usual process is to stick with an element name,  while altering its attribute and text content for different map entries. For example, to save this map:

{"key1": "value1", "key2": "value2"}

into XML content, we usually save it as:

Categories: Java

Pages