DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Build a Google Photos Clone - Part 1
  • Build a Java Backend That Connects With Salesforce
  • How to Develop Event-Driven Architectures
  • Build Even Faster Quarkus Applications With fast-jar

Trending

  • You Do Not Need a PhD to Become an AI Engineer: The Essential Skill Stack
  • How to Hide Sensitive Information Before You Share a File
  • Kafka vs Flink vs Spark: Do You Really Need Real-Time?
  • The Man Who Invented Selenium Also Built Its Replacement

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. How to Build Scalable, Stateful Java Services in Under 15 Minutes

How to Build Scalable, Stateful Java Services in Under 15 Minutes

Learn more about building scalable Java services in 15 minutes or less.

By 
Bradley Johnson
·
Updated Jun. 19, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Five years ago, when I started tracking media buzz around stateful architectures, I’d see a few articles every month about running stateful containers. That's about when Caitie McCaffrey first shared this awesome presentation about building scalable stateful architectures. Since then, the dominant software paradigm has become functional application design. The actor model and other object-oriented paradigms are still in use, but database-centric RESTful architectures are standard means of building web applications today.

However, the tides are beginning to shift. Due to innovations like the blockchain, growing demand for real-time applications, the digitization of OT assets, and the proliferation of cheap compute resources at the network edge; there’s renewed interest in decentralized application architectures. As such, there’s also been increased focus on stateful applications. For example, at least five Apache Foundation projects (Beam, Flink, Spark, Samza, and TomEE) are touting statefulness as a benefit today. Modern applications communicate across multiple application silos and must span real-world machines, devices, and distributed data centers around the world. Stateful application architectures provide a way to abstract away the logistical effort of state management, thereby reducing development and management effort necessary to operate massive-scale distributed applications.

Build a Scalable, Stateful To-Do List in 15 Minutes or Less

For the rest of this post, I want to disprove the notion that building scalable, stateful applications is a task too complex for everyday Java developers. In order to illustrate how easily a stateful application can be set up, we’ll walk through a tutorial for building a simple to-do list using the Swim platform. You can find all the source code for the to-do list tutorial here on GitHub.

Getting Started: Setting Up Your Development Environment

Before diving into the to-do list app itself, it’s important to ensure your development environment is properly set up to run Swim applications.

To run the to-do list example application, only Java 9 and the Gradle build tool (which is included in the wrapper script) are required. This video tutorial goes through the process of setting up your local Swim development environment:


Tutorial: How to Build a Simple, Stateful “To-Do List” App Using Swim

Once your development environment is set up, you’re ready to build a stateful to-do list. The to-do list example is quite basic, but the same patterns can be applied to Swim applications of any complexity. As I mentioned earlier in the post, all the code for the to-do list example app is available on GitHub.

For a better idea of what's included in this tutorial, take a look at the following lines of server code. This code block defines a Web Agent that can create and observe a to-do list. Every server-side to-do list continuously and statefully runs the logic below:

/**
 * This is a simple WebAgent to manage the list of items in the To-do list
 */
public class ListAgent extends AbstractAgent {

  /**
   * This MapLane holds each list item and uses a timestamp for a unique key
   */
  @SwimLane("list")
  MapLane<Long, String> todoList = this.<Long, String>mapLane();

  /**
   * This is a command lane used to add new items to the to-do list
   */
  @SwimLane("addListItem")
  public CommandLane<String> addListItem = this.<String>commandLane()
    .onCommand((String newListItem) -> {
      // create a new timestamp as our UUID
      final long newUuid = System.currentTimeMillis();
      // cap item length to 255 characters
      newListItem = newListItem.substring(0, Math.min(newListItem.length(), 255));

      // add the new item to the todoList MapLane
      todoList.put(newUuid, newListItem);
    });

  /**
   * This is a command lane used to remove items from the 
   * to-do list by its key (or uuid)
   */
  @SwimLane("removeListItem")
  public CommandLane<String> removeListItem = this.<String>commandLane()
    .onCommand((String uuidString) -> {
      // parse the value sent by the UI from a String to Long
      long uuidToRemove = Long.parseLong(uuidString);

      // remove the list item from the todoList MapLane
      todoList.remove(uuidToRemove);
    });
}


Finally, if you’re ready to get started building your own stateful to-do list application, check out this tutorial video:


Learn More

Let us know your thoughts about this tutorial and what you're building on the open-source Swim platform. You can get started with Swim here and make sure to STAR us on GitHub.

application Build (game engine) Java (programming language) Architecture

Published at DZone with permission of Bradley Johnson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Build a Google Photos Clone - Part 1
  • Build a Java Backend That Connects With Salesforce
  • How to Develop Event-Driven Architectures
  • Build Even Faster Quarkus Applications With fast-jar

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook