Generating Java record classes with Jackson Annotations to map JSON using ChatGPT

There’s a lot of discussion about how to use ChatGPT to generate tests for code. Another interesting use case I’ve seen fairly little coverage of is generating DTOs from JSON. Here is an example with the prompt I’ve put together applied to JSON from the manifest of a Distributed Map Run.

Continue reading “Generating Java record classes with Jackson Annotations to map JSON using ChatGPT”

Yes, Java ThreadLocalRandom is Safe to Use with Virtual Threads

Because Java 21 virtual threads are very cheap — per JEP Cafe, they are about 1,000 times faster to launch than a platform thread, and use about 1,000 times less memory than a platform thread — they should never be pooled. As a result, ThreadLocal variables are unlikely to be useful in applications that use virtual threads for concurrent processing. If each task lives in its own dedicated thread, then each call to ThreadLocal ends up returning a new value, and it ends up being a factory instead of a pool! So using ThreadLocal in virtual threads is an anti-pattern. Developers should use another technique for pooling objects, such as structured concurrency, instead.

But what about ThreadLocalRandom? Is this dedicated class for providing sources of randomness to threads safe to use with virtual threads?

The short answer is: Yes, ThreadLocalRandom is safe to use with virtual threads. ThreadLocalRandom is tightly integrated with the Thread class to ensure that it remains efficient, even for virtual threads. For more information, keep reading.

Continue reading “Yes, Java ThreadLocalRandom is Safe to Use with Virtual Threads”

Example JWT with Private JWK

When writing code or testing, it’s useful to have valid examples of whatever you’re working with for sanity checks. I need some example JWTs with accompanying private JWK and couldn’t find any, so I minted a few. I’m posting them here in the hopes they’re useful to others, too.

These keys were created using a fresh RS256 key. No other keys have been created using this key. Because the private part of this key pair is public — it’s literally right below, in plain text — remember that these keys should only ever be used for sanity checking and testing. In particular, never use these keys for production use cases!

Continue reading “Example JWT with Private JWK”

Apache HTTPComponents HTTP Header Names are Case Insensitive

The source code never lies.

    /**
     * Gets all of the headers with the given name.  The returned array
     * maintains the relative order in which the headers were added.
     *
     * <p>Header name comparison is case insensitive.
     *
     * @param name the name of the header(s) to get
     *
     * @return an array of length &ge; 0
     */
    @Override
    public Header[] getHeaders(final String name) {
        List<Header> headersFound = null;
        for (int i = 0; i < this.headers.size(); i++) {
            final Header header = this.headers.get(i);
            if (header.getName().equalsIgnoreCase(name)) {
                if (headersFound == null) {
                    headersFound = new ArrayList<>();
                }
                headersFound.add(header);
            }
        }
        return headersFound != null ? headersFound.toArray(EMPTY) : EMPTY;
    }

The Lambda Iceberg: A Deep Dive on AWS Lambda for Java

AWS Lambda is Amazon’s FaaS product. Pound for pound, it’s one of the best serverless computing products on the market. Easy to use, inexpensive to run (among FaaS offerings), and with compelling features like Layers, Extensions, and SnapStart, Lambda is a rock-solid choice for building serverless architectures.

However, its managed nature cuts both ways. The same FaaS features that make it so easy to use for vanilla workloads — just upload your program and go — also make it hard to use for anything that requires even a little customization, like ML models. I’ve done some significant work building out complex lambda functions for Java lately, and while deploying these complex workloads on AWS Lambda using Java is complex, the reward — a perfectly elastic, pay-for-uptime microservice architecture — is well worth the effort. But it turns out there’s a lot of the “Lambda iceberg” below the water to understand before you can expect to get these complex serverless applications working with high performance and reliability.

In this blog series, I will unpack what I learned in my journey to deploy an OpenCV-backed ML model onto AWS Lambda with minimal cold start, and show how the process can be used to deploy ML models on other backends, like TensorFlow Lite, onto Lambda as well.

Community-Managed AWS Lambda Base Images for Java 21

I’ve added a new custom base image for Java 21 on Lambda to complement the community base images already available for Java 17, Java 18, Java 19, and Java 20. You can find the images on the ECR Public Gallery and DockerHub and the source code on GitHub. Java 21 is an LTS release with some of the most exciting new features Java has seen in a long time, so everyone should be looking to upgrade ASAP!

I’ve also released a custom Lambda runtime for Java 21, if that’s more your speed.

These should be plenty to get you started on your AWS Lambda + Java 21 adventure!

Continue reading “Community-Managed AWS Lambda Base Images for Java 21”

Java 21 Custom Runtime for AWS Lambda

By standing on the shoulders of giants (I’m looking at you, Mark Sailes), I was able to turn around an AWS Lambda Custom Runtime for Java 21 on Java 21 launch day. Instructions to use it are in the repo’s README. All files required to create your own function are in the releases, so no building required.

There are a couple of known issues, particularly around CDS (Class Data Sharing), per the but they appear to be (mostly) cosmetic at first blush. It’ll be fun ironing that out.

I’m also looking forward to releasing a custom image for Java 21, too, as soon as an updated Amazon Corretto image drops.

Happy hacking!

emoji4j v15.0.1 Released

A new version v15.0.1 of my emoji processing library, emoji4j, for Java 8+ just dropped. Here are the updates:

  • Update to Unicode 15
  • New method GraphemeMatcher#results()
  • Imroved documentation
  • Even more tests

There is also now a Cookbook in the emoji4j wiki to help users solve hard or common problems with emoji4j.

Enjoy!

Community-Managed AWS Lambda Base Images for Java 20

I’ve added a new custom base image for Java 20 on Lambda to complement the community base images already available for Java 17, Java 18, and Java 19. You can find the images on the ECR Public Gallery and DockerHub and the source code on GitHub. All the new features in Java 20 are in preview or incubator, as befits a non-LTS release, but for those who like to live life on the bleeding edge, there’s lots of new toys to play with. These base images will let you get started.

Continue reading “Community-Managed AWS Lambda Base Images for Java 20”