wildfly / wildfly-plugin-tools

A group of tools for interacting/managing with a WildFly container

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WildFly Plugins Tools

The WildFly Plugins Tools offers some simple API’s for deploying applications to WildFly or JBoss EAP and tooling around provisioning a server.

Deployment Manager

The deployment manager can be used to deploy or redeploy content to a running server as well as undeploy content. It works with both standalone servers and managed domains.

A simple example is deploying a WAR from the file system.

final Path deploymentPath = Paths.get(System.getProperty("user.home"), "projects", "myapp", "target", "myapp.war");
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
    final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
    final Deployment deployment = Deployment.of(deploymentPath);
    deploymentManager.forceDeploy(deployment).assertSuccess();
}

You can also deploy an input stream.

final String deploymentName = "example.war";
final WebArchive archive = ShrinkWrap.create(WebArchive.class, deploymentName);
archive.add(EmptyAsset.INSTANCE, "META-INF/beans.xml");
archive.addPackage("org.jboss.example");
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
    final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
    final Deployment deployment = Deployment.of(archive.as(ZipExporter.class).exportAsInputStream(), deploymentName);
    deploymentManager.forceDeploy(deployment).assertSuccess();
}
Managed Domain Example
final Path deploymentPath = Paths.get(System.getProperty("user.home"), "projects", "myapp", "target", "myapp.war");
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
    final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
    final Deployment deployment = Deployment.of(deploymentPath)
        .addServerGroups("main-server-group", "other-server-group");
    deploymentManager.deploy(deployment).assertSuccess();
}
Redeploy Example
final Path deploymentPath = Paths.get(System.getProperty("user.home"), "projects", "myapp", "target", "myapp.war");
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
    final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
    if (deploymentManager.hasDeployment(deploymentPath.getFileName().toString())) {
        deploymentManager.redeploy(Deployment.of(deploymentPath)).assertSuccess();
    }
}
Undeploy Example
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
    final DeploymentManager deploymentManager = DeploymentManager.Factory.create(client);
    final DeploymentResult result = deploymentManager.undeploy(UndeployDescription.of("example.war").setFailOnMissing(true));
    if (!result.successful()) {
        logger.errorf("Failed to undeploy example.war. %s", result.getFailureMessage());
    }
}

Deployment Operation Helper

There is a helper if you’d rather execute operations on your own as well using the org.wildfly.plugin.core.DeploymentOperations.

final Path deploymentPath = Paths.get(System.getProperty("user.home"), "projects", "myapp", "target", "myapp.war");
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
    final Operation op = DeploymentOperations.createDeployOperation(Deployment.of(deploymentPath));
    final ModelNode outcome = client.execute(op);
    if (!Operations.isSuccessfulOutcome(outcome)) {
        throw new DeploymentException(Operations.getFailureDescription(outcome).asString());
    }
}

Server Utilities

You can also use the org.wildfly.plugin.tools.server.DomainManager, org.wildfly.plugin.tools.server.ServerManager and org.wildfly.plugin.tools.server.StandaloneManager utilities to interact with a running server.

final Path wildflyHome = Paths.get(System.getProperty("user.home"), "servers", "wildfly-10.0.0.Final");
final Process process = Launcher.of(StandaloneCommandBuilder.of(wildflyHome)).launch();
try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
    final StandaloneManager serverManager = ServerManager.builder().client(client).process(process).standalone();
    // Wait at the maximum 30 seconds for the server to start
    if (!serverManager.waitFor(30, TimeUnit.SECONDS)) {
        throw new RuntimeException("Server did not start within 30 seconds.");
    }
    final Path deploymentPath = Paths.get(System.getProperty("user.home"), "projects", "myapp", "target", "myapp.war");
    final DeploymentManager deploymentManager = serverManager.deploymentManager();
    final Deployment deployment = Deployment.of(deploymentPath);
    deploymentManager.forceDeploy(deployment).assertSuccess();

    // Shutdown the standalone server
    serverManager.shutdown();
} finally {
    process.destroy();
}

About

A group of tools for interacting/managing with a WildFly container

License:Apache License 2.0


Languages

Language:Java 100.0%