Container

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 Container prior to working with any deployments using the ShrinkWrap API.

It’s also recommended that the Container 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
        Container container = new Container();

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

        // Optionally add the default deployment to the archive
        // This will give you all of the project's Classes/Resources
        deployment.addAsLibrary(container.createDefaultDeployment());

        container.start();
        container.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 Container()
           .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(…​).