Class Backend

java.lang.Object
com.codename1.backend.Backend

public final class Backend extends Object

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
  • Method Details

    • isMeasured

      public boolean isMeasured()
      Whether this server records request and job metrics.
    • getTracer

      public Tracer getTracer()
      The tracer this server installed, or null: its scheduled jobs are traced by it.
    • builder

      public static Backend.Builder builder()
      A builder whose defaults come from the configuration this process sees.
    • builder

      public static Backend.Builder builder(Config config)
      A builder over a configuration the caller already loaded or built.
    • getListenAddress

      public String 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

      public HttpServer getServer()
      The running server, for its metrics or to stop it.
    • getDataSource

      public DataSource getDataSource()
      The connection pool, or null when this server has no database.
    • getEntityManager

      public EntityManager getEntityManager()
      The entity manager, or null when this build generated no entities.
    • getConfig

      public Config getConfig()
      The configuration this server resolved its settings from.
    • getApplication

      public Backend.Application getApplication()
      The build-generated wiring of this server's beans, or null when it has none.
    • getManagedBeans

      public List 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

      public RequestLog getRequestLog()
      The log of this server's recent requests, off until something enables it.
    • getSessions

      public Sessions 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()
      Whether stop() 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 DataSource constructor 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

      public static HttpServer.Request currentRequest()
      The request the calling thread is serving, or null outside one. Maintained only for applications whose build asked for it -- see Backend.Application.tracksCurrentRequest().