Feed aggregator

How to Find and Fix Memory Leaks in Your Java Application

Javalobby Syndicated Feed - 2 hours 53 min ago

Do you have a Java application that runs fine at first but slows down after a while, or it runs fine for a small number of files but performance degrades for a large number of files? Maybe you have a memory leak.

When fixing memory leaks, if someone were to ask me: "If you knew back then what you know now, what would you tell yourself?" Well, I would say...

Categories: Java

Introducing Combinators – Part 1

Javalobby Syndicated Feed - 5 hours 53 min ago

Outline

In this article, we present a few basic functional constructs that every developer should be aware of. We also introduce the concept of combinator, which is of key importance to write useful functional code, though we carefully avoid digging into the intricacies of combinatory logic.

Background

Have you ever had the need to execute the same code in many places? Have you ever copy-pasted this same code over here and there, because you didn't have time to develop a more robust solution? Or maybe you are a more meticulous programmer and have never done that. Instead, you've created a method with that piece of code and have invoked it from wherever you needed to do it...

Categories: Java

Creating a Spring Boot Project With Eclipse and Maven

Javalobby Syndicated Feed - 9 hours 52 min ago

Setting up a basic project with Spring Boot is a cake walk. In this article, we will explore the different options of creating Spring Boot projects with Maven and Eclipse. You will learn:

  • How to bootstrap a simple project with Spring Initializr.
  • How to use the Spring Starter Eclipse Plugin to create a simple project with Spring Boot, Maven, and Eclipse.
  • How to create a Spring Boot project manually, step-by-step.

Video References

Tools You Will Need

  • Maven 3.0+ is your build tool
  • Your favorite IDE (we use Eclipse in this article)
  • JDK 1.8+

Introduction to Maven

Defining what Maven does is very difficult. To help explain, let's consider some of the things that a developer does every day...

Categories: Java

How to Create an Immutable Class in Java

Javalobby Syndicated Feed - 14 hours 53 min ago

An object is immutable if its state cannot change after construction. Immutable objects don’t expose any way for other objects to modify their state; the object’s fields are initialized only once inside the constructor and never change again.

In this article, we'll define the typical steps for creating an immutable class in Java and also shed light on the common mistakes which are made by developers while creating immutable classes.

Categories: Java

SRP in Object-Oriented Design

Javalobby Syndicated Feed - Mon, 11-Dec-17 14:01

In object-oriented programming, there are five basic principles (SOLID) that, properly applied, make the difference between good and bad design. They're the difference between an application that is easy to maintain and one that is not. The difference between a good developer and a bad one. Today, I would like to concentrate on the first principle in object-oriented design (the Single Responsibility Principle) but first, let’s mention the five SOLID principles:

  • Single Responsibility Principle
  • Open/Closed Principle
  • Liskov Substitution Principle
  • Interface Segregation Principle
  • Dependency Inversion Principle 

Single Responsibility Principle

We’re going to start with the first one, the Single Responsibility Principle (SRP) that is defined by Robert C. Martin in his book “Agile Software Development, Principles, Patterns, and Practices”. The principle is actually a very simple concept to explain, but it can be difficult to implement. As its name suggests, it implies that a class or module must have a unique responsibility.

Categories: Java

Java Annotated Monthly: December 2017

Javalobby Syndicated Feed - Mon, 11-Dec-17 10:01

While Java Annotated Monthly is usually brought to you by our colleague Trisha Gee, for the next few months, I’ll be your host, as Trisha is away on maternity leave. And from all of us here, congratulations to her and her family!

Due to multiple releases and important conferences, November is probably the hottest time of the year for us at JetBrains. Finally, now when most of it is over, it’s time to compile the latest news into one piece.

Categories: Java

Tail Recursion in Scala [Video]

Javalobby Syndicated Feed - Mon, 11-Dec-17 04:01

Recursion is quite common in the programming world. As you probably know, it's the process of solving a problem by breaking it down into smaller subproblems. You can easily spot recursion if you see a method calling itself with a smaller subset of inputs.

