launcher
-
Overview
-
Attributes
-
Child Elements
-
Rendering Operations
-
Execution Operations
- Nested Element Definitions
-
Examples
Overview
The Java <launcher> element defines a Java application that will be executed by Magnet. Based on the provided configuration, Magnet will use the Java reflexion API to dynamically call the main(String[] args) method of the specified class.
Magnet supports starting more than one Java application per JVM. Indeed, the flexible classloader strategy configuration scheme that Magnet features provides a baseground for many applications to live within the same JVM, each application with its own classloader. As discussed in the classpath element documentation, you can design a classloader hierarchies that suits your needs.
Attributes
The following list the xml attributes of the Java launcher element:
| Name | Description | Required | Interpolation |
|---|---|---|---|
| type | Defines the type of launcher. This attribute must have the value 'java'. | yes | no |
| name | The name of this Java launcher. | yes | no |
| mainClass | The fully qualified name of the class that contains the main() method to invoke. | yes | yes |
| args | Defines the application arguments that will be passed to the main() method of the main class. | no | yes |
| isDaemon | Tells Magnet how it should setup the thread that will call the class' main() method. The default value is false. | no | yes |
| default | The name of the default profile of this launcher. This is optional and allows specifying which profile of this launcher will be executed in the cases where the profile specified at startup is not configured at the level of this launcher. | no | no |
| waitTime | If specified, tells Magnet how many milliseconds it needs to wait after launching this Java application, before resuming its execution. This can be used to insert some pause between application startups when your Magnet file contains many launchers. | no | no |
Child Elements
The following lists the possible child xml elements the Java launcher element can hold:
| Name | Cardinality | Description |
|---|---|---|
| profile | 1 or * | Defines how the launcher is configured by profile. |
Rendering Operations
When a Java <launcher> element is rendered, it performs the following operations in order:
- Renders the appropriate <profile> child element.
- Adds all the rendered parameters (taking the profile into account) to the current magnet context (for variable interpolation).
- Resolves the mainClass, arguments and isDaemon attributes (using variable interpolation).
Execution Operations
Once a Java <launcher> element is rendered with success, it is executed according to these steps - in the order specified:
- Creates the required classloaders to reflect the classpath hierarchy specified in the configuration (see the classpath element documentation for more details).
- Loads the main class using the leaf classloader of the previous step (using reflexion).
- Creates a new thread, setting its name to the name of the launcher, the daemon flag, and context classloader.
- Starts the created thread to asynchronously call the main() method of the main class.
Nested Element Definitions
Profile
The Java <profile> element defines the configuration of the launcher for a given profile. It provides a way to define parameters specific to this launcher and the classpath to use when bootstrapping the main class. This can vary by profile - and is thus "profile-aware".
Attributes
| Name | Description | Required | Interpolation |
|---|---|---|---|
| name | The name of this profile. | yes | no |
Child Elements
The following lists the possible child xml elements that can hold the Java profile element:
| Name | Cardinality | Description |
|---|---|---|
| parameters | 0 or 1 | Defines specific parameters that must be used (or overwritten) for the main() method's execution. |
| classpath | 1 | Defines the classpath for the main() method's execution. |
Rendering Operations
When an java <profile> element is rendered, it performs the following operations in order:
- Renders the <parameters> child element if provided.
- Renders the <classpath> child element.
Examples
This first example shows a basic Java launcher that runs a HelloWorldApp class:
<magnet xmlns:magnet="http://schemas.sapia-oss.org/magnet/"
name="JavaLauncherExample1"
description="This is a first java launcher example.">
...
<launcher type="java" name="helloWorld"
mainClass="org.sapia.magnet.examples.HelloWorldApp"
args="only an english message">
<profile name="english">
<classpath parent="hello_cp">
<path directory="${user.dir}/lib">
<include pattern="**/*.jar" />
</path>
</classpath>
</profile>
</launcher>
...
</magnet>
This second example how to start two Java applications with a common classpath element that contains common jar files:
<magnet xmlns:magnet="http://schemas.sapia-oss.org/magnet/"
name="JavaLauncherExample2"
description="This is a second java launcher example.">
<parameters>
<param name="jndi.portNumber" value="1099" />
</parameters>
<classpath id="common_cp">
<path directory="${user.home}/libs/common">
<include pattern="*.jar" />
</path>
</classpath>
<!-- Define a java launcher that will start the stateless time server. -->
<launcher type="java" name="timeServer" isDaemon="true" waitTime="5000"
mainClass="org.sapia.ubik.rmi.examples.time.StatelessTimeServer"
args="${timeServer.jndiUrlProvider} ${timeServer.jndiInitialFactory}"
default="local">
<profile name="local">
<parameters>
<param name="timeServer.jndiUrlProvider"
value="ubik://localhost:${jndi.portNumber}" />
<param name="timeServer.jndiInitialFactory"
value="org.sapia.ubik.naming.InitialContextFactory" />
</parameters>
<classpath parent="common_cp">
<path directory="${user.home}/libs/server">
<include pattern="*.jar" />
</path>
</classpath>
</profile>
</launcher>
<!-- Define a java launcher that will start the time client application. -->
<launcher type="java" name="timeClient"
mainClass="org.sapia.ubik.rmi.examples.time.EndlessTimeClient"
default="local" waitTime="5000">
<profile name="local">
<classpath parent="common_cp">
<path directory="${user.home}/libs/client">
<include pattern="*.jar" />
</path>
</classpath>
</profile>
</launcher>
...
</magnet>