With the 7.0.1 release of AS7 comes a new release of the JBoss AS7 Maven Plugin. In addition to allowing you to deploy your apps to AS7, the plugin now has the ability to automatically add datasources to a running AS7 instance.

Deploying a datasource is a two stage process, first we have to deploy the driver, then we setup a datasource through the AS7 management api. The example below walks you through the process of deploying a postgresql datasource, but for the impatient among you a complete example can be found here.

Deploying a database driver

To deploy a database driver we first have to list our driver in the <dependencies> section of pom.xml:

<dependencies>
  <dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>8.4-702.jdbc4</version>
  </dependency>
</dependencies>

We then have to setup the JBoss AS7 Maven plugin to deploy the datasource to the app server:

<plugin>
    <groupId>org.jboss.as.plugins</groupId>
    <artifactId>jboss-as-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>deploy-driver</id>
            <phase>package</phase>
            <configuration>
                <groupId>postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <name>postgresql.jar</name>
            </configuration>
            <goals>
                <goal>deploy-artifact</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Running mvn package will now connect to a running AS7 instance on localhost and deploy the driver as a deployment named postgresql.jar.

Note that this capability it not limited to just deploying database drivers, using the deploy-artifact goal it is possible to deploy any maven artifact to the app server.

Creating the DataSource

Now that we have our driver deployed, we need to create the DataSource. To do this, we will need to update our pom again so it looks like this:

<plugin>
    <groupId>org.jboss.as.plugins</groupId>
    <artifactId>jboss-as-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>deploy-driver</id>
            <phase>package</phase>
            <configuration>
                <groupId>postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <name>postgresql.jar</name>
            </configuration>
            <goals>
                <goal>deploy-artifact</goal>
            </goals>
        </execution>
        <execution>
            <id>add-datasource</id>
            <phase>install</phase>
            <configuration>
                <address>subsystem=datasources,data-source=myDatasource</address>
                <properties>
                    <connection-url>jdbc:postgresql://localhost/myDatabase</connection-url>
                    <jndi-name>myDatasource</jndi-name>
                    <enabled>true</enabled>
                    <pool-name>myPool</pool-name>
                    <user-name>dmusername</user-name>
                    <password>secret</password>
                    <driver-name>postgresql.jar</driver-name>
                </properties>
            </configuration>
            <goals>
                <goal>add-resource</goal>
            </goals>
        </execution>
    </executions>
</plugin>

With this configuration running mvn install should deploy a Postgresql datasource to AS7.

For some more examples, including XA DataSource and MySQL examples, please see my example pom.xml at https://github.com/stuartwdouglas/quickstart/blob/master/datasource/pom.xml.

How it works

You may have noticed about that the name of the maven goal is add-resource, rather than add-datasource. This is because the plugin is not limited to just adding datasources, but can also be used to add other resources to AS7. Internally the plugin uses the AS7 management API, and the <address> and <properties> elements correspond exactly to the management API / command line equivalents. This means that we can add other resources as well, e.g. JMS Queues.

The first thing we need to do is figure out the syntax we need to use to add a JMS Queue. To do this we are going to use the command line client. Fire up an instance of AS7, and then in another terminal window run jboss-admin.sh in the bin directory. You should see the following:

You are disconnected at the moment. Type 'connect' to connect to the server or 'help' for the list of supported commands.
[disconnected /]

Type connect and you should connect to the running AS7 instance. Now we need to figure out the syntax to add a JMS Queue. To do this we run the following command:

[standalone@localhost:9999 /] /subsystem=messaging/jms-queue=*:read-operation-description(name="add")

This should give us the following result:

{
    "outcome" => "success",
    "result" => {
        "operation-name" => "add",
        "description" => "Add a queue.",
        "request-properties" => {
            "entries" => {
                "type" => LIST,
                "description" => "The jndi names the queue will be bound to.",
                "required" => true,
                "nillable" => false
            },
            "selector" => {
                "type" => STRING,
                "description" => "The queue selector.",
                "required" => false,
                "nillable" => true
            },
            "durable" => {
                "type" => BOOLEAN,
                "description" => "Whether the queue is durable or not.",
                "required" => false,
                "nillable" => true
            }
        },
        "reply-properties" => {}
    }
}

This output means that the operation has three parameters, entries, selector and durable, Of type List, String, and Boolean respectively. Using this information we can add queue on the command line as follows:

[standalone@localhost:9999 /] /subsystem=messaging/jms-queue=cmdLineQueue:add(entries=["java:jboss/cmdLineQueue"], durable=true)
{"outcome" => "success"}

Now lets re-create this from the maven plugin:

<plugin>
    <groupId>org.jboss.as.plugins</groupId>
    <artifactId>jboss-as-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-queue</id>
            <phase>install</phase>
            <configuration>
                <address>subsystem=messaging,jms-queue=myQueue</address>
                <properties>
                    <entries>!!["java:jboss/myQueue", "java:jboss/anotherJndiBinding"]</entries>
                    <durable>true</durable>
                </properties>
            </configuration>
            <goals>
                <goal>add-resource</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The most interesting part of the above configuration is the <entries> element. Because the parameter is of a non-primitive type (in this case LIST), it must be specified as a DMR String. The !! escape as the start of the test tells the plugin to interpret it as such.


Back to top