Why Recursion?

Many programmers consider recursion tough and error-prone, but in Scala, we use recursion because:

Categories: Java

10 Talented Women in the Java/JVM Community

Javalobby Syndicated Feed - Mon, 11-Dec-17 01:01

A couple of weeks ago, Duchess, a global organization for women in Java technology, celebrated its 10th anniversary:

This got me thinking of the women in the Java/JVM community from whom I have learned a lot, whether through their books, courses, or presentations.

Categories: Java

Developing modern offline apps with ReactJS, Redux and Electron – Part 4 – Electron

codecentric Blog - Sun, 10-Dec-17 23:00

The previous part of this series showed the beautiful interplay of React and Redux. In this part, we are going to take a rough look at a technology called Electron. One essential technology in our recent projects, Electron is vastly different from the previous two parts of this blog series. React and Redux are solely used to implement the application logic. Electron, on the other hand, is used to implement both structure and application logic to create real cross-platform desktop apps. It is a wrapper which contains a chromium browser in a NodeJS environment. This technology enables the combination of pure web frontend technologies and additionally gives your application full access to the underlying operating system via NodeJS. In the following, we will introduce the basic concepts using a simple Electron app and show how this technology solves the everlasting single-threaded obstacle of non-responsive JavaScript applications.

  1. Introduction
  2. ReactJS
  3. ReactJS + Redux
  4. Electron framework
  5. ES5 vs. ES6 vs. TypeScript
  6. WebPack
  7. Build, test and release process

The Core Parts

An Electron app consists of a few main parts. The basic concept is that you have two or more concurrently running processes. First you have the main process of your application. In this process you have access to NodeJS and thus all your operating system’s power and access to a huge distinct subset of the Electron API. Furthermore the main process creates browser windows. They have one or more render processes and share an important property with your normal browser. These processes are contained in a sandbox. This is because these processes are responsible for rendering the DOM of our web app. Render processes have access to the NodeJS API and a distinct subset of the Electron API, but not to the operating system.

A few functionalities of Electron can even be used in both the main and a render processes. By default JavaScript processes in NodeJS and Chromium are single-threaded and therefore still limited, even if both processes are operating system level processes.

Electron core parts Electron core parts

OS Integration

Since Electron is a JavaScript technology, the final app can be deployed to common desktop operating systems like Windows, MacOS and Linux in 32 and 64-bit versions. To do so, you can use the electron-packager, which is developed by the community. The packager creates installers for various operating systems which make it easy to deploy the Electron apps in enterprise environments. Furthermore, Electron provides essential OS integration on its ownm, menu bars, OS level notifications, file dialogs and many other features for nearly all operating systems.

In our projects we used the file dialog to import files from the file system. The allowed properties depend on the operating system. Please check out the API for more details [DIALOG].

