Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Sievers msievers

View GitHub Profile
@msievers
msievers / [Spring Boot, Kubernetes] Make a Spring Boot app expose apis on different ports utilizing HandlerInterceptor.md
Created Feb 15, 2019
[Spring Boot, Kubernetes] Make a Spring Boot app expose apis on different ports utilizing HandlerInterceptor
View [Spring Boot, Kubernetes] Make a Spring Boot app expose apis on different ports utilizing HandlerInterceptor.md

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 Feb 15, 2019
[Spring Boot, Spock] Disable components for SpringBootTest tests by replacing them with mocks
View [Spring Boot] Disable components for SpringBootTest tests by replacing them with mocks.md

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 Aug 13, 2020
[Spring, JMH] Howto integrate JMH benchmarks with Spring
View [Spring, JMH] Howto integrate JMH benchmarks with Spring.md

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 Oct 25, 2018
[jooq] Add generic support for Postgres json(b) columns using Jackson JsonNode
View [jooq] Add generic support for Postgres json(b) columns using jacksons JsonNode.md

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 Oct 25, 2018
[Spring] Make Async annotated methods run synchronously within (integration) tests
View [Spring] Make @Async annotated methods run synchronously within (integration) tests.md

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 Oct 25, 2018
Run jooq codegen without a database
View [jooq] Run jooq codegen without a database.md

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

View [ActiveRecord, SQL] Multi-model sql-only search with UNION
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 Oct 25, 2018 — forked from cpswan/overlay-docker-systemd.sh
Configure systemd to use overlay file system for Docker
View [Docker] overlay-docker-systemd.sh
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 Oct 25, 2018
Example for hijacking ruby via FFI and using native structs and functions
View [Ruby] mri.rb
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
@msievers
msievers / ApplicationHelper.rb
Last active Aug 29, 2015
An attempt for a render_relative for rails
View ApplicationHelper.rb
# e.g. inside app/views/records/show.html.erb use
#
# <%= render_relative "./record/title" %>
#
# to render app/views/records/show/record/title
module App::ApplicationHelper
def render_relative(relative_partial_path, locals = {}, &block)
calling_file_directory = @virtual_path.split("/")[0..-2].join("/")
partial_path = File.join(calling_file_directory, relative_partial_path)
You can’t perform that action at this time.