Java Bean Mapping with MapStruct

MapStruct is a nice tool to generate mappers for converting one Java bean into another e.g. for projections, data-transfer-objects and so on …​ As long as fields in source and target beans do match, the mapper is able to generate the data setting automatically .. else we may specify which source fields to map into which target fields or to register custom converters with ease. Using Maven, we need to add dependencies and plugin integration to our pom.xml: ...

March 31, 2022 · 3 min · 459 words · Micha Kops

Terminal Based Progress Bar for Java Applications

Recently I needed to add a progress bar to a Java based terminal/console application and I used a specific library that I’d like to demonstrate in the following snippet. Figure 1. Terminal based Progress Bar for Java Dependencies Using Maven, we just need to add the following dependency: pom.xml <dependency> <groupId>me.tongfei</groupId> <artifactId>progressbar</artifactId> <version>0.7.3</version> </dependency> Sample Application The application shows how to set up the progress bar and how to advance its status by-one, by a given amount or to a specific number. ...

March 31, 2019 · 2 min · 242 words · Micha Kops

Annotation based Kubernetes and Openshift Manifests for Java Applications with ap4k

Writing our manifest files for Kubernetes / Openshift often forces us to edit xml, json and yml files by hand. A new library, ap4k allows to specify metadata for these manifest files directly in our Java code using annotations. In the following short example I am going to demonstrate how to generate manifest files using Maven and ap4k. Figure 1. ap4k Tutorial Dependencies Using Maven we just need to add the following one dependency to our project’s pom.xml ...

February 28, 2019 · 5 min · 898 words · Micha Kops

Analyzing Java Applications on the Fly with Arthas

Arthas created by Alibaba is a tool that allows developers to connect to running Java applications without stopping them or suspending threads for debugging the application from the console. It offers features like monitoring invocation statistics, searching for classes and methods in the classloaders, view method invocation details (like parameters), show the stack trace of a method invocation, monitor system metrics and others. In the following examples I’m going to demonstrate some of these features applied to a running web application. ...

October 31, 2018 · 8 min · 1525 words · Micha Kops

AWS Snippets

Install AWS CLI v2 $ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64-2.0.30.zip" -o "awscliv2.zip" unzip awscliv2.zip sudo ./aws/install AWS Documentation Generate Signed URLs with Linux Tools e.g. for accessing a website behind a CloudFront distribution using a canned policy. Write the policy file policy { "Statement": [ { "Resource": "https://xxxxxxxxxxxx.cloudfront.net/", "Condition": { "DateLessThan": { "AWS:EpochTime": 1648293147 } } } ] } Then apply the following commands[1] - you need to have OpenSSL installed. cat policy | tr -d "\n" | (1) tr -d " \t\n\r" | (2) openssl sha1 -sign private_key.pem | (3) openssl base64 -A | (4) tr -- '+=/' '-_~' (5) ...

March 1, 2018 · 2 min · 371 words · Micha Kops

Microbenchmarks with JMH / Java Microbenchmark Harness

Writing microbenchmarks for parts of our applications is not always easy – especially when the internals of the virtual machine, the just-in-time-compiler and such things are coming into effect. Java Microbenchmark Harness is a tool that takes care of creating JVM warmup-cycles, handling benchmark-input-parameters and running benchmarks as isolated processes etc. Now following a few short examples for writing microbenchmarks with JMH. Figure 1. Java JMH Microbenchmarks running in IntelliJ...

October 2, 2017 · 8 min · 1578 words · Micha Kops

Object Graph Mapping by Example with Neo4j OGM and Java

When integrating a Neo4j database into a Java application a developer often needs to map nodes and edges of the graph to corresponding Java classes of the domain model. Neo4j OGM eases this work and allows us to map our domain objects to the graph database using simple annotations – similar to the Java Persistence API (JPA) for relational database management systems. In the following tutorial I’d like to demonstrate how to use Neo4j OGM to build a simple train timetable planner and a permission system mapping between graph, nodes, edges and POJOs. ...

July 18, 2016 · 9 min · 1828 words · Micha Kops

Finding Memory Leaks using Eclipse and the MemoryAnalyzer Plugin

The MemoryAnalyzer Plugin for Eclipse allows us to quickly analyze heap dumps from a virtual machine and search for memory leaks. In the following tutorial we’re going to create and run a small application that is going to cause an OutOfMemoryException during its runtime. In addition, we’re forcing the virtual machine to save a heap dump and finally analyzing this data using Eclipse and the MemoryAnalyzer plugin. Prerequisites Java Development Kit 6 Eclipse Indigo ...

November 2, 2011 · 4 min · 704 words · Micha Kops

Eclipse Snippets

Favorites Spare my time when using static imports .. Window > Preferences > Java > Editor > Content Assist > Favorites: com.google.common.collect.Lists com.jayway.restassured.matcher.RestAssuredMatchers com.jayway.restassured.RestAssured io.restassured.matcher.RestAssuredMatchers io.restassured.RestAssured org.hamcrest.MatcherAssert org.hamcrest.Matchers org.mockito.Mockito javaslang.API javaslang.Predicates Template to insert a static logger instance Go Windows > Preferences > Java > Editor > Templates > New … Enter logger as name and as template: ${:import(org.slf4j.Logger,org.slf4j.LoggerFactory)} private static final Logger LOG = LoggerFactory.getLogger(${enclosing_type}.class); Afterwards you’re able to type logger in your code and ctrl+space gives the option to insert the logger ...

March 1, 2010 · 1 min · 101 words · Micha Kops

Firefox Snippets

Configure address bar to return search results Enter about:config in the address bar Search for the key keyword.url Modify the value for your search engine of choice .. e.g. for the google search: http://www.google.com/search?q=

March 1, 2010 · 1 min · 34 words · Micha Kops