const {dialog} = require('electron');
const properties = ['openFile', 'openDirectory’];
dialog.showOpenDialog({ properties });

We also created custom Electron menu bars for production and development mode. During development we could toggle the developer tools from chromium. For production you can remove that feature from the final Electron app.

 const createMenu = () => {
 const { app, Menu } = electron;
 const template = [
   {
     label: 'Edit',
     submenu: [ 
      { role: 'cut' }, 
      { role: 'copy' }, 
      { role: 'paste' },
      { role: 'selectall' }
    ]
   },
   {
     label: 'View',
     submenu: [ 
      { role: 'reload' },
      { role: 'forcereload' },  
      { role: 'toggledevtools' }
     ]
   }
 ];
 const menu = Menu.buildFromTemplate(template);
 Menu.setApplicationMenu(menu);
};

Electron native menu

To see a full list of all native Electron features, go to [ELECTRON].

IPC Communication

In the previous section we talked about the awesome OS integration of Electron. But how can we harness the full potential of our operating system and backend languages like NodeJS to unleash the power of JavaScript? We can do this with the built-in inter-process-communication in Electron. The modules that handle that communication, the ipcMain and ipcRenderer, are part of Electron’s core. ipcMain enables communication from the main process to the render processes. The ipcRenderer handles the opposite direction from render to main.

“The ipcRenderer module is an instance of the EventEmitter class. It provides a few methods so you can send synchronous and asynchronous messages from the render process (web page) to the main process. You can also receive replies from the main process.” [IPCRENDERER]

In the following example, we register an Event Listener with ipcMain process using the channel name LOAD_FILE_WITH_PATH. Once the Event Listener finishes, we send an event back to the React app. Depending on the result, we add a “success” or “error” to the channel name. This allows us to operate differently with the response inside React [IPCMAIN].

In the React app, we use the ipcRenderer.send to send messages asynchronously to the Event Listener, using the identical channel name. To send messages synchronously use ipcRenderer.sendSync. After that we add a one time listener function for the event using ipc.once. To distinguish IPC calls we add a unique uuid to the Channel name [IPCRENDERER].

electron.js
const ipc = require('electron').ipcMain;
ipc.on(ipcConstants.LOAD_FILE_WITH_PATH, async (event, request) => {
  try {
    const fileContent = await fileService.readFileAsync(request.path);
    event.sender.send(
      `${ipcConstants.LOAD_FILE_WITH_PATH}-success-${request.uuid}`, fileContent);
  } catch (error) {
    event.sender.send(
      `${ipcConstants.LOAD_FILE_WITH_PATH}-error-${request.uuid}`, error.message);
  }
});
fileService.js
const ipc = require('electron').ipcRenderer;
export function readFileContentFromFileSystem(path) {
  const uuid = uuidV4();
  ipc.send(LOAD_FILE_WITH_PATH, { uuid, path });
  return new Promise((resolve, reject) => {
    ipc.once(`${LOAD_FILE_WITH_PATH}-success-${uuid}`,
      (event, xml) => {
        resolve(xml);
      });
    ipc.once(`${LOAD_FILE_WITH_PATH}-error-${uuid}`,
      (event, args) => {
        reject(args);
      });
  });
}

To debug the IPC communication between your React application and Electron, you need to install the Electron DevTools Extension.

npm install --save-dev devtron

Afterwards run the following command from the console tab of your application. This will add another tab with the Devtron tools.

require('devtron').install()

Under the Devtron tab you get all kinds of details about your Electron application. Devtron displays all default event listeners from Electron as well as your own custom listeners. Under the IPC link you can record all IPC calls from your application. The Lint tab allows you to do Lint checks and the Accessibility tab checks your web application against the Accessible Rich Internet Applications Suite (ARIA) standard.

Devtron event listener

Here is an example what the IPC communication in our project looks like.

Devtron IPC call

Remember that we claimed that Electron is the end of the everlasting single-threaded obstacle? Using IPC we can move CPU intensive work to Electron and outsource these tasks using electron-remote. With one single line we can create a task pool that will actually create a new browser window in the background and execute our code (electronFileService.js) in a separate OS process / browser window. Here is an example how to setup the task pool for the file service.

const { requireTaskPool } = require('electron-remote');
const fileService = requireTaskPool(require.resolve('./electronFileService'));

Offline and Storage

When developing an offline desktop application with Electron you have several options on where to store and read data from.

Option 1: Electron / NodeJS

In Electron you can execute NodeJS commands. Therefore you can use almost any module from npmjs.org to read and store data on your local operating system. We recommend this option when you need to persist and process a lot of data.

  • SQLite3 (relational database)[SQLITE]
  • MongoDB (document database)[MONGODB]
  • Neo4J (graph database)[NEO4J]

Electron app

Option 2: React & Redux / Web Browser

In the second option we persist and process data inside the browser. Modern browsers offer a range of APIs that allow for persisting browser data, i.e. LocalStorage, IndexedDB, SessionStorage, WebSQL and Cookies. We recommend this approach for small datasets that need to be persisted locally. This can be done with any web technology. In our case, the React web application uses Redux as a store for the application state. You can use the redux-persist module to automatically persist the Redux store to the IndexedDB or LocalStorage. In case your web app crashes or you restart the browser, you can configure redux-persist [REDUXP] to automatically rehydrate the Redux Store.

React WebApp

Modern browsers support service worker API to span threads for processing data. If there is information that you need to persist and reuse across restarts, service workers have access to the various browser storage technologies.

Option 3: Combination of Option 1 and 2

There might be times when your desktop client will be online and can retrieve data from a backend server. With our proposed stack you have the full freedom of choosing how to access the backend services. You can either call the backend services via the web application layer (i.e. React WebApp) or you can use the Electron/NodeJS layer. Which way you choose is up to you and might depend on security restrictions or the existence of NodeJS modules you can reuse or other aspects.

Electron React App

Summary

Electron is an extremely powerful technology that enables you and your team to create beautiful, responsive, OS independent and maintainable desktop applications. Because there is so much more to Electron, we highly recommend reading https://electronjs.org/docs for the parts that you are interested in or need in your projects. Just keep tuned for our next article.

References

The post Developing modern offline apps with ReactJS, Redux and Electron – Part 4 – Electron appeared first on codecentric AG Blog.

Categories: Agile, Java, TDD & BDD

Automatic-Module-Name: Calling All Java Library Maintainers

Javalobby Syndicated Feed - Sun, 10-Dec-17 22:01

Creating modular applications using the Java module system is an enticing prospect. Modules have module descriptors in the form of module-info.java, declaring which packages are exported and what dependencies it has on other modules. This means we finally have explicit dependencies between modules at the language level and can strongly encapsulate code within these modules. The book Java 9 Modularity (O'Reilly) written by Paul Bakker and me explains these mechanisms and their benefits in detail.

That, however, is not what this post is about. Today, we'll talk about what needs to be done to move the Java library ecosystem toward modules. In the ideal world where all libraries have module descriptors, all is well.

Categories: Java

Memory Leaks: Fallacies and Misconceptions

Javalobby Syndicated Feed - Sat, 09-Dec-17 23:01

In the years we have spent building Plumbr, we have detected and solved so many memory leaks that I have actually lost count. Interestingly, during these years, we have encountered even more situations where a memory leak was nowhere in sight, but somehow our users were convinced that there had to be one. The pressure from such users has been high enough for us to even come up with a specific term that we use internally: “memory anxiety.”

With Java memory management being a complex domain, I do understand the background of this anxiety. When your software does not perform the way it should, the current state of root cause detection forces you to apply different kinds of dark arts to really understand what is going on. Often enough, the process involves a lot of guesswork. One of the frequent guesses seems to often take the form of, “Gosh, I have a memory leak”. In this post, I would like to give some examples of different situations and suggest patterns that you can follow to verify whether or not you actually are a victim of a memory leak.

Categories: Java

Spring 5, Embedded Tomcat 8, and Gradle

Javalobby Syndicated Feed - Fri, 08-Dec-17 22:01

In this article, we are going to learn how to use Gradle to structure a Spring 5 project with Tomcat 8 embedded. We will start from an empty directory and will analyze each step needed to create an application that is distributed as an über/fat jar. This GitHub repository contains a branch called complete with the final code that we will have after following the steps described here.

Why Spring

Spring is the most popular framework available for the Java platform. Developers using Spring can count on a huge, thriving community that is always ready to help. For example, the framework contains more than 11k forks on GitHub and more than 120k questions asked on StackOverflow are related to it. Besides that, Spring provides extensive and up-to-date documentation that covers the inner workings of the framework.

Categories: Java

After 10 Years, Effective Java 3rd Edition Is Coming Soon

Javalobby Syndicated Feed - Fri, 08-Dec-17 14:01

Hello guys, I have an interesting news to share with you today. After a long wait of almost 10 years, Effective Java 3rd edition is finally coming this year.

The Effective Java 2nd Edition was released in May 2008 and updated for Java SE 6, but it has been a good 10 years now and there is a lot of interest from Java developers around the world for Effective Java 3rd edition, especially after Java SE 8's release. I am very happy to inform you all that, finally, all our wishes have been granted and Effective Java 3rd edition is set to arrive this year.

Categories: Java

This Week in Spring: Tool Suites and Cloud Gateways

Javalobby Syndicated Feed - Fri, 08-Dec-17 10:01

Hi, Spring fans and welcome to This Week in Spring from the premier JVM-language event SpringOne Platform 2017! There is a massive amount of stuff to cover, especially in light of SpringOne Platform, so let’s get to it!

Categories: Java

Refer to a Connector Configuration From a Java Component [Snippet]

Javalobby Syndicated Feed - Fri, 08-Dec-17 04:01

When using a Java component to send a message to a VM inbound endpoint, if there is more than one connector configuration defined, then it is necessary to specify the connector reference to use. Otherwise, an error similar to the one below will be shown:

org.mule.transport.service.TransportFactoryException: 
There are at least 2 connectors matching protocol "vm", 
so the connector to use must be specified on the endpoint 
using the 'connector' property/attribute. Connectors in your 
configuration that support "vm" are: VM-1, VM-2, 
(java.lang.IllegalStateException). Component that caused 
exception is: DefaultJavaComponent{vm-javaFlow.component.2140635066} 


Categories: Java

Choosing the Right GC

Javalobby Syndicated Feed - Fri, 08-Dec-17 01:01

Size matters when it comes to software. It has become clear that using small pieces within a microservices architecture delivers more advantages compared to the big monolith approach. The recent Java release of Jigsaw helps decompose legacy applications or build new cloud-native apps from scratch.

This approach reduces disk space, build time, and startup time. However, it doesn’t help enough with RAM usage management. It is well-known that Java consumes a large amount of memory in many cases. At the same time, many have not noticed that Java has become much more flexible in terms of memory usage and provided features to meet the requirements of microservices.

Categories: Java

The Power of the Gradle Kotlin DSL

Javalobby Syndicated Feed - Thu, 07-Dec-17 22:01

The following is based on Gradle 4.3.1.

A few weeks ago, I started migrating most of my Groovy-based gradle.build scripts to Kotlin-backed gradle.build.kts scripts using the Kotlin DSL.

Categories: Java

Scala: The Option Type (Part 1)

Javalobby Syndicated Feed - Thu, 07-Dec-17 14:01

Developers familiar with Java would have experienced NullPointerExceptions at some point. It is mainly used to indicate that no value or null is assigned to a reference variable or an Object.

Different languages treat null in different ways. Scala tries to solve the problem of nulls by getting rid of null values altogether and by providing a type to represent an optional/unknown value, i.e. Option[Employee].

Categories: Java

Uncommon Java Syntax: Ellipses…

Javalobby Syndicated Feed - Thu, 07-Dec-17 10:01

Existing since Java SE 5.0 the ellipsis, also known as varargs, is one of those rarely underutilized features of Java. My guess is many novice programmers, and indeed even some experienced ones, have yet to meet Mr. Ellipsis — "…". I for one didn’t come across this elegant feature until after a year of full-time programming with Java. So what is an ellipsis?

Defining Varargs

I could not find any clear definition from the Javadocs but from what I could gather online, ellipses (also officially known as varargs (Variable Arguments)) are a Java syntax that describes an argument in a method that can take in zero or many arguments. Confusing? Let’s look at an example.

Categories: Java

Java Quiz 6: Calling Constructors by Using the Keyword This

Javalobby Syndicated Feed - Thu, 07-Dec-17 07:01

Before we start with this week's quiz, here is the answer to Java Puzzle 5: Static Variables and Object Instantiation.

  1. We actually need to invoke the method intMethod to assign a value to the variable i and invoke the strMethod to assign a value to the object str.

Categories: Java

Thread Slivers eBook at Amazon

Syndicate content