ITcompressor IT — compressed and to the point
← Back to articles

GlassFish

GlassFish is an open implementation of Jakarta EE (formerly Java EE): an application domain, a web administration console and the asadmin CLI out of the box. As an application server it fits both development and production use.

All commands below are ./asadmin from the /opt/glassfish7/bin directory.

Installation#

GlassFish 7 — Jakarta EE 10, JDK 17+ is required. The distribution is a zip from Maven Central; check glassfish.org for the fresh version:

# Dependencies
sudo apt update
sudo apt install openjdk-17-jdk unzip

# Download and unpack (GlassFish 7.0.20)
wget https://repo1.maven.org/maven2/org/glassfish/main/distributions/glassfish/7.0.20/glassfish-7.0.20.zip
unzip glassfish-7.0.20.zip
sudo mv glassfish7 /opt/glassfish7

First start#

cd /opt/glassfish7/bin

# Start the default domain (domain1)
./asadmin start-domain

# Set a password for the default admin (asks for the old one — empty — and the new one)
./asadmin change-admin-password

# Enable encryption of the admin interface (except the very first password request)
./asadmin enable-secure-admin

After enable-secure-admin the domain must be restarted (./asadmin restart-domain), and admin traffic goes over TLS.

Another admin is added to the asadmin group of the same admin-realm:

./asadmin create-user --authrealmname admin-realm --groups asadmin backupadmin

The web console — http://localhost:4848; login with an account from the admin-realm. Better not to expose this port to the outside (see the firewall at the end).

Domain management#

# Start / stop / restart
./asadmin start-domain
./asadmin stop-domain
./asadmin restart-domain

# List domains and their status
./asadmin list-domains

start-domain --verbose is the foreground with logs to the console (handy for debugging), not a background mode. For auto-start on a server, create a systemd unit with /opt/glassfish7/bin/asadmin start-domain in ExecStart.

Application deployment#

# A WAR application
./asadmin deploy /path/to/orders-app.war

# An EAR enterprise application
./asadmin deploy /path/to/orders-app.ear

# A custom context root instead of the file name
./asadmin deploy --contextroot=/orders /path/to/orders-app.war

# What is already deployed
./asadmin list-applications

# Remove an application
./asadmin undeploy orders-app

By default deploy puts the application under the context derived from the file name (orders-app.war is available at /orders-app); redeploying the same name replaces the old version — hot deployment works.

Data sources (JDBC)#

The DB driver is just a jar in the domain's lib/ext; after that — a connection pool and a JDBC resource (JNDI) on top of it:

cp /path/to/postgresql.jar /opt/glassfish7/glassfish/domains/domain1/lib/ext/
./asadmin restart-domain
# A connection pool to PostgreSQL
./asadmin create-jdbc-connection-pool \
  --datasourceclassname=org.postgresql.ds.PGSimpleDataSource \
  --restype=javax.sql.DataSource \
  --property user=app:password=secret:serverName=10.0.0.14:portNumber=5432:databaseName=ordersdb \
  jdbc/ordersPool

# A JDBC resource (the JNDI name jdbc/ordersDS) — what applications look up
./asadmin create-jdbc-resource --connectionpoolid jdbc/ordersPool jdbc/ordersDS

# Pool liveness check
./asadmin ping-connection-pool jdbc/ordersPool

Properties are separated by :; the names serverName, databaseName, portNumber are for the PostgreSQL driver — other providers have their own property sets. Applications see the same resource through java:comp/env/jdbc/ordersDS.

Firewall#

# HTTP / HTTPS application listeners — to clients
sudo ufw allow from 10.0.0.0/24 to any port 8080
sudo ufw allow from 10.0.0.0/24 to any port 8181

# 4848 (admin console) — from the trusted admin subnet only
sudo ufw allow from 10.0.0.5 to any port 4848

Do not open port 4848 to the outside: the console can reconfigure the whole domain.

← Back to articles