Running Java shebang with Kubernetes

Java 11, among other features, introduced the possibility of running a single Java source file. We are going to see a practical example of this feature running with kubernetes. apiVersion: v1 kind: ConfigMap metadata: name: java-shebang-config-map data: script.sh: | #!/usr/bin/java --source 17 public class App { public static void main(String args[]) { System.out.println("Hello Java Script"); } } --- apiVersion: v1 kind: Pod metadata: name: app spec: volumes: - name: java-shebang-config configMap: name: java-shebang-config-map defaultMode: 0777 containers: - image: openjdk:17.0.2-oracle command: ["./scripts/script.sh"] name: app volumeMounts: - mountPath: /scripts name: java-shebang-config restartPolicy: Never kubectl create -f hello-java.yml kubectl logs app

August 10, 20221 min

How to collect more than a single collection or a single scalar

A well-known characteristic of Java Stream is that it can be consumed only once, thus if we want to do some operations that require two passes or more through elements we have to create a new stream or develop a custom collector. Thankfully, Java 12 comes with Collectors::teeing that solves exactly this situation. Imagine that we need a concise way to get both the min and max values of a stream. ...

May 9, 20213 min

How to Organize the Code in the Ports and Adapters Architecture

In this article, we’ll have a look at how to separate an application into different modules so that it can be developed and tested in isolation of any external dependencies. As a result, we’ll obtain loosely coupled components where things like database and communication protocol can easily be replaced without any changes in the domain logic. First of all, let’s introduce a user story so that the domain logic of the example will be clear from the start: ...

May 1, 20213 min

Functional Programming Concepts in Java

With the rise of lambda expression and newer features, there is an increasing necessity to review some of the functional programming concepts and how are they applied in Java. Currying Currying is the process of turning a function with multiple arguments into a function with fewer arguments. For example, we can turn an addition between three numbers into a single argument function: Function<Integer, Function<Integer, Function<Integer, Integer>>> abc = a -> b -> c -> a + b + c; assert abc.apply(1).apply(2).apply(3) == 6; Which is fun, but is it really useful? Definitely! Sometimes we don’t want to repeat the same argument and cannot overload the method (to simulate default parameter): ...

March 16, 20213 min

An Introduction to Java Sealed Classes and Interfaces

In this article, we’ll explore what a sealed type in Java is, how to define a class and interface as sealed and finally we’ll go through a practical example. Sealed class syntax To have explicit control over the extensibility of a class we use identifier sealed and then permits to specify the set of classes allowed to extend: sealed class Advice permits BeforeAdvice, Interceptor, AfterAdvice { } non-sealed class BeforeAdvice extends Advice { } final class Interceptor extends Advice { } sealed class AfterAdvice extends Advice { } final class AfterReturningAdvice extends AfterAdvice { } ...

August 31, 20205 min