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 a Asynchronous Spring REST API with Java 8’s CompletableFutures

Spring MVC is a versatile framework for building RESTful APIs. Spring has long supported asynchronous endpoints through its DeferredResult, which enables async by freeing up request threads to process other requests while a request waits for async tasks to complete. This can greatly increase throughput for IO intensive APIs. Here’s a simple example.

 DeferredResult

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

    new Thread(() -> { 
      String apiResponse = callApi(input)
      deferredResult.setResult(apiResponse);
    }).start();

    return defResult;
  }

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

 CompletableFuture

Java 8 introduced CompletableFuture

Continue reading →


Step-by-Step Guide to Deploying Your First 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 →