portfolio / jbend

JBend

A Java backend I wrote from scratch, starting at the socket. No Netty, no Jetty, no Spring. It is running on this server right now.

▶ Open the live demo Read the documentation Source on GitHub

Built with
Java, MySQL (JDBC), JavaScript
When
2020 to 2022; version 2.0 in 2026
Size
About 8,500 lines of Java, no dependencies beyond the MySQL driver
Source
github.com/kanetempleton/JBend

Why I built it

I wanted to know what a web framework actually does, so I wrote one without using one. JBend starts with a non-blocking TCP server on Java NIO (ServerSocketChannel and a Selector) and everything else sits on top of that: HTTP, WebSockets, logins and a database layer.

It wasn't only an exercise. I used it to build the repair ticket system at RZ Tech & Web, where employees entered and tracked customer jobs on it, and it served an earlier version of this site.

An application is one config file

JBend's main method is two lines long. What an application consists of is written in config/application.conf: each block becomes a thread, and they start in order, each waiting for the one before it to be ready.

app-id:shop

dataserver:
source env:DB_DSN          # MySQL, address taken from the environment

login:
sessions 7d                # accounts, sessions, lockouts, bans

webserver:
listen 8080
home res/front
handler com.shop.Site      # my class, extending JBend's HTTP server
upgrade /ws 8081

websocket:
listen 8081
bind 127.0.0.1
handler com.shop.LiveUpdates

app:
class com.shop.NightlyReport

The config language has its own lexer, parser and interpreter. A line it does not understand stops the startup with the line number, before anything is half-running. The configuration reference lists every block.

How it fits together

Launcher  (reads config/application.conf)
 ├── DatabaseManager    one MySQL connection, queries wait in a queue
 ├── LoginHandler       accounts, sessions, lockouts, bans
 ├── HTTP server    ─┐
 ├── WebSocket      ─┤
 ├── TCP server     ─┼── each a protocol on the same
 ├── Bridge         ─┤   non-blocking server core
 ├── Proxy          ─┘
 ├── your threads       a game loop, a scheduler
 └── Console            commands while it runs: status, kick, ban, mysql

What using it looks like

Application code extends one of JBend's protocols and is named in the config. A page with logic:

public class Site extends HTTP {
    public Site(String home, int port) { super(home, port); }

    @Override
    public Response handle(ServerConnection c, Request req) {
        if (req.path.equals("/hello"))
            return Response.text("Hello, " + req.param("name", "world") + "!");
        return null;   // not mine: fall through to routes and static files
    }
}

A database query, with the code that uses its result attached to it:

new ServerQuery(c, "SELECT x, y FROM players WHERE username = ?", name) {
    public void done() {
        c.sendMessage("pos:" + value("x") + "," + value("y"));
    }
};

The live demo

kanetempleton.com/JBend/demo is one JBend process. This site's Go server passes requests under that address to it. Its config file starts a web server, two games and the plumbing between them:

It uses the same MySQL database as the rest of this site, in tables of its own.

Version 2.0

The 2020 version worked for the projects I built on it, on machines I controlled. Version 2.0, in 2026, is a pass over every class to make it something that can sit on the public internet, and it finishes the config language I had started and never committed. Some of what that pass turned up:

The full list is in the documentation.

Built on JBend

Open the live demo Documentation Source on GitHub Back to the portfolio