Class Backend
A configured, running server: the twenty lines every main used to open with, written once.
Backend.builder()
.handler(new ApiRouter(new Api()))
.run();
That reads the configuration, opens the database the deployment named, registers the generated daos, binds the port, installs a shutdown handler that drains what is in flight, and waits. Each of those was previously the developer's to write and to get wrong -- a main that returns ends the process without a word, because the host threads are detached, and a server with no signal handler loses every connection it was serving when the orchestrator stops it.
What comes from where
Anything the builder is TOLD is used as given. Anything it is not told, it
reads from Config: the port, the worker count, TLS, static files and
the database, in that layered order of system property, environment variable,
profile file, base file. The rule is worth stating once because the opposite
rule is also defensible: a value in the source wins over a value in the
environment, so a port written here is the port, and a port that should follow
the deployment is one nobody writes here.
The database is opened when there is one to open: a URL is configured, a pool was handed in, or the build generated at least one entity and the ORM therefore needs one. A server with no database opens none, which is what makes this the same entry point for both kinds.
# a laptop
CN1_PROFILE=dev ./server
# production
DATABASE_URL=postgres://app:secret@db.internal/app PORT=8080 ./server
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThe build-generated wiring of an application: every bean, constructed and injected by straight-line code the build wrote, and the lifecycle calls around them.static final classstatic final classWhat anBackend.Applicationis built from.static interfaceWhere the handlers are built, once the things they need exist.static interfaceWhere a server's websocket endpoints come from. -
Method Summary
Modifier and TypeMethodDescriptionvoidBlocks until the server stops.static Backend.Builderbuilder()A builder whose defaults come from the configuration this process sees.static Backend.BuilderA builder over a configuration the caller already loaded or built.static HttpServer.RequestThe request the calling thread is serving, or null outside one.The build-generated wiring of this server's beans, or null when it has none.The configuration this server resolved its settings from.The connection pool, or null when this server has no database.The entity manager, or null when this build generated no entities.The address a client on this machine reaches the listener at: the one it is bound to (bracketed when IPv6), or 127.0.0.1 when it listens on every interface.The managed beans THIS server registered, as a copy.The log of this server's recent requests, off until something enables it.The running server, for its metrics or to stop it.This server's sessions: their settings and the store they are kept in.The tracer this server installed, or null: its scheduled jobs are traced by it.booleanWhether this server records request and job metrics.booleanisReady()Whether the server has finished starting and serves its application.booleanWhetherstop()has finished: the beans are destroyed and the pool closed.static DataSourcerequireDataSource(DataSource dataSource, String handler) The pool, or a refusal naming the handler that asked for one.static EntityManagerrequireEntities(EntityManager entities, String handler) The entity manager, or a refusal naming the handler that asked for one.voidstop()Stops accepting, lets what is in flight finish, and closes the database.
-
Method Details
-
isMeasured
public boolean isMeasured()Whether this server records request and job metrics. -
getTracer
The tracer this server installed, or null: its scheduled jobs are traced by it. -
builder
A builder whose defaults come from the configuration this process sees. -
builder
A builder over a configuration the caller already loaded or built. -
getListenAddress
The address a client on this machine reaches the listener at: the one it is bound to (bracketed when IPv6), or 127.0.0.1 when it listens on every interface. A listener bound to one address answers on no other. -
getServer
The running server, for its metrics or to stop it. -
getDataSource
The connection pool, or null when this server has no database. -
getEntityManager
The entity manager, or null when this build generated no entities. -
getConfig
The configuration this server resolved its settings from. -
getApplication
The build-generated wiring of this server's beans, or null when it has none. -
getManagedBeans
The managed beans THIS server registered, as a copy. Per server rather than per process: a second server in the same process -- or this one started again -- must not list or invoke the beans of one that has stopped. -
isReady
public boolean isReady()Whether the server has finished starting and serves its application. -
getRequestLog
The log of this server's recent requests, off until something enables it. -
getSessions
This server's sessions: their settings and the store they are kept in. -
awaitTermination
public void awaitTermination()Blocks until the server stops. -
stop
public void stop()Stops accepting, lets what is in flight finish, and closes the database.
The order matters and is the reason this exists rather than two calls: closing the pool first would fail the requests that were still being served with it.
-
isStopped
public boolean isStopped()Whetherstop()has finished: the beans are destroyed and the pool closed. -
requireDataSource
public static DataSource requireDataSource(DataSource dataSource, String handler) throws IOException The pool, or a refusal naming the handler that asked for one.
Called from generated wiring: a controller declaring a
DataSourceconstructor is declaring a DEPENDENCY, and handing it null because nothing configured a database turns that into a server that starts, reports healthy and fails on the first request that touches the database. The deployment is missing a setting, and start-up is where that is cheap to see.- Throws:
IOException
-
requireEntities
public static EntityManager requireEntities(EntityManager entities, String handler) throws IOException The entity manager, or a refusal naming the handler that asked for one.- Throws:
IOException
-
currentRequest
The request the calling thread is serving, or null outside one. Maintained only for applications whose build asked for it -- seeBackend.Application.tracksCurrentRequest().
-