Container (Swarm)

At the root of every WildFly Swarm application is the container.

The container provides an API for instantiating an embedded WildFly Swarm container, starting and stopping it, and deploying.

The container can either be instantiated inside your own main(…​), or if you don’t provide a main(…​), then WildFly Swarm will handle the construction of the container and subsequent deployment of your project artifact.

User-Provided main(…​)

If you provide a main(…​) and configure it in using the Maven Plugin or Gradle Plugin, it should simply be a regular Java main(…​) method.

Because of some initialization timing issues, you should construct the Swarm prior to working with any deployments using the ShrinkWrap API.

It’s also recommended that the Swarm is constructed prior to any other code in the main(…​). This is to prevent any issues of JBoss Logging not being able to initialize the logging subsystem if another framework or class has used JUL, or another logger, first.

public class MyMain {
    public static void main(String...args) {

        // Instantiate the container
        Swarm swarm = new Swarm();

        // Create one or more deployments
        JAXRSArchive deployment = ShrinkWrap.create(JAXRSArchive.class);
        ...

        // Add resource to deployment
        deployment.addClass(MyResource.class);

        swarm.start();
        swarm.deploy(deployment);
    }
}

If you only need to do some configuration of the container without needing to manipulate the deployment archive, you can call deploy() with no arguments to have it deploy the default archive. For example, here we configure an h2 datasource, then deploy the default archive:

public class MyMain {
    public static void main(String...args) {
         new Swarm()
           .fraction(new DatasourcesFraction()
                .jdbcDriver("h2", (d) -> {
                    d.driverClassName("org.h2.Driver");
                    d.xaDatasourceClass("org.h2.jdbcx.JdbcDataSource");
                    d.driverModuleName("com.h2database.h2");
                })
                .dataSource("ExampleDS", (ds) -> {
                    ds.driverName("h2");
                    ds.connectionUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
                    ds.userName("foo");
                    ds.password("bar");
                }))
           .start()
           .deploy();
    }
}

No User-Provided main(…​)

If you choose not to provide a main, org.wildfly.swarm.Swarm#main(…​) will be used by default. It boots the default container (based upon your dependencies) and deploys your project artifact.

The org.wildfly.swarm.Swarm class is also suitable for executing directly from your IDE if you have no requirement of providing a customized main(…​).

Supporting with Command-Line arguments

When no user-provided main(..) is available, the default Swarm main supports a handful of command-line arguments to the -swarm.jar

In the event a user-provided main(…​) exists, it can still optionally provide support for the above-mentioned command-line arguments. A constructor of Swarm takes the String…​args provided to the main(…​) and will process all relevant arguments.

public class MyMain {
    public static void main(String...args) {
        Swarm swarm = new Swarm(args);
        ...
    }
}

In this case, be aware that if the -swarm.jar is invoked with --help, a call to System.exit(0) will occur, abruptly terminating the application.