Java Lobby Announcements

Subscribe to Java Lobby Announcements feed
Recent posts in Java on
Updated: 7 hours 25 min ago

Securing URLs Using Spring Security

Sat, 2016-11-19 07:31

Typically when securing a URL you are looking to do one of the following:

  • Allow access to everyone to a given URL
  • Secure URL based on roles.
  • Secure URL based on multiple roles.
  • Secure URL based on IP Address.

This post shows how to do this using spring security

Categories: Java

The Internal Cache of Integers

Fri, 2016-11-18 12:31
The Internal Cache of Integers

There is an IntegerCache in java.lang.Integer which stores instances for values -128 though 127. This means that Integer.valueOf(17) always will return the very same instance, while Integer.of(200) will not. While this clearly has the advantage of reuse of commonly used Integer values, thus relieving the GC from some work, it also has implications for autoboxing and identity comparisons.

The Cache

An easy way to see the cache in action is to investigate identity for two small integers compared to numbers outside the scope of the cache. The following two statements both hold true.

Categories: Java

Fixed-Point and Floating-Point: Two Things That Don't Go Well Together

Fri, 2016-11-18 11:01

One of the more challenging aspects of software development can be dealing with floating-point numbers. David Goldberg's 1991 Computing Surveys paper What Every Computer Scientist Should Know About Floating-Point Arithmetic is a recognized classic treatise on this subject. This paper not only provides an in-depth look at how floating-point arithmetic is implemented in most programming languages and computer systems, but also, through its length and detail, provides evidence of the nuances and difficulties of this subject. The nuances of dealing with floating-point numbers in Java and tactics to overcome these challenges are well documented in sources such as JavaWorld's Floating-point Arithmetic, IBM DeveloperWorks's Java's new math, Part 2: Floating-point numbers and Java theory and practice: Where's your point?, Dr. Dobb's Java's Floating-Point (Im)Precision and Fixed, Floating, and Exact Computation with Java's Bigdecimal, Java Glossary's Floating Point, Java Tutorial's Primitive Data Types, and NUM04-J. Do not use floating-point numbers if precise computation is required.

Most of the issues encountered and discussed in Java related to floating-point representation and arithmetic are caused by the inability to precisely represent (usually) decimal (base ten) floating point numbers with an underlying binary (base two) representation. In this post, I focus on similar consequences that can result from mixing fixed-point numbers (as stored in a database) with floating-point numbers (as represented in Java).

Categories: Java

Three Methods to Automatically Validate PDF Data

Fri, 2016-11-18 10:01

An insurance customer delivery team wanted to (as part of regression testing) automate the validation of data present in PDF documents. After going through the requirements, we explored multiple options and suggested three solutions, each with its own set of unique features. Two of the options involve a two-step process, where the first step converts the PDF document into a text document, while text is extracted in the second step. In this article, we elaborate on the problem and also share an overview of each option.


Recently, we were in a discussion with a project delivery team that was dealing with PDF documents. The delivery team works for an insurance customer, where one of their activities is to generate customer policies as PDF documents. As a standard process, the PDF documents generated are verified for content and structure and then sent to the customer. After each functionality change, the team needs to perform a regression test using various data sets and multiple templates. Today, the team has to go through each generated PDF document and validate information like name, address, policy number, policy start date, and the like, manually. As the number of tests is expected to grow along with the number of PDF templates, the team wanted a solution that would reduce the manual efforts involved and work across a large volume of documents.

Categories: Java

Tracing in Microservices With Spring Cloud Sleuth

Fri, 2016-11-18 08:31

One of the problems developers encounter as their microservice apps grow is tracing requests that propagate from one microservice to the next. It can quite daunting to try and figure out how a requests travels through the app, especially when you may not have any insight into the implementation of the microservice you are calling.

Spring Cloud Sleuth is meant to help with this exact problem. It introduces unique IDs to your logging which are consistent between microservice calls which makes it possible to find how a single request travels from one microservice to the next.

Categories: Java

The Beauty of Quasi-Enumerations

Thu, 2016-11-17 23:37

