Carl Martensen

I’m a Software Engineer in New York City

Read this first

Java 9’s Immutable Collections Are Easier To Create But Use With Caution

Code is easier to reason about when collections cannot be altered after their creation. Having to keep track of the current state of a collection as it gets passed around from this method to that equates to more mental balls to juggle. Mutating the state of methods` arguments is called a side effect and is a cardinal sin of functional programming. Since immutability is almost inarguably better why do Java developers generally completely ignore it?

 Instantiating an Immutable Collection in Java 8

Creating immutable collections in Java is convoluted to the point that it’s probably not worth the price of admission. This is a best practice implementation of an immutable set in Java 8:

Set<String> set = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("darth", "plagueis", "the", "wise")));

By comparison, here’s the same thing in Scala:

val set = Set("darth", "plagueis", "the",

Continue reading →

Create an Asynchronous Spring REST API with CompletableFutures and DeferredResult

Spring MVC is a powerful framework for building RESTful APIs. In accordance with the shift to asynchronous programming, Spring introduced the DeferredResult which an endpoint method can return so that its contents are propagated to the client when the asynchronous results of the request are eventually completed. Here’s a simple example:


public class DeferredController {
  public DeferredResult<String> get(@RequestParam String input) {
    DeferredResult<String> defResult = new DeferredResult<>();

    new Thread(() -> { 
      String apiResponse = callApi(input)

    return defResult;

  String callApi(String str) {
    // restTemplate.invoke(...)
    return str.toUpperCase();

Using DeferredResult frees up the request thread to process other

Continue reading →

Simplify Concurrency Using ParallelStream

Choosing the right multithreading framework for a Java application can be more difficulty than implementing the solution. Out of the box there are several utilities suitable for parallelizing simple I/O bound tasks such as ExecutorService (Java 1.5), ForkJoinPool (Java 1.7), ParallelStream (Java 1.8), CompletableFuture (Java 1.8), and Spring’s TaskExecutor (Spring 2.0) with its optional @EnableAsync (Spring 3.1) proxying. Any of these tools are more than capable of getting the job done with nearly equivalent performance metrics, and so the most important selection criterion is maintainability. As we’ll see, ParallelStream provides arguably the cleanest syntax for handling basic concurrency.

 Test Scenario

A RESTful microservice that responds to POST requests by performing four database writes and then returning a 200 or 400 HTTP code depending on success execution of the database

Continue reading →

Step-by-Step Guide to Deploying a Full-Stack Spring Boot Application in AWS

Building and deploying a full-stack web application into the cloud can be a challenge even for experienced software engineers. Often times we become so specialized in front-end, back-end, or dev-ops that releasing even a trivial full-stack application can be an odyssey into stackoverflow and beyond. This guide starts with a simple Spring Boot hello world application, wires it into a database, and goes through each step in deploying it to AWS backed by a Postgres database.

We will first build and deploy the application locally and then deploy it to AWS.

 Setting up your dev environment

The first thing is getting your environment set up with the Java 8 JDK, git, Maven 3, and PostgresSQL. For mac users with homebrew installed, this may be as simple as:

brew install java
brew install maven
brew install git
brew install postgresql

 Building the application locally

Now that our dev

Continue reading →

Subscribe to Carl Martensen

Don’t worry; we hate spam with a passion.
You can unsubscribe with one click.