Currently, reactive is one of those trendy buzzwords in programming. Reactive is a word widely used as a prefix for “system” and “programming,” which both describe very different matters.
However, as an IT consultant and reactive sponsor, I have had a lot of experience and conversations surrounding reactive, but essentially, the main question is always the same:
Is there a path for data validation in an elegant, standard, and concise way? Is there a way that doesn’t fall into unreadability, helps us to keep most of the data validation logic together, and has most of the code already done for us by developers of popular Java frameworks?
Spring Data JPA was created primarily to allow easy query creation with query generation by method name. However, sometimes, we need to create complex queries and cannot take advantage of a query generator.
The Spring Data JPA provides a repository programming model that starts with an interface per managed domain object. Defining these interfaces serves two purposes: first, by extending the JpaRepository, we get a bunch of generic CRUD methods, like save, findAll, delete, and so on. Second, this will allow the Spring Data JPA repository infrastructure to scan the classpath for this interface and create a Spring bean for it. A typical repository interface will look something like this:
I know — there are A LOT of technologies listed in the title of this article. Spring WebFlux has been introduced with Spring 5 and Spring Boot 2 as a project for building reactive-stack web applications. I have already described how to use it together with Spring Boot and Spring Cloud for building reactive microservices in that article: Reactive Microservices with Spring WebFlux and Spring Cloud. Spring 5 has also introduced projects that support reactive access to NoSQL databases, like Cassandra, MongoDB, or Couchbase. But, there was still a lack of support for reactive to provide access to relational databases. The change is coming together with the R2DBC (Reactive Relational Database Connectivity) project. That project is also being developed by Pivotal members. It seems to be a very interesting initiative, however, it is at the beginning of the road. Anyway, there is a module for integration with Postgres, and we will use it for our demo application.
R2DBC will not be the only one new interesting solution described in this article. I will also show you how to use Spring Data JDBC – another really interesting project that was released recently. It is worth mentioning the features of Spring Data JDBC. This project has already been released and is available under version 1.0. It is a part of bigger Spring Data framework. It offers a repository abstraction based on JDBC. The main reason of creating that library is to allow access to relational databases using Spring Data (through
CrudRepository interfaces) without including the JPA library to the application dependencies. Of course, JPA is still certainly the main persistence API used for Java applications. Spring Data JDBC aims to be simpler conceptually than JPA by not implementing popular patterns like lazy loading, caching, dirty context, and sessions. It also provides very limited support for annotation-based mapping. Finally, it provides an implementation of reactive repositories that use R2DBC for accessing the relational database. Although that module is still under development (only a SNAPSHOT version is available), we will try to use it in our demo application. Let’s proceed to the implementation.
In this article, we will learn more about the commonly-used
LocalDateTime Class APIs with examples.
LocalDateTime represents a combination of date and time.
This is the most commonly used class when we need a combination of date and time. The class offers a variety of APIs, and we will look at some of the most commonly used ones.
StackOverFlowError is one of the common confronted JVM errors. In this blog post, we will look at the inner mechanics of thread stacks, reasons that can trigger StackOverFlowError, and potential solutions to address this error.
To gain deeper understanding into StackOverFlowError, let’s review this simple program:
Hi, Spring fans! Welcome to another installment of This Week in Spring! This week, I’m in Toronto, Canada, where I’ve been speaking to customers and at a meetup, then it’s off to St. Petersburg, Russia, for the epic Joker conf 2018. As usual, we’ve got a ton to cover, so let’s get to it!
jlinkin Java 9+.
In the previous article, we saw several examples of functions as first-class citizens and some of the kinds of uses they can be put to. Just to recap, a function is a first-class citizen when it is a value in its own right and can be passed around a program, just like any other type of value.
Now, when a function accepts another function as its argument, yields another function as its return value, or both, it is said to be a higher-order function. We actually already saw an example in the previous article, if you recall the Sieve of Eratosthenes exercise, which had this function in it:
These top Java programming books are some of the best books to learn Java — I would even say that some of them are the best Java books ever published. Whenever a programmer starts learning the Java programming language, the first question he asks is, "Which book should I refer to learn Java?," or "What is the best book to learn Java for beginners?," or "Can you tell me some good books to learn Java?" That, itself, says how important Java books are for programmers especially beginners. Despite having so many free resources available in Java, like tutorials, tips, blogs, and code examples, Java books have their own place because of these two reasons:
These Java books are my personal favorites, and whenever I get some time, I prefer to read them to refresh my knowledge. Though I have read many of them already (I have read Effective Java at least four times), I always want to learn something new. These books are the best available today and equally useful for beginners, intermediate, and advanced Java programmers.
This post is a continuation from our previous post on deploying Spring Boot to ECS. In our second installment, we will cover how to deploy a Spring Boot application in the ECS container. We will be using a simple task planner application that is available on GitHub.
A Docker file is already available inside this project. Make sure you have Git, Docker, and AWS CLI installed wherever you are running the following commands. Check out this link for a Git clone.
The idea for writing this article came to me after a discussion from my previous article about the Filterer Pattern. The discussion concerned the validity of introducing subtype polymorphism (instead of parametric polymorphism) into a composition-based design.
The goal of this article is to present a case where subtype design works better than parametric design. From this, I can draw a conclusion that, in certain context, subtype design is better than a parametric one. However, it is a non-goal of this article to speculate how often such context occurs in the real world (I have encountered such context only twice so far).
Excel is — no doubt — the most commonly used business application in the world. It is so easy that almost anyone can pick it up without learning. It is powerful in the sense that anyone can design a template and style it nicely. Along with that, anyone can create custom reports or forms with its powerful built-in features like sorting, filtering, charting and conditional formatting — it even comes with advanced formula functions for professionals.
However, Excel Sheets are scattered among each user and require frequent uploading/downloading/emailing when traveling among systems and users.
The main news so far has been the release of Java 11. It’s not a large release in terms of new developer functionality since Java 10, but it is the first Long Term Support Release since Java 8. So if you’ve been hanging around on Java 8, now’s the time to look at all new things that made it into Java 9, 10, and 11. Java 11 also removes features. This, like, never happens in Java. Another noteworthy change is that Oracle changed its licensing and support model since Java 8. Read this month’s Annotated Monthly to find out more.
I covered this topic in the last Annotated Monthly since it was an area of some concern in the community. Since then, the Java Champions (an independent group of experts) have released a comprehensive document covering the situation. I also wrote a summary of what I hope are the most relevant points for developers. Here are other relevant links, if you’re interested.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.1:shade (default) on project cores-batch: Error creating shaded jar: invalid LOC header (bad signature) -> [Help 1] ... .... (remove many lines for brevity). ... Caused by: java.util.zip.ZipException: invalid LOC header (bad signature) at java.util.zip.ZipFile.read(Native Method) at java.util.zip.ZipFile.access$1400(ZipFile.java:56) at java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:679) at java.util.zip.ZipFile$ZipFileInflaterInputStream.fill(ZipFile.java:415) at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158) at java.io.FilterInputStream.read(FilterInputStream.java:107) ...
org.springframework.context.ApplicationContext interface represents the Spring IoC container. This container instantiates, configures, and assemble beans by using externally provided configuration metadata.
BeanFactory are the same, but the
ApplicationContext adds more enterprise-related functionality. In short, the
ApplicationContext is a superset of the
Java Flight Recorder (JFR) used to be a commercial add-on of the Oracle JDK. As it’s been open sourced along with Java Mission Control, everyone using OpenJDK 11 can now troubleshoot their Java apps with this excellent tool for free of charge. JFR, previous proprietary solution, might be lesser known for those relying on previous versions of OpenJDK. Therefore, I thought it was worth writing a fresh post on using JFR with OpenJDK 11.
JFR is a profiling tool used to gather diagnostics and profiling data from a running Java application. Its performance overhead is negligible and that’s usually below 1 percent. For short running apps, this overhead might be above that because JFR requires some warm-up time on the start.
With the appearance of Java 8 lambdas, and even before that, a lot of APIs appeared that used the fluent API pattern (that started as a pattern explained in this post from Martin Fowler). To name a few of these API instances in Java includes:
There are a lot of articles about it on DZone and other sources:
In this article, we will discuss how to use method-level annotations
@PreDestroy to manage Spring bean lifecycle.
Note that the
@PreDestroy annotation does not belong to Spring, it’s located in the J2ee library – common-annotations.jar.
A software architect is a software technology expert who makes high-level design choices and dictates technical standards, including software coding standards, tools, and platforms. The leading expert is referred to as the chief architect. Their day-to-day tasks include:
A Java software architect should have knowledge in the following areas:
The following demonstrates how to sort a given map based on the value using Java 8. One important thing to keep in mind is that you cannot sort a map in the Hashmap object.
map.entrySet().stream().sorted((o1, o2) -> o1.getValue().compareTo(o2.getValue())).map(e -> e.getKey()).collect(Collectors.toList()).forEach(k -> sortedMap.put(k, map.get(k)));
Spring is a powerful, lightweight framework used for application development. In broader terms, you can say that the Spring framework is a well-defined tool that supports several web applications using Java as a programming language.
Before the launching of the framework in the IT Sector, the applications were developed using JEE standards. With these standards, we can deploy an application on any JEE application server. But, it had several problems, including:
I just cleared my OCAJP certification for Java SE 8 with an 88 percent score. Thanks to my friends for sharing good study materials for my success.
I have been a passionate learner of Java since my first year of college and my dream is to become a software architect. Though it is too early to think about this role, my hard work in design skills definitely will help me to achieve this goal.
I accepted the invitation to review Java by Comparison: Become a Java Craftsman in 70 Examples (2018, The Pragmatic Bookshelf) because the premise of this book interested me. The book Software Craftsmanship: The New Imperative had a significant effect on me when I was a less-experienced software developer, and I looked forward to reviewing Java by Comparison because of its connection with the concept of software craftsmanship and its examples being provided in the Java programming language. I was provided with the electronic version of the book and I chose the PDF format for my review.
Java by Comparison is written by Simon Harrer, Jörg Lenhard, and Linus Dietz and has over 160 pages of substantive content (not counting prefaces, forewords, table of contents, etc.). Java by Comparison features nine chapters and the 70 cases covered span the first eight of those nine chapters.
In this article, we will discuss how to refactor the observer design pattern using Java 8 lambda expressions.
Many existing object-oriented design patterns can be made redundant or written in a more concise way using lambda expressions. For example, if we have already implemented the observer design pattern in our projects using JDK 6 or 7, then you can refactor using lambda expressions.
Eclipse IDE gives the warning "The import XXX is never used" whenever it detects an unused import in a Java source file and shows a yellow underline. Though the unused import in a Java file does not create any harm, it’s unnecessary to increase the length and size of a Java source file, and if you have too many unused imports in your Java source file, those yellow underlines and Eclipse warnings affect the readability of your code. In my last post on Eclipse, we looked at some Java debugging tips on Eclipse. In this post, we will see an Eclipse shortcut to remove all unused imports in Eclipse. There are many options to tackle this problem, e.g. you can collapse the import section of code in Eclipse or you can altogether remove all unused imports from Java file, and we’ll see them in this short tutorial.
If you are completely new to Eclipse IDE, then I suggest you to first go through a comprehensive course, like The Eclipse Guided Tour: Part 1 and 2 from Pluralsight, which will teach you everything you need to know about Eclipse from the Java development point of view.
An interface cannot declare any of the methods of the object class as a default method. This restriction may be surprising, especially since the interface does not inherit from object.
Behind the scenes, an interface implicitly declares a public abstract method for most of the object’s method. As a consequence, there is no hierarchic relation between, for example, the object’s equals method and the equals method that is implicitly declared in an interface. So, why does Java prohibit us from declaring an object method as the default method?
In this article, we will learn the object-oriented design principles that can be applied in our day-to-day project work. It’s important to learn the basics of object-oriented programming, like abstraction, encapsulation, polymorphism, and inheritance. But, at the same time, it’s equally important to know object-oriented design principles, to create a clean and modular design. These guidelines help design strong object-oriented software design.
First, let’s list the important object-oriented design principles, and we will discuss each guideline in greater detail.
This guide will help you use Swagger with Spring Boot to document your RESTful services. We will learn how to expose automated Swagger documentation from your application. We will also add documentation to the REST API with swagger annotations.
The following screenshot shows the structure of the project we will create.
Hi, Spring fans! This week, I’m in sunny San Francisco for the epic SpringOne Tour San Francisco event. I’m head down and preparing for a large amount of travel ahead of me. Starting on the 14th, I’ll start four weeks of travel, bringing the message far-and-wide to Toronto, St. Petersburg, Brussels, San Francisco, Hartford, Paris, Beijing, Tokyo, Singapore, Seoul, Hong Kong, and… I forget… but, there’s more! I’m so looking forward to seeing you all at each and every one of those places!
I also loved this tweet from Pivotal:
Java lambda expressions were new in Java 8, profoundly enhancing the expressive power of Java. A Java lambda expression is an anonymous (that is, unnamed) method that can be created without belonging to any class. Instead, it is used to implement a method defined by a function interface (this interface contains one — and only one — abstract method but also can contain multiple default and static methods). Thus, a lambda expression results in a form of anonymous class.
Method reference is an important feature related to lambda expressions, which can let you reuse existing method definitions and pass them just like lambda expressions. It has the syntax form of:
This article focuses on applying some of the best practices during the functional testing of a Spring Boot application. We will demonstrate an advanced approach on how to test a service as a black box without setting up the staging environment. This article is the continuation of my previous post, Native Integration Testing in Spring Boot, so I will refer to it to show the difference between these two types of testing. I recommend taking a look at that before reading this post.
Let’s start with defining what functional testing means:
Sometimes, we are faced with a project that is both open source and part proprietary. Here’s how I address the challenges of synchronization amongst builds.
When faced with a partly open-source and partly closed-source project, it is common to use a Git sub-tree to synchronize them. The open-source project is added in a folder of the closed-source project, and all development happens in that root project (alias the mono-repo). On top of that, we want our build tool to handle the sub-tree project as it is a part of the mono-repo.
All SQL databases support the standard aggregate functions:
Some databases support other aggregate functions, like:
There has been significant news related to JDK 12 recently released.
JDK 12 Early Access Build 14 (2018/10/4) was released today. The release notes indicate that the fix for JDK-8210692 ["The ‘com.sun.awt.SecurityWarning’ class can be dropped"] is a significant part of this build. The class
com.sun.awt.SecurityWarning deprecated for removal with JDK 11 and has now been removed altogether. Other links of interest related to OpenJDK 12 Early Access Build 12 include "Changes in this build" and "issues addressed in this build." This build also includes, of course, the preview features switch expressions (JEP 325) and raw string literals (JEP 326) that I have blogged about previously.
Oracle, the owners of Java, says:
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.
Java (and many, if not most, other programming languages) provide an exception as a way to handle errors. When a severe error occurs, the program creates an object and "throws" it. It travels up the call stack until another object that is capable of handling the problem "catches" it. If no one catches it, the error can cause the thread, and even the whole program, to exit.
Stream library in Java 8 is one of the most powerful additions to the Java language ever. Once you start to understand its versatility and resulting code readability, your Java code-style will change forever. Instead of bloating your code with all the nitty and gritty details with
switch statements and numerous intermediate variables, you can use a
Stream that just contains a description of what to do, and not really how it is done.
Some years ago, we had to make an API decision for a Java project: which return type should we select for the two fast local in-memory data cache methods with;
Scalability of a cluster is often perceived as a matter of being able to scale out, but in practice the constraint of latency may be at least as important; it does not really help making a system future proof in terms of data size capacity if it turns out that handling an increasing amount of data will render the system intolerably slow. This post gives a high-level idea of the concept of sharding. We will elaborate on the principle in general, how it relates to affinity and speed and then present some code examples of sharding usage for low latency.
There are many problems that can be solved by dividing the problem into smaller subproblems and then assembling the subresults. In a cluster environment where several nodes cooperate to deliver a service, such a divide and conquer strategy, they may turn a single intractable problem into several smaller problems and therefore, solvable problems. If the size of the data is too large for any of the cluster nodes, the cluster may still be able to handle the service if the data can be partitioned between the nodes.
Kotlin is one of the most competitive JVM languages. It has been created by JetBrains and it keeps rising. Google recently endorsed Kotlin as an officially supported language for Android development, enhancing Kotlin’s profile, but it also applies to server-side development. For example, Kotlin works smoothly with Spring Boot.
The factory method pattern is one of the creational patterns of the well known “Gang of Four” design patterns. In the factory pattern, we can create objects by calling a factory method rather by calling a constructor. As a result, we don’t need to specify the class of the created object. It provides different approaches to code for interface rather than implementation, and it makes our code less coupled and more extendable.
Another IntelliJ IDEA 2018.3 EAP with more new goodies is now available for download! Let’s review what is inside the fresh EAP build!
First, the upcoming IntelliJ IDEA 2018.3 updates Git multi-repository projects significantly faster than before. Previously, the IDE could only fetch repositories one by one. Now, the IDE fetches all Git repositories in the project in parallel during the project update.
The producer-consumer problem is a classic example of a multi-process synchronization problem. For most of us, this problem is maybe the first synchronization problem that we studied back in school and were facing parallel algorithms for the first time. Simple as it is, it resumes the biggest challenge in parallel computing — the sharing of a single resource by multiple processes.
There are two processes, a producer and a consumer, that share a common buffer with a limited size. The producer “produces” data and stores it in the buffer, and the consumer “consumes” the data, removing it from the buffer. Having two processes that run in parallel, we need to make sure that the producer will not put new data in the buffer when the buffer is full and the consumer won’t try to remove data from the buffer if the buffer is empty.
Many of you are stuck with the question of which build system is best for your project?
It depends on many factors, including the size of your project, your need for customization, dependency handling, external dependency, and a few other variables that can help you choose. Let’s take a look.
As mentioned in the introduction article, Vert.x has a golden role: don’t block the event loop. To be honest, usually we work with synchronous APIs and many of the methods are likely to block. Hence, in some cases, we can’t avoid waiting for a result of our thread. For example, this includes when creating a database connection, writing data to disk, or executing a query, as well as many other situations. Well, does Vert.x have a solution for such situations?
Vert.x provides two ways to use traditional blocking APIs safely within a Vert.x application. One of these ways is the
executeBlocking method, which executes the blocking code and specifies a result handler to be called back asynchronous when the blocking code has been executed.
Writing JUnit tests can be a tedious and boring process. Learn how you can improve your tests classes using permutations in combination with
TestFactory methods and
DynamicTest objects with a minimum of coding effort.
In this article, I will use the Java stream ORM Speedment because it includes a ready-made
Permutation class and thereby helps me save development time. Speedment otherwise allows database tables to be connected to standard Java streams. Speedment is an open-source tool and is also available in a free version for commercial databases.
A HashMap (or hash table) is a data structure that maps keys to values for highly efficient lookup. There are a number of ways to implement this data structure. This post is about the simple implementation of HashMaps in Java using an array of a linked list.
So, let’s first define a class representing a node of a linked list as: