Feed aggregator

Value Objects

Javalobby Syndicated Feed - Tue, 11-Apr-17 21:01

Since I’ve already set you up in a DDD mood with the previous post, let’s capitalize on that further. In this post, I’ll try to provide an easy explanation of Value Objects and their benefits.

Motivation

Most of the concepts we’re modeling in our software have no global identity. If you wonder what I meant with the previous sentence, look inside your entity classes. Each of them contains a bunch of fields, usually represented by standard types such as String or BigDecimal, or by simple data structures. These, without the context of the enclosing entity, cannot be distinguished from other Strings, BigDecimals, or structures. We don’t identify them by ID of any kind; we identify them only by their values.

Categories: Java

A Look at Java 8's Supplier and Consumer Interfaces

Javalobby Syndicated Feed - Tue, 11-Apr-17 13:01

java.util.function.Supplier is a functional interface. As per the definition of functional interfaces, it has one abstract functional method T get().

Editing: “Supplier and Consumer Interface in Java8”

Javadoc Definition

Functional Interface: This is a functional interface and can, therefore, be used as the assignment target for a lambda expression or method reference. Instances of functional interfaces can be created with lambda expressions, method references, or constructor references.

Categories: Java

Kotlin for Front-End Developers

Javalobby Syndicated Feed - Tue, 11-Apr-17 09:01

I assume that readers are already familiar with the Kotlin language. Advertised from the site:

Statically typed programming language for the JVM, Android and the browser 100% interoperable with Java. 
— Headline from the Kotlin website

Categories: Java

Merging vs. Rebasing

Javalobby Syndicated Feed - Tue, 11-Apr-17 03:01

The git rebase command has a reputation for being magical Git voodoo that beginners should stay away from, but it can actually make life much easier for a development team when used with care. In this article, we’ll compare git rebase with the related git merge command and identify all of the potential opportunities to incorporate rebasing into the typical Git workflow.

Conceptual Overview

The first thing to understand about git rebase is that it solves the same problem as git merge. Both of these commands are designed to integrate changes from one branch into another branch—they just do it in very different ways.

Categories: Java

XFS: Possible Memory Allocation Deadlock in kmem_alloc

codecentric Blog - Tue, 11-Apr-17 02:00

A few weeks ago we were surprised by seemingly random I/O hangs on several virtual machines. Any attempt to write to their data volumes blocked, making the load average rise into the stratosphere, and — slightly more consequentially — make Elasticsearch or MongoDB freak out.

Looking at the hypervisor’s logs I noticed lots of these messages in dmesg and syslog:

...
Mar  5 22:42:57 node06 kernel: XFS: possible memory allocation deadlock in kmem_alloc (mode:0x250)
Mar  5 22:42:59 node06 kernel: XFS: possible memory allocation deadlock in kmem_alloc (mode:0x250)
...

This post describes the analysis, cause and remedy of the problem.

First Things First: A Quick Fix

The highest priority was to get the databases in the VMs running again. Google turned up a recommendation to drop the page cache on the hypervisor, because somehow the problem seemed to be memory fragmentation related and could be resolved this way at least temporarily:

% echo 1 > /proc/sys/vm/drop_caches

Indeed, doing that immediately made the VMs responsive again. In one case, Elasticsearch needed to be restarted inside the VM, but the cluster quickly recovered. However in the following days, the problem kept recurring, increasing in frequency and happening on several hypervisors.

Kernel Update to the Rescue (?)

I noticed we were running a kernel with a known problem in XFS, effectively it being more wasteful with memory than it needed to be for certain operations (see this 2013 XFS Mailing List Post). According to this Ubuntu Launchpad Issue that particular problem was fixed in a later Ubuntu kernel, so we installed the most recent one on the hypervisors. For a few days it seemed to fix the issue, because we did not see the hangs again. Unfortunately, it still came back, just a little later.

Intermediate measure: the xfs-guard

Doing more research I consistently came across the topic of memory fragmentation, not understanding why that could become a problem all of a sudden, because the machines had run with unchanged configurations, both inside the VMs and on the hypervisor, for several months. (It actually made sense after all, but at this point I had not yet understood the underlying mechanisms.) It turned out, dropping the page cache was not really necessary after all, dropping the slab cache (echo 2 > /proc/sys/vm/drop_caches) was enough to get the VMs responsive. I think dropping the page cache also worked via a side effect of freeing enough (unrelated) memory for more slab allocations to succeed.

Knowing that this worked, to buy me some time, I quickly threw together a small “xfs-guard” daemon and installed it on the hypervisors. Basically, all it does is tail the syslog constantly, look for the error message and drop the slab cache repeatedly until the message stops repeating. You can find it on Github and on Ansible Galaxy.

Once that was deployed (at not a moment too soon: it kicked in for the first time just a few hours after rolling it out) I had a little more time to find the root cause.

Digging deeper

Reading further, everything kept pointing towards file system fragmentation, and in turn (slab) memory fragmentation inside the kernel. I felt a bit reminiscent of the 90s, when running something like Norton Speed Disk to defragment FAT file systems was a pretty common task. I was reluctant to believe that this could still be a problem almost 30 years later, especially because XFS has a reputation for being pretty good at keeping fragmentation under control.

Categories: Agile, Java, TDD & BDD

All About Overriding in Java

Javalobby Syndicated Feed - Tue, 11-Apr-17 00:01

A child class can redefine the instance methods of its parent class. This is called method overriding. The signature (return type, parameter type, the number of parameters, and order of parameters) must be the same as defined in the parent class. Method overriding is done to achieve runtime polymorphism.

What Is Polymorphism?

Polymorphism allows you define one interface and have multiple implementations. This is one of the basic principles of object-oriented programming. The word ‘polymorphism’ literally means ‘a state of having many shapes’ or ‘the capacity to take on different forms.’ When applied to object-oriented programming languages like Java, it describes a language’s ability to process objects of various types and classes through a single, uniform interface.

Categories: Java

Java Garbage Collectors: When Will G1GC Force CMS Out?

Javalobby Syndicated Feed - Mon, 10-Apr-17 21:01

In JEPs proposed to target JDK 9 (2017/4/4), Mark Reinhold has written that JEP 291 ("Deprecate the Concurrent Mark Sweep (CMS) Garbage Collector") is one of two JEPs that "have been placed into the 'Proposed to Target' state by their owners after discussion and review." If things go well for JEP 291, it will be targeted for JDK 9.

Reinhold explains in this message why JEP 291 can still be targeted to JDK 9 at this relatively late date: "JEP 291 requires only a minuscule code change, to enable the proposed warning message to be issued. It's a JEP in the first place not because it's a risky change but to bring visibility to the plan to remove the CMS collector in the long term." As these sentences state, the JDK 9 targeted action is simply to mark the Concurrent Mark Sweep (CMS) collector as deprecated with the idea that it will be removed at some point "in the long term."

Categories: Java

Ratpacked: Conditionally Map or Flatmap a Promise

Javalobby Syndicated Feed - Mon, 10-Apr-17 13:01

When we want to transform a Promise value, we can use the map and flatMap methods. There are also variants to this method that will only transform a value when a given predicate is true: mapIf and flatMapIf. We provide a predicate and function to the methods. If the predicate is true the function is invoked, otherwise, the function is not invoked and the promised value is returned as is.

In the following example, we have two methods that use the mapIf and flatMapIf methods of the Promise class:

Categories: Java

Your First Application With Play and Scala

Javalobby Syndicated Feed - Mon, 10-Apr-17 09:01

Today we are going to develop a simple play application using Scala.

To do so, we must have sbt installed in our system. Once installed, we issue the command

Categories: Java

Try-With-Resource Enhancements in Java 9

Javalobby Syndicated Feed - Mon, 10-Apr-17 03:01

Try-with-resources was a great feature introduced in Java 7 to automatically manage resources using an AutoCloseable interface. This helps a lot, of course, as we have no need to close the resources explicitly in our code.

Java 7 Code

public void loadDataFromDB() throws SQLException {
    Connection dbCon = DriverManager.getConnection("url", "user", "password");
    try (ResultSet rs = dbCon.createStatement().executeQuery("select * from emp")) {
        while (rs.next()) {
            System.out.println("In loadDataFromDB() =====>>>>>>>>>>>> " + rs.getString(1));
        }
    } catch (SQLException e) {
        System.out.println("Exception occurs while reading the data from DB ->" + e.getMessage());
    } finally {
        if (null != dbCon)
            dbCon.close();
    }
}


Categories: Java

FileInputStream / FileOutputStream Considered Harmful

Javalobby Syndicated Feed - Mon, 10-Apr-17 00:01

Ok, so say you have been given an array of bytes that you have to write to a file. You’re a Java developer. You have been writing Java code for years. You got this:

public void writeToFile(String fileName, byte[] content) throws IOException {
    try (FileOutputStream os = new FileOutputStream(fileName)) {
        os.write(content);
    }
}


Categories: Java

The Garage Architecture

Javalobby Syndicated Feed - Sun, 09-Apr-17 21:01

After a couple of weeks break, we’re back to our architecture series. This time, we’ll look at something I’ve recently come up with – the Garage Architecture!

Background

I’ve been recently working on a pet project at home, playing with the all-cool Ionic 2 framework, and I came to a point where I wanted to create some sort of back-end for my app. For some reason, the mere thought of building a full-blown layered/hexagonal application made me feel bad. I was heavily thinking about using the DB directly with PouchDB or using a “backendless” solution like Firebase. To cut the storytelling before everyone leaves, I wondered why, as a “true” back-end developer, I even look towards those “fake” solutions and why I feel some sort of disgust for doing the work myself. My conclusion: pet projects require a different approach than enterprise ones!

Categories: Java

A Java EE Adoption Story From Luqman Saeed

Javalobby Syndicated Feed - Sat, 08-Apr-17 22:01

One of the most important things to do at this stage of the lifecycle of Java EE is highlighting successful adoption stories at a regular cadence. The community has been doing just that for a long time, including at JavaOne. A number of these stories are curated here

. Luqman Saeed recently agreed to share his Java EE adoption story. He has developed a number of production Java EE applications in his native Ghana. Luqman had a number of insightful things to say about Java EE worth paying attention to. I have highlighted specific passages that really stand out.

Categories: Java

Making Spring Web Services With Scala

Javalobby Syndicated Feed - Fri, 07-Apr-17 22:01

Spring is perhaps the most popular web development framework for the Java platform. Scala is a statically typed, functional programming language that runs on the JVM. Scala is highly interoperable with the Java language, so any Java lib can be used alongside Scala. Scala can be used along with Spring's ecosystem to build highly scalable, robust web applications.

The following shows how to create a Spring web application in Scala, Maven, Spring Boot
The code for this is available on GitHub.

Categories: Java

More Power to Interface in Java 9

Javalobby Syndicated Feed - Fri, 07-Apr-17 13:01

Interface is a very popular way to expose our APIs. Until Java 7 it was getting used as a contract (abstract method) where a child class was obliged to implement the contract; but after Java 8, Interface got more power — to where we can have static and default methods.

This guide will walk you through some Java 8 Interface enhancements.

Categories: Java

This Week in Spring: Spring Boot, Best Practices, and Events

Javalobby Syndicated Feed - Fri, 07-Apr-17 09:01

Welcome to another installment of This Week in Spring! This week I’m in London, UK; Paris, France (for the epic Devoxx France event where I’ll be speaking with my buddy Matt Raible) and Stuttgart, Germany, where I’ll be speaking at conferences and to customers! If you’re around, don’t hesitate to say, “Hi”!

Categories: Java

Clean Up the Leak!

Javalobby Syndicated Feed - Fri, 07-Apr-17 03:01

Spill

Even with the most sophisticated and thorough QA process, bugs will make it to your production systems. Even the most experienced developers will be at times confident about a certain release, only to then face the reality of a production environment, where things just don't work out the same way as they did during testing. This does not rate the quality of your code, it's a part of life — your customers and users can always surprise you. What's important is how you react to such incidents.

You may have very few bugs in your releases, but if the mitigation of these incidents is not satisfactory, then the user experience — which grades your work at the end of the day — will dip rapidly. This post does not aim to provide a swiss-knife (or swiss army knife) for every scenario, but addresses a class of problems that is far more frequent than it should be. I mean the infamous and regular problem known to Java application administrators as the out of memory error.

Categories: Java

Using Apache POI to Read Excel (Part 2)

Javalobby Syndicated Feed - Fri, 07-Apr-17 00:01

In part 1, we examined how to create an Excel spreadsheet and add data to it. We also looked at properly storing data into cells to avoid “Number Stored as Text” errors.

In this chapter, let us look at some more options for formatting data within an Excel spreadsheet.

Categories: Java

A Journey With Scala

Javalobby Syndicated Feed - Thu, 06-Apr-17 21:01

To those who are regular visitors of this blog, it is well-known that when it comes to developing code, I am a Spring/Java guy. Also, I use different technologies like Node or Python, but this depends largely on the project’s needs.

Due to some recent projects and courses involving Spark, stumbling upon Scala was inevitable. After some investigation, I decided to adopt it as one of my main tools, and there are plenty of reasons for that.

Categories: Java

Java Hashcode and Equals Deep Dive

Javalobby Syndicated Feed - Thu, 06-Apr-17 14:10

Hashcode and Equals

This article is part of Marcus Biel’s free Java 8 course focusing on clean code principles.

A PDF of this article is also available here.

Categories: Java

Thread Slivers eBook at Amazon

Syndicate content