Skip to content

Instantly share code, notes, and snippets.

View msievers's full-sized avatar

Michael Sievers msievers

View GitHub Profile
@msievers
msievers / [Spring Cloud, Zookeeper, Configuration] Setup Zookeeper configuration with Spring Boot and Spring Cloud.md
Created April 30, 2021 04:26
[Spring Cloud, Zookeeper, Configuration] Setup Zookeeper configuration with Spring Boot and Spring Cloud

Motivation

With latest release of Spring Cloud 2020.0 (aka Ilford) it's now easier than ever to setup Zookeeper configuration support, but the official documentation currnently lacks some straight-forward example.

Solution

Add the spring-cloud-dependencies bom to the dependencyManagement section of you pom.xml

<dependencyManagement>
@msievers
msievers / [Spring Boot, Kubernetes] Make a Spring Boot app expose apis on different ports utilizing HandlerInterceptor.md
Created February 15, 2019 10:03
[Spring Boot, Kubernetes] Make a Spring Boot app expose apis on different ports utilizing HandlerInterceptor

Motivation

Assume you have a Spring Boot application which exposes multiple apis, for example one for dogs and one for cats. Let's further assume that the api base paths look something like this

  • /api/cats/v1/
  • /api/dogs/v1/

Especially within kubernetes, it would be slick to be able to expose these apis using dedicated services. Let's recap a simple kubernetes service definition.

@msievers
msievers / [Spring Boot] Disable components for SpringBootTest tests by replacing them with mocks.md
Created February 15, 2019 09:01
[Spring Boot, Spock] Disable components for SpringBootTest tests by replacing them with mocks

Motivation

Annotating a test with @SpringBootTest will usally lead to initializing a Spring context with all components found by Springs component scan mechanism (usally all components of your app). Sometimes, you want some components to be excluded from component scan within your tests, for example

  • startup event listeners
  • setup code which configures external systems/apis/clients etc.
  • ...
@msievers
msievers / [Spring, JMH] Howto integrate JMH benchmarks with Spring.md
Last active March 23, 2024 21:12
[Spring, JMH] Howto integrate JMH benchmarks with Spring

Motivation

Integrate JMH (Java Microbenchmarking Harness) with Spring (Boot) and make developing and running benchmarks as easy and convinent as writing tests.

Idea

Wrap the necessary JMH boilerplate code within JUnit to benefit from all the existing test infrastructure Spring (Boot) provides. It should be as easy and convinent to write benchmarks as it is to write tests.

TL;DR;

@msievers
msievers / [jooq] Add generic support for Postgres json(b) columns using jacksons JsonNode.md
Last active May 31, 2023 01:36
[jooq] Add generic support for Postgres json(b) columns using Jackson JsonNode

Motivation

jooq does not support Postgres nativ json(b) datatypes out of the box. It would be nice have jooq recognize json columns and provide them in a generic way within generated records.

Idea

The official jooq documentation provides an example for creating a custom data type binding for Postgres json columns using GSON. Based on this, it is possible to tweak the implemenation to

@msievers
msievers / [Spring] Make @Async annotated methods run synchronously within (integration) tests.md
Last active October 25, 2018 07:48
[Spring] Make Async annotated methods run synchronously within (integration) tests

Motivation

If you are writing (integration) tests and you want to debug code within the scope of a method annotated with @Async (e.g. an event handler) your IDE might skip over breakpoints within the async code because it might be executed on a different/new thread.

Idea

Replace the default TaskExecutor with an instance of SyncTaskExecutor to ensure, that everything runs on the same thread.

@msievers
msievers / [jooq] Run jooq codegen without a database.md
Last active October 25, 2018 06:19
Run jooq codegen without a database

Motivation

In order to run jooqs codegen, it needs an appropriate database (with the correct schema) to be running. This often leads to checking in jooq generated code into your applications repository to ease the build process.

Idea

jooq provides an option to run the code generator based on flyway migrations without the need for a specific database to be running. The feature is described here as codegen from DDL.

How it works

@msievers
msievers / [ActiveRecord, SQL] Multi-model sql-only search with UNION
Last active October 25, 2018 05:54
A simple, sql only, multi model search
The idea is, to have models adhere to a convention, which stats, that there has to be a field "searchable_tokens",
which includes searchable tokens. Now, we can simply have a (virtual) model/table `search_results`, which can be
unioned with "casted selects" of each model we want to search. The casted selects make the columns of each model to fit
into the schema of the search result.
Mixed with ActiveRecord's polymorphic associations, the result is an ActiveRecord relation with elements, which points to
their respective subject.
@msievers
msievers / [Docker] overlay-docker-systemd.sh
Last active October 25, 2018 05:57 — forked from cpswan/overlay-docker-systemd.sh
Configure systemd to use overlay file system for Docker
sudo mkdir /etc/systemd/system/docker.service.d
sudo bash -c 'cat <<EOF > /etc/systemd/system/docker.service.d/overlay.conf
[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon -H fd:// --storage-driver=overlay
EOF'
sudo systemctl daemon-reload
sudo systemctl restart docker
@msievers
msievers / [Ruby] mri.rb
Last active October 25, 2018 05:58
Example for hijacking ruby via FFI and using native structs and functions
require "ffi"
module MRI
extend FFI::Library
ffi_lib [FFI::CURRENT_PROCESS, "ruby"]
attach_function :rb_str_resize, :rb_str_resize, [:pointer, :long], :pointer
def self.sizeof(type)
Class.new(FFI::Struct) do