Everybody knows at least some HTTP Status Codes, even if they don’t realize it. Most people did at one point or another in their web browsing career see the famous 404 – Not Found, or 200 – OK codes.

How could these codes be represented in Java code, however, when writing HTTP client or server code?

Categories: Java

Small Scale Stream Processing Kata (Part 1): Thread Pools

Thu, 2016-11-17 12:41

Once again I prepared a programming contest on GeeCON 2016 for my company. This time the assignment required designing and optionally implementing a system given the following requirements:

A system delivers around one thousand events per second. Each event has at least two attributes:

Categories: Java

Supporting Your Unsupported Class Version Error [Code Snippet]

Thu, 2016-11-17 11:01

This article covers how to fix Java errors akin to UnsupportedClassVersionError. Please feel free to comment/suggest if I missed something.

How to Reproduce the Error
  • Set JAVA_HOME with one version of Java. Include a “bin” of different Java versions in the PATH variable.
  • Compile the class, for example, with “javac”.
  • Start the JVM with following class: “java HelloWorld”.

With the steps above, you might see an error like:

Categories: Java

Gradle Goodness: Validate Model In Rule Based Model Configuration

Thu, 2016-11-17 10:01

Rule based model configuration gives Gradle more knowledge about the objects and their dependencies. This information can be used by Gradle to optimise the build process. We define rules on how we want Gradle to create objects and how we want to mutate objects in a class that extends RuleSource. We can also add rules to validate objects available in the Gradle model space. We use the @Validate annotation on methods that have validation logic. The first argument of the method is of the type of the object we want to validate. This type must be managed by Gradle.

In the following example we use the sample from a previous post. In this sample, we have a VersionFile class that is managed by Gradle. The class has a version and outputFile property. The version must be set and must start with a v. The outputFile property is also required.

Categories: Java

This Week in Spring: SpringOne Talks, REST APIs, and Spring 5's Approach

Thu, 2016-11-17 08:31

Welcome to another installment of This Week in Spring! Time sure is flying! We’ve got so much to get into this week. Can you believe we’re staring down 2017? We’ve got so much to cover so let’s get to it!

Categories: Java

Getting Started with Java and Couchbase [Video]

Thu, 2016-11-17 07:31

In this quickstart video tutorial, you'll learn how to set up and start building a Java application that communicates with Couchbase Server to save NoSQL JSON data as well as query for it numerous ways.

For more information on how to use the Couchbase Java SDK, visit the Couchbase developer portal. For help getting Couchbase Server set up on your machine, check out the Ubuntu, Windows, Mac, or Docker tutorials.

Categories: Java

Define-Datatype and Cases in Clojure

Wed, 2016-11-16 12:31

I'm going through the Essentials of Programming Languages (3rd ed.) book and it's been pretty good so far. In chapter 2, the authors use a pair of macros — define-datatype and cases — to make it easy to define data-driven programs, where objects belong to types, each of which has several "variants" with custom fields.

The canonical example used chapter 2 is the "Lambda calculus expression":

Categories: Java

Learn Drools: Part III (Filter Facts)

Wed, 2016-11-16 11:01

In the previous article, we saw how Drools creates a Cartesian product between Facts if there is a HAS-A relationship between Domain Object/Facts. In this article we will learn how to filter the facts. You can think it as of using Where clause in SQL to filter the resultset.

Let's understand it with the same problem we have used in the previous article, where we created the following Drools Rule.

Categories: Java

Gradle Goodness: Set Default Values with Rule-Based Model Configuration

Wed, 2016-11-16 10:01

When we use rule-based model configuration in our Gradle project, we can give Gradle rules on how to manage objects from the model space. These rules are defined in a class that extends RuleSource. When we want to set some default values for properties of a model object (in Gradle terms this is a subject) we can use the @Defaults annotation. Rules annotated with @Defaults are invoked right after the object is created and before any other methods that can mutate the state of the object.

The method, to set the default values, must have the type of the object as the first parameter. Other parameters are considered input parameters and can be used to set a default value based on other model objects.

Categories: Java

Pippo and Jersey (JAX-RS): A Match Made in Heaven

Wed, 2016-11-16 08:31

For the last few, days I have been playing with Pippo and Jersey (JAX-RS) and I wanted to share my experience with you. The result (a very good one) and the details of my experiment are materialized in this article — along with a demo application.

To begin with, I would like to say a few words about the main actors:

Categories: Java

Toward More Functional Java: Digging Into Nested Data Structures

Wed, 2016-11-16 07:31

In the last post, we saw an example that used a generator combined with a filter to find the first available port in a specific range. It returned an Optional to model the case when no open ports are found, as opposed to returning null. In this example, we'll look at how to use Java 8 streams to dig into a nested data structure and find objects of a specific type. We'll use map and filter operations on the stream, and also introduce a new concept, the flat-map.

In the original, pre-Java 8 code that I was working on in a project, the data structure was a three-level nested structure that was marshaled into Java objects from an XML file based on a schema from an external web service. The method needed to find objects of a specific type at the bottom level. For this article, to keep things simple we will work with a simple class structure in which class A contains a collection of class B, and B contains a collection of class C. The C class is a base class, and there are several subclasses C1, C2, and C3. In pseudo-code the classes look like:

Categories: Java

Gradle Goodness: Adding Tasks With Rule Based Model Configuration

Tue, 2016-11-15 12:31

Gradle has an incubating feature Rule based model configuration. This is a new way to configure Gradle projects where Gradle has more control of the configuration and the dependencies between configuration objects. This allows Gradle to resolve configuration values before they are used, because Gradle knows there is a dependency. With this new model, we don't need any lazy evaluation "tricks" we had to use. For example, there was an internal convention mapping mechanism for tasks to assign values to a task configuration after the task was already created. Also, the project.afterEvalute is a mechanism to have late binding for task properties. With the new rule based model, Gradle can do without these options, we can rely on Gradle resolving all dependent configuration values when we create a task.

In Gradle, we already know about the "project space" where the Project object is the root of the object graph. For example repositories are part of the project space. Gradle can get some useful information from the project space, but it is mostly a graph of objects that Gradle only partially can reason about. Then we have the "model space". This is part of a project and we can use it in our build script with the model configuration block. The model space is separate from the project space and contains objects that are managed by Gradle. Gradle knows dependencies between the objects and how to create and change them. This helps Gradle to optimize build logic. To help Gradle we must define rules to work with objects in the model space. Each rule is like a recipe for Gradle on how to work with the model. Gradle can build a graph of models and know about dependencies between models. This way Gradle guarantees that model objects are completely configured before being used. For example, if a rule needs a VersionFile model configuration object then Gradle makes sure that the VersionFile is created and all properties are set. So we don't need any lazy or late binding anymore, because the properties will be set (Gradle makes sure) when we want to use them. The rules are defined by a class that extends RuleSource. Such a class is stateless and only contains methods to work with the model objects. Gradle has some specific annotations that can be used on methods to indicate what a method should do.

Categories: Java

The Way of the Lambda and removeIf()

Tue, 2016-11-15 11:01
The Way of the Lambda: Elegant and Efficient

In Java 8, you can do:

items.removeIf(i -> predicate(i));

Where you used to do:

Categories: Java

JavaNord 2016 Retrospective

Tue, 2016-11-15 10:01

This article is dedicated to the non-commercial Java conference that I visited last month in Hannover, Germany. JavaNord took place for the second time in October. The spoken language is German but most of the presentations are written in English. I wanted to list the links to the presentations I visited along with a small description.

Life, IT, and Everything — Uwe Friedrichsen


Categories: Java

Examining ValueObjects

Tue, 2016-11-15 08:31

When programming, I often find it's useful to represent things as a compound. A 2D coordinate consists of an x value and y value. An amount of money consists of a number and a currency. A date range consists of start and end dates, which themselves can be compounds of year, month, and day.

As I do this, I run into the question of whether two compound objects are the same. If I have two point objects that both represent the Cartesian coordinates of (2,3), it makes sense to treat them as equal. Objects that are equal due to the value of their properties, in this case their x and y coordinates, are called value objects.

Categories: Java