Class Q2
java.lang.Object
org.jpos.q2.Q2
- All Implemented Interfaces:
FileFilter, Runnable
The Q2 application container — bootstraps and manages the lifecycle of QBean components.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBookkeeping record for a deployed QBean: when it was deployed, its JMX registration, the underlying object, and whether it should start eagerly. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault subdirectory undergetDeployDir()that Q2 scans for deployable XML.static final StringSuffix appended to deploy files that duplicate a prior deployment.static final StringSuffix recognised as environment-variable overlays in deploy.static final StringSuffix appended to deploy files that fail to load.static final StringJMX domain under which Q2 registers its MBeans.static final StringFile name of the embedded licensee descriptor.static final StringFile name for the logger configuration auto-deployed at startup.static final StringLogger name used for Q2 lifecycle events.static final StringElement name used to mark a QBean as protected (cannot be undeployed).static final byte[]SHA-256 hash of the public key bundled with theLICENSEEverifier.static final StringObject name of the Q2 class loader MBean.static final StringObject-name prefix used for QBean services.static final StringRealm tag for Q2 lifecycle log entries.static final intDeploy-directory poll interval in milliseconds.static final longMaximum time, in milliseconds, that Q2 waits for QBeans to stop on shutdown. -
Constructor Summary
ConstructorsConstructorDescriptionQ2()Constructs a newQ2instance with no command-line arguments and the default class loader.Constructs a newQ2instance with the specified deployment directory and the default class loader.Constructs a newQ2instance with the specified command-line arguments and the default class loader.Q2(String[] args, ClassLoader classLoader) Constructs a newQ2instance with the specified command-line arguments and class loader. -
Method Summary
Modifier and TypeMethodDescriptionbooleanFileFilterhook used by the deploy-directory scan: accepts readable XML descriptors and, when recursive scanning is enabled, sub-directories other thanlib.protected org.jdom2.Documentdecrypt(org.jdom2.Document doc) Reversesencrypt(Document): whendocis a<protected-qbean>envelope, returns the decrypted descriptor; otherwise returnsdocunchanged.voiddeployElement(org.jdom2.Element e, String fileName, boolean encrypt, boolean isTransient) Writes a single QBean descriptor tofileNameinside the deploy directory, optionally encrypting it and/or marking it transient.voiddeployTemplate(String template, String filename, String prefix) Renders a deploy template, prefixing every QBean inside withprefixand writing the result asfilenameunder the deploy directory.voidPrints the Q2 version banner toSystem.out.protected org.jdom2.Documentencrypt(org.jdom2.Document doc) Wrapsdocin a<protected-qbean>envelope whose body is the DES-encrypted, hex-encoded XML payload.String[]environmentArgs(String[] args) Prepends comma-separated values of theq2.argsenvironment property, if any, to the supplied command-line arguments.static StringReturns the application's version banner, when an embedded application has deployed its ownbuildinfo/revisionresources, ornullwhen none is present.static StringReturns the source branch of the running jPOS build.static StringReturns the timestamp at which the running jPOS jar was built.static StringReturns the JVM class path, expanded against theClass-Pathmanifest entry when the JVM was launched with a single executable jar.static StringReturns the SHA-1 hash of the class path returned bygetClassPath().String[]Returns the original command-line arguments Q2 was launched with.Returns the directory Q2 scans for deploy descriptors.Returns theQFactoryresponsible for instantiating QBeans from deploy descriptors.Returns the per-process random identifier assigned to this Q2 instance.protected byte[]getKey()Derives the 8-byte DES key used to protect deploy descriptors.Returns the dynamic class loader Q2 uses to load deployed QBeans.getLog()Returns the lifecycle log used by Q2 itself, lazily attaching a stdout listener when no logger is configured and Q2 is not running under the CLI.Returns the JMX MBean server backing Q2's QBean registrations.io.micrometer.core.instrument.MeterRegistryReturns the Micrometer registry that aggregates Q2 metrics.io.micrometer.prometheusmetrics.PrometheusMeterRegistryReturns the Prometheus-flavoured registry exposed for scraping.static Q2getQ2()Returns the running Q2 instance from theNameRegistrar, if any.static Q2getQ2(long timeout) Waits up totimeoutmilliseconds for a Q2 instance to register itself.static StringReturns a release identifier combininggetVersion()andgetRevision().static StringReturns the source revision of the running jPOS build.Returns Q2's uptime sincerun()captured the start instant.static StringReturns the jPOS build version, sourced from the bundled buildinfo resource.static StringBuilds the multi-line version banner used bydisplayVersion(), combining jPOS version metadata with any embedded application metadata.Returns the configuredWatchServiceclass name, ornullwhen polling is used.booleanReturnstrueif the dynamic class loader has been disabled via command-line options.static voidStandalone entry point: instantiates Q2, requestsSystem.exit(0)on shutdown, and starts the worker thread.static intnode()Returns the node identifier embedded in the licensee descriptor.booleanready()Returns whether Q2 has finished its initial deployment scan and is ready to serve requests.booleanready(long millis) Waits up tomillismilliseconds for Q2 to becomeready.voidrelax()Sleeps for one second, returning early on shutdown.voidrelax(long sleep) Sleeps the calling thread for at mostsleepmilliseconds, returning early if shutdown is requested in the meantime.voidrun()booleanrunning()Returns whether Q2 has started and has not yet been shut down.voidshutdown()Initiates an orderly shutdown of Q2 without waiting for the worker thread to terminate.voidshutdown(boolean join) Initiates shutdown and optionally blocks until the worker thread terminates.voidstart()Starts Q2 on a new daemon-style worker thread.voidstop()Initiates an orderly shutdown of Q2 and the QBeans it has deployed.
-
Field Details
-
DEFAULT_DEPLOY_DIR
Default subdirectory undergetDeployDir()that Q2 scans for deployable XML.- See Also:
-
JMX_NAME
-
LOGGER_NAME
-
REALM
-
LOGGER_CONFIG
File name for the logger configuration auto-deployed at startup.- See Also:
-
QBEAN_NAME
-
Q2_CLASS_LOADER
-
DUPLICATE_EXTENSION
Suffix appended to deploy files that duplicate a prior deployment.- See Also:
-
ERROR_EXTENSION
Suffix appended to deploy files that fail to load.- See Also:
-
ENV_EXTENSION
Suffix recognised as environment-variable overlays in deploy.- See Also:
-
LICENSEE
-
PUBKEYHASH
SHA-256 hash of the public key bundled with theLICENSEEverifier. -
PROTECTED_QBEAN
Element name used to mark a QBean as protected (cannot be undeployed).- See Also:
-
SCAN_INTERVAL
-
SHUTDOWN_TIMEOUT
Maximum time, in milliseconds, that Q2 waits for QBeans to stop on shutdown.- See Also:
-
-
Constructor Details
-
Q2
Constructs a newQ2instance with the specified command-line arguments and class loader. This constructor initializes various configurations, processes command-line arguments, sets up directories, and registers necessary components for the application.- Parameters:
args- an array ofStringcontaining the command-line arguments.classLoader- theClassLoaderto be used by the application. Ifnull, the class loader of the current class is used.Key Initialization Steps:
- Parses the command-line arguments twice: once before environment variable substitution and once after.
- Initializes the deployment directory and library directory (`lib`).
- Generates a unique instance identifier for the application instance.
- Sets the application start time to the current moment.
- Registers MicroMeter metrics and Q2-specific components.
Note: The
deployDirdirectory is created if it does not already exist.- See Also:
-
Q2
Constructs a newQ2instance with the specified command-line arguments and the default class loader.- Parameters:
args- an array ofStringcontaining the command-line arguments. If no arguments are provided, the application initializes with default settings.This constructor delegates to
Q2(String[], ClassLoader)with anullclass loader, causing the default class loader to be used.- See Also:
-
Q2
public Q2()Constructs a newQ2instance with no command-line arguments and the default class loader.This constructor is equivalent to calling
Q2(new String[]{}). It initializes the application with default settings.- See Also:
-
Q2
Constructs a newQ2instance with the specified deployment directory and the default class loader.- Parameters:
deployDir- aStringspecifying the path to the deployment directory. This is passed as a command-line argument using the-doption.This constructor is equivalent to calling
Q2(new String[]{"-d", deployDir}). It sets the deployment directory and initializes the application.- See Also:
-
-
Method Details
-
start
Starts Q2 on a new daemon-style worker thread.- Throws:
IllegalStateException- ifstop()has already been called on this instance
-
stop
Initiates an orderly shutdown of Q2 and the QBeans it has deployed. -
getMeterRegistry
Returns the Micrometer registry that aggregates Q2 metrics.- Returns:
- the active
MeterRegistry
-
getPrometheusMeterRegistry
Returns the Prometheus-flavoured registry exposed for scraping.- Returns:
- the
PrometheusMeterRegistrybacking the Prometheus endpoint
-
run
-
shutdown
Initiates an orderly shutdown of Q2 without waiting for the worker thread to terminate. -
running
-
ready
Returns whether Q2 has finished its initial deployment scan and is ready to serve requests.- Returns:
trueonce the initial deploy has completed and shutdown is not in progress
-
ready
-
shutdown
Initiates shutdown and optionally blocks until the worker thread terminates.- Parameters:
join- whentrue, waits up toSHUTDOWN_TIMEOUTms for the Q2 worker thread to terminate
-
getLoader
Returns the dynamic class loader Q2 uses to load deployed QBeans.- Returns:
- the active
QClassLoader
-
getFactory
-
getCommandLineArgs
Returns the original command-line arguments Q2 was launched with.- Returns:
- the command-line argument array
-
accept
FileFilterhook used by the deploy-directory scan: accepts readable XML descriptors and, when recursive scanning is enabled, sub-directories other thanlib.- Specified by:
acceptin interfaceFileFilter- Parameters:
f- candidate file- Returns:
trueiffshould be considered for deployment
-
getDeployDir
Returns the directory Q2 scans for deploy descriptors.- Returns:
- the deploy directory
-
getWatchServiceClassname
Returns the configuredWatchServiceclass name, ornullwhen polling is used.- Returns:
- the fully-qualified watch-service class name, or
null
-
getQ2
Returns the running Q2 instance from theNameRegistrar, if any.- Returns:
- the registered Q2 instance, or
nullif none is registered
-
getQ2
-
node
Returns the node identifier embedded in the licensee descriptor.- Returns:
- the node identifier
-
relax
Sleeps the calling thread for at mostsleepmilliseconds, returning early if shutdown is requested in the meantime.- Parameters:
sleep- maximum sleep time, in milliseconds
-
relax
Sleeps for one second, returning early on shutdown. -
getLog
-
getMBeanServer
Returns the JMX MBean server backing Q2's QBean registrations.- Returns:
- the active
MBeanServer
-
getUptime
-
displayVersion
Prints the Q2 version banner toSystem.out. -
getInstanceId
Returns the per-process random identifier assigned to this Q2 instance.- Returns:
- a stable
UUIDfor the running instance
-
getVersionString
Builds the multi-line version banner used bydisplayVersion(), combining jPOS version metadata with any embedded application metadata.- Returns:
- the formatted version banner
-
deployElement
public void deployElement(org.jdom2.Element e, String fileName, boolean encrypt, boolean isTransient) throws ISOException, IOException, GeneralSecurityException Writes a single QBean descriptor tofileNameinside the deploy directory, optionally encrypting it and/or marking it transient.- Parameters:
e- QBean descriptor elementfileName- target file name relative to the deploy directoryencrypt- whentruethe descriptor is DES-encrypted before being writtenisTransient- whentruethe file is tagged with the running instance id and removed on JVM exit- Throws:
ISOException- if encryption failsIOException- if writing the descriptor failsGeneralSecurityException- if the encryption cipher is unavailable
-
getKey
Derives the 8-byte DES key used to protect deploy descriptors. Subclasses may override to source the key material from a secret manager.- Returns:
- the DES key for
encrypt(Document)/decrypt(Document)
-
encrypt
protected org.jdom2.Document encrypt(org.jdom2.Document doc) throws GeneralSecurityException, IOException Wrapsdocin a<protected-qbean>envelope whose body is the DES-encrypted, hex-encoded XML payload.- Parameters:
doc- QBean descriptor to encrypt- Returns:
- an encrypted document suitable for writing to the deploy directory
- Throws:
GeneralSecurityException- if the DES cipher failsIOException- if serialising the input document fails
-
decrypt
protected org.jdom2.Document decrypt(org.jdom2.Document doc) throws GeneralSecurityException, IOException, org.jdom2.JDOMException Reversesencrypt(Document): whendocis a<protected-qbean>envelope, returns the decrypted descriptor; otherwise returnsdocunchanged.- Parameters:
doc- descriptor read from the deploy directory- Returns:
- decrypted document, or
docwhen not an encrypted envelope - Throws:
GeneralSecurityException- if the DES cipher failsIOException- if reading the embedded payload failsorg.jdom2.JDOMException- if the decrypted payload is not well-formed XML
-
main
Standalone entry point: instantiates Q2, requestsSystem.exit(0)on shutdown, and starts the worker thread.- Parameters:
args- command-line arguments accepted byQ2(String[])- Throws:
Exception- if Q2 fails to construct or start
-
getVersion
Returns the jPOS build version, sourced from the bundled buildinfo resource.- Returns:
- the jPOS version string
-
getRevision
Returns the source revision of the running jPOS build.- Returns:
- the revision string from the bundled revision resource
-
getBranch
-
getBuildTimestamp
Returns the timestamp at which the running jPOS jar was built.- Returns:
- the build timestamp string from the bundled buildinfo resource
-
getRelease
Returns a release identifier combininggetVersion()andgetRevision().- Returns:
- version and revision separated by a space
-
getAppVersionString
Returns the application's version banner, when an embedded application has deployed its ownbuildinfo/revisionresources, ornullwhen none is present.- Returns:
- the formatted application version string, or
nullwhen absent
-
getClassPath
Returns the JVM class path, expanded against theClass-Pathmanifest entry when the JVM was launched with a single executable jar.- Returns:
- the class path; never
null, possibly empty
-
getClassPathHash
Returns the SHA-1 hash of the class path returned bygetClassPath().- Returns:
- the hex-encoded SHA-1 hash, or an empty string when the class path is empty or hashing fails
-
isDisableDynamicClassloader
Returnstrueif the dynamic class loader has been disabled via command-line options.- Returns:
trueif dynamic classloading is disabled
-
deployTemplate
public void deployTemplate(String template, String filename, String prefix) throws IOException, org.jdom2.JDOMException, GeneralSecurityException, ISOException, NullPointerException Renders a deploy template, prefixing every QBean inside withprefixand writing the result asfilenameunder the deploy directory. Templates can be loaded either from ajar:URL or from a file path.- Parameters:
template- path orjar:URL of the template documentfilename- target file name relative to the deploy directoryprefix- prefix prepended to each contained QBean's name- Throws:
IOException- if reading the template or writing the result failsorg.jdom2.JDOMException- if the template is not well-formed XMLGeneralSecurityException- if encryption is requested and failsISOException- if encryption failsNullPointerException- iftemplateisnull
-
environmentArgs
Prepends comma-separated values of theq2.argsenvironment property, if any, to the supplied command-line arguments.- Parameters:
args- original command-line arguments- Returns:
- the merged argument array;
argsunchanged when no override is set
-