Changing the Default Port of Spring Boot Apps [Snippets]

Javalobby Syndicated Feed - Mon, 19-Mar-18 13:01

By default, Spring Boot applications run on an embedded Tomcat via port 8080. In order to change the default port, you just need to modify the server.port attribute, which is automatically read at runtime by Spring Boot applications.

In this tutorial, we provide a few common ways of modifying the server.port attribute.

Categories: Java

Programming by Contract on the JVM

Javalobby Syndicated Feed - Mon, 19-Mar-18 03:01

This week, I’d like to tackle an interesting approach that I’ve rarely seen used, but is quite useful.

Design by contract, also known as contract programming, programming by contract and design-by-contract programming, is an approach for designing software. It prescribes that software designers should define formal, precise and verifiable interface specifications for software components, which extend the ordinary definition of abstract data types with preconditions, postconditions and invariants. These specifications are referred to as "contracts", in accordance with a conceptual metaphor with the conditions and obligations of business contracts.

In essence, conditions allow us to fail fast. It’s no use to run code if, at the end, a computation will fail because of a wrong assumption.

Categories: Java

A Personal Opinion on the Future of Jakarta EE

Javalobby Syndicated Feed - Mon, 19-Mar-18 00:01

The following is just my personal opinion of where Jakarta EE may be going. I am far removed from the decision makers and this opinion is based on what I have read and my experience as a teacher. Feel free to label me a crackpot.

I began to teach J2EE in 2002. It was a mess. Most of my time was spent on explaining the purpose of the numerous XML files and the three strange beans, session, session stateless, and EJB. Of course, this was in the days when it was thought that programs will be frequently reconfigured via their XML files and that pooling the strange beans was the state of the art. When I got a copy of Rod Johnson’s book J2EE Development without EJB, I thought that the concept was intriguing. However, I was required to teach what was most commonly used in industry, and at that time, it was J2EE.

Categories: Java

Testing Kotlin With Spock (Part 1): Object

Javalobby Syndicated Feed - Sun, 18-Mar-18 21:01

The object keyword in Kotlin creates a singleton in a very convenient way. It can be used, for example, as a state of an operation. Spock is one of the most expressive and readable test frameworks available in the Java ecosystem. Let's see how Kotlin's object can be used in Spock tests.

What Do We Want to Test?

We have a single method, validate, in our Validator interface that returns validation status: Ok or Error.

Categories: Java

Doing Stuff With Spring WebFlux

Javalobby Syndicated Feed - Sat, 17-Mar-18 22:01

Spring Boot 2.0 went GA recently, so I decided to write my first post about Spring in quite a while. Since the release, I have been seeing more and more mentions of Spring WebFlux along with tutorials on how to use it. But after reading through them and trying to get it working myself, I found it a bit hard to make the jump from the code included in the posts and tutorials I read to writing code that actually does something a tiny bit more interesting than returning a string from the back-end. Now, I’m hoping I’m not shooting myself in the foot by saying that, as you could probably make the same criticism of the code I use in this post, but here is my attempt to give a tutorial of Spring WebFlux that actually resembles something that you might use in the wild.

Before I continue, and after all this mentioning of WebFlux, what actually is it? Spring WebFlux is a fully non-blocking reactive alternative to Spring MVC. It allows better vertical scaling without increasing your hardware resources. Being reactive, it now makes use of Reactive Streams to allow asynchronous processing of data returned from calls to the server. This means we are going to see a lot less Lists, Collections, or even single objects. Instead, we'll see their reactive equivalents such as Flux and Mono (from Reactor). I’m not going to go in depth on what Reactive Streams are, as honestly, I need to look into it even more myself before I try to explain it to anyone. Instead, let's get back to focusing on WebFlux.

Categories: Java

Streaming Microservices With Akka Streams and Kafka Streams [Webinar]

Javalobby Syndicated Feed - Fri, 16-Mar-18 22:02

One of the most frequent questions that we at Lightbend have been asked is “what’s the difference between Akka Streams and Kafka Streams?” After all, there is only a 1 letter difference between these two technologies, so how different could they be?

Well, as we are about to learn, they are actually quite different. Both tools are part of Lightbend Fast Data Platform, but were created with entirely different technological approaches in mind. For example, Akka Streams emerged as a dataflow-centric abstraction for the Akka Actor model, designed for general-purpose microservices, very low-latency event processing, and supports a wider class of application problems and third-party integrations via Alpakka. Kafka Streams, by comparison, is purpose-built for reading data from Kafka topics, processing it, and writing the results to new topics in a Kafka-centric way.

Categories: Java

Building Spring Boot 2.0 Apps: VS Code, Initializr, and Azure Starters

Javalobby Syndicated Feed - Fri, 16-Mar-18 09:01

We are excited to announce full support of Spring Boot 2.0 on the Spring Boot Starters for Azure, already available on Spring Initializr. Plus, with new Java and Spring extensions for Visual Studio Code you can build production-ready apps and easily deploy them to the cloud.

Integrate Azure Services With Spring Boot 2.0 Apps

Spring Initializr enables developers to handle the dependency management and makes the bootstrapping process for Spring projects much easier. Today, we’re announcing that the Azure Starters have been updated to support Spring Boot 2.0, so you can easily integrate cloud services with your application.

Categories: Java

Using Reactive Streams, Akka Streams, Akka HTTP, and Alpakka For Legacy Integrations

Javalobby Syndicated Feed - Fri, 16-Mar-18 03:01

Software is turning more and more into individual services that provide value to individual consumers. It is a part of a greater whole, and in a typical enterprise, we see that multiple generations of technologies must co-exist for systems to function properly as a whole.

In addition to that, the need to integrate with existing systems is increasing. The technologies used can be WebServices, HTTP APIs, databases, or even external devices such as sensors.

Categories: Java

Creating an AWS Lambda Deployment JAR Using Maven

Javalobby Syndicated Feed - Fri, 16-Mar-18 00:01

One key aspect of AWS Lambda functions (with Java) is creating deployment packages (JAR or ZIP files) for uploading/deploying on the AWS Lambda service. In this post, you will learn different ways in which you can create a deployment JAR file for AWS Lambda projects using Maven. We will look at:

  • Deployment JAR using Maven and Eclipse IDE
  • Deployment JAR using Maven and the command prompt

I recommend using the "Maven and command prompt" technique for creating your deployment JAR package, but we will cover both methods.

Categories: Java

Duke Takes His Last Tumble

Javalobby Syndicated Feed - Thu, 15-Mar-18 21:01

When Java was first released, the world was a very different place. This was 1995, and the Internet and World Wide Web were still very much in their infancy. When James Gosling and John Gage demonstrated the WebRunner browser (as it was then called) running a Java applet that could rotate a molecule, the world changed. Suddenly, the web went from having only static content with hypertext links to a dynamic, interactive system.

From tiny acorns, mighty oak trees grow (if you’ll excuse the pun) and that’s precisely what we’ve seen with the web. Almost every aspect of our lives, from information access to banking to social interaction, now takes place through the Internet and the web. What we experience through the browser has changed beyond all recognition.

Categories: Java

Java Version Upgrades: GC Overview

Javalobby Syndicated Feed - Thu, 15-Mar-18 13:01

I'm guessing there are many companies on the verge of upgrading to a newer Java version because developers love to upgrade, but besides this adventurous attitude, there is also fear. The biggest fear of such a change is that applications can produce unexpected behaviors. In my opinion, the root of this fear is the GC system, the fear of the unknown. 

First, we will do a quick walkthrough of how memory management architecture looks, what kind of GC algorithms are available, and what GC types exist. We will then go over how to come up with a plan that is safe and can persuade management that switching to a newer Java version won't be the end of worlds. If you are pretty confident on how the GC works, you can skip to the end of the article where the TODOs are listed. For others, let's start with the architecture for the JVM.

Categories: Java

This Week in Spring: Even More Spring Boot 2

Javalobby Syndicated Feed - Thu, 15-Mar-18 09:01

Hi, Spring fans! Welcome to another installment of This Week in Spring! This week I’m in blizzard-besieged Boston, Massachusetts, for the epic Spring One Tour Boston event. Unfortunately, due to this crazy snow storm/blizzard, the event’s been postponed one day as we all grapple with the weather. Hope you were able to join the Spring Boot 2.0 launch webinar! If not the replay will be available here and don’t forget to check out the launch blog!

Snow or no snow! The show must go on, at least here on the Spring blog, so without further ado:

Categories: Java

Concurrent Java: Scalability, Deadlocks, and Garbage Creation

Javalobby Syndicated Feed - Thu, 15-Mar-18 08:54

On the example of three concurrent map implementations, we will see that we have low scalability, the risk of deadlocks, or unnecessary garbage creation. Locks lead to low scalability when we use only one — or risk deadlocks when using multiple locks. Compare and swap operations and immutable data structures, on the other hand, lead to unnecessary object creation, which causes more garbage collection cycles.

So what technique should you choose? If we have a small data structure, we can use an immutable class. And if our data structure is not performance critical, we can use a single lock. In all other cases, I would start with multiple locks, since it is easier to implement than compare and swap operations. But let us first start with the easiest case, a hash map synchronized with a single lock.

Categories: Java

Updates on JavaFX, Valhalla, Data Classes, and Java's Floating-Point

Javalobby Syndicated Feed - Thu, 15-Mar-18 00:01

There have been some interesting posts this and recent weeks that provide more insight into the future of Java and the JDK.

JavaFX Removed From JDK with JDK 11

In the blog post "The Future of JavaFX and Other Java Client Roadmap Updates," Oracle's Donald Smith announced that "starting with JDK 11, Oracle is making JavaFX easier to adopt by making the technology available as a separate download, decoupled from the JDK." Smith provides a brief history of JavaFX and discusses other motivations for the decision to decouple JavaFX from the JDK.

Categories: Java

Build a Microservices Architecture for Microbrews With Spring Boot

Javalobby Syndicated Feed - Wed, 14-Mar-18 21:01

Adopting a microservice architecture provides unique opportunities to add failover and resiliency to your systems, so your components can handle load spikes and errors gracefully. Microservices make change less expensive too. It can also be a good idea when you have a large team working on a single product. Your project can likely be broken up into components that can function independently of one another. Once components can function independently, they can be built, tested, and deployed independently. This gives an organization and its teams the agility to develop and deploy very quickly.

In a previous article, I showed you how to build a Spring Boot API with an Angular client. I then showed you how to convert the Angular app into a progressive web application that works offline. The Angular PWA is a good example of a resilient application because it still works when connectivity fails. Did you know you can develop similar resiliency in your API with Spring Boot, Spring Cloud, and a microservices architecture? This article shows you how to convert the previously created Spring Boot application to use microservices. You’ll create a beer catalog service, an edge service (for filter and displaying good beers), and a Eureka service that registers the services and allows them to communicate with one another.

Categories: Java

How Synchronization Works in Java (Part 2)

Javalobby Syndicated Feed - Wed, 14-Mar-18 13:01

In my previous article, we saw how synchronized blocks execute in Java. We did some debugging in Eclipse IDE as well to understand the flow of execution clearly.

This article focuses more on synchronized methods in Java, so let's begin.

Categories: Java

Java Out of Memory Heap Analysis

Javalobby Syndicated Feed - Wed, 14-Mar-18 09:01

Any software developer who has worked with Java-based enterprise-class backend applications would have run into this infamous or awkward error from a customer or QA engineer: java.lang.OutOfMemoryError: Java heap space.

To understand this, we have to go back to the computer science fundamentals of the complexity of algorithms, specifically "space" complexity. If we recall, every application has a worst-case performance. Specifically, in the memory dimension, when this is unpredictable or is spiky, more than the recommended memory is being allocated to the application. That leads to an over-usage of the heap memory allocated and hence an "out of memory" condition.

Categories: Java

Hello OpenJ9 on Windows, I Didn’t Expect You so Soon!

Javalobby Syndicated Feed - Wed, 14-Mar-18 03:01

Faster startup time, lower memory footprint, and higher application throughput only by replacing the Java Virtual Machine? That sounds too good to be true. So far, there has been no real alternative to Oracle’s Java HotSpot VM on Windows. With Eclipse OpenJ9, which emerged from open-sourcing IBM’s J9 VM, there is now the alternative that promises exactly this.

At the end of January, the first OpenJDK 9 with Eclipse OpenJ9 nightly builds for Windows were published, but they were not very stable at that time. This week, I tested the nightly builds again to run the Eclipse IDE and I was pleasantly surprised: OpenJ9 ran without crashing. Here are my results: the start time of the Eclipse Oxygen.2 Java IDE improves with OpenJ9 from 20 to 17 seconds, with some tuning (see below) even to 12 seconds compared to the Java 9 JDK with Oracle’s HotSpot VM on my more than six-year-old laptop. Also, the Windows Task Manager shows less memory used by the Eclipse IDE and tasks like compiling a large project are a bit faster with OpenJ9.

Categories: Java

Fluent Assertions With AssertJ

Javalobby Syndicated Feed - Wed, 14-Mar-18 00:01

I recently gave a presentation to my Keyhole team members about JUnit 5. I started off the presentation by covering the importance of automated testing, how lack of automated testing affects an organization's ability to deliver code to production, and how without automated testing you are building legacy.

I pointed out two key benefits of automated testing: confidence you are fixing what you set out to fix, and confidence you are not introducing a new bug. A co-worker however pointed out an important third benefit of automated testing: providing living documentation.

Categories: Java

Upgrading to Spring Boot 2

Javalobby Syndicated Feed - Tue, 13-Mar-18 21:01

Spring Boot 2 was released recently and the production instance of Spring Initializr ( was upgraded to Spring Boot 2 the same day.

In this post, I’d like to walk you through the process of upgrading a Spring Boot 1.x app to Spring Boot 2.

Categories: Java

Thread Slivers eBook at Amazon

Syndicate content