Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

Commit

Permalink
Another README.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobmcwhirter committed Dec 14, 2015
1 parent 850e854 commit 7b48c9d
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 187 deletions.
64 changes: 38 additions & 26 deletions datasource/datasource-deployment/README.adoc
Expand Up @@ -13,20 +13,25 @@ a _deployment_.

The project is a normal maven project with `jar` packaging, not `war`.

<packaging>jar</packaging>
[source,xml]
----
<packaging>jar</packaging>
----

The project adds a `<plugin>` to configure `wildfly-swarm-plugin` to
create the runnable `.jar`.

<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<version>${version.wildfly-swarm}</version>
<configuration>
<mainClass>org.wildfly.swarm.examples.ds.deployment.Main</mainClass>
</configuration>
...
</plugin>
[source,xml]
----
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<configuration>
<mainClass>org.wildfly.swarm.examples.ds.deployment.Main</mainClass>
</configuration>
...
</plugin>
----

As with the other examples, this one inherits a few standard executions
from the parent `pom.xml`, particularly the `package` execution.
Expand All @@ -35,28 +40,35 @@ To define the needed parts of WildFly Swarm, a few dependencies are added.
The first is not strictly required for doing datasource deployments, but is
needed for the example to demonstrate usage of the deployed datasource:

<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-jaxrs</artifactId>
<version>${version.wildfly-swarm}</version>
</dependency>
[source,xml]
----
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-jaxrs</artifactId>
</dependency>
----

To support datasources in general, the next dependency is required.
To support datasources in general, the next dependency is required:

<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-datasources</artifactId>
<version>${version.wildfly-swarm}</version>
</dependency>
[source,xml]
----
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-datasources</artifactId>
</dependency>
----

Additionally, the JDBC driver jar you wish to deploy is specified as a dependency
within your `pom.xml`

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.187</version>
</dependency>
[source,xml]
----
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.187</version>
</dependency>
----

== Project `main()`

Expand Down
188 changes: 188 additions & 0 deletions datasource/datasource-subsystem/README.adoc
@@ -0,0 +1,188 @@
= Datasource via Fraction

> Please raise any issues found with this example in this repo:
> https://github.com/wildfly-swarm/wildfly-swarm-examples
>
> Issues related to WildFly Swarm core should be raised in the main repo:
> https://github.com/wildfly-swarm/wildfly-swarm/issues

This example demonstrates how to create a datasource through
fraction configuration.

Deploying a JDBC driver through this method requires that
the driver be a part of the JBoss Modules module tree.

Just as WildFly-Swarm can provide portions of the module tree
through various jars, so can your own application.

By creating `src/main/resources/modules/com/h2database/h2/main/module.xml`,
this new module is made available during the boot of the app-server.

The `module.xml` references the `h2.jar` through Maven coordinates and
property substitution:

[source,xml]
----
<resources>
<artifact name="com.h2database:h2:${version.h2}"/>
</resources>
----

The `${version.h2}` is simply a property defined in your `pom.xml`,
and normal Maven resource-filtering replaces it, to avoid having
to hard-code it.

== Project `pomx.xml`

The project is a normal maven project with `jar` packaging, not `war`.

[source,xml]
----
<packaging>jar</packaging>
----

The project adds a `<plugin>` to configure `wildfly-swarm-plugin` to
create the runnable `.jar`. Additional configuration parameters are
added to instruct the plugin to include the `com.h2database.h2` module
from the WildFly distribution. This allows access to the H2 driver
jar.

[source,xml]
----
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<configuration>
<mainClass>org.wildfly.swarm.examples.ds.subsystem.Main</mainClass>
</configuration>
....
</plugin>
----

As with the other examples, this one inherits a few standard executions
from the parent `pom.xml`, particularly the `package` execution.

To define the needed parts of WildFly Swarm, a few dependencies are added.
The first is not strictly required for configuring datasources, but is
needed for the example to demonstrate usage of the datasource:

[source,xml]
----
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-jaxrs</artifactId>
</dependency>
----

To support datasources in general, the next dependency is required:

[source,xml]
----
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-datasources</artifactId>
</dependency>
----

The `wildfly-swarm-jaxrs` dependency allows usage of ShrinkWrap APIs within the `main()` in addition
to providing the JAX-RS APIs. The `wildfly-swarm-datasources` dependency provides configuration
classes for adding the JDBC driver and datasources to the container.

== Project `main()`

Since this project deploys JAX-RS resources without a `.war` being construction, it
provides its own `main()` method (specified above via the `maven-jar-plugin`) to
configure the container and deploy the resources programatically.


[source,java]
----
package org.wildfly.swarm.examples.ds.subsystem;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.wildfly.swarm.container.Container;
import org.wildfly.swarm.datasources.DatasourcesFraction;
import org.wildfly.swarm.jaxrs.JAXRSArchive;
public class Main {
public static void main(String[] args) throws Exception {
Container container = new Container();
// Configure the Datasources subsystem with a driver
// and a datasource
container.fraction(new DatasourcesFraction()
.jdbcDriver("h2", (d) -> {
d.driverDatasourceClassName("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("sa");
ds.password("sa");
})
);
// Start the container
container.start();
JAXRSArchive appDeployment = ShrinkWrap.create(JAXRSArchive.class);
appDeployment.addResource(MyResource.class);
// Deploy your app
container.deploy(appDeployment);
}
}
----

This method constructs a new default Container, which automatically
initializes all fractions (or subsystems) that are available. The datasources
fraction has no particular default configuration, so by providing a
specific configuration we enable a driver and a datasource.

JNDI names are bound automatically.

The empty container is started.

A `JAXRSDeployment` is constructed, and the JAX-RS resource class is
added to it and then deployed to the container.

The resource looks up the Datasource through JNDI at run-time:


[source,java]
----
@Path("/")
public class MyResource {
@GET
@Produces("text/plain")
public String get() throws NamingException, SQLException {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("jboss/datasources/ExampleDS");
Connection conn = ds.getConnection();
try {
return "Howdy using connection: " + conn;
} finally {
conn.close();
}
}
}
----


== Run

You can run it many ways:

* mvn package && java -jar ./target/example-datasource-subsystem-swarm.jar
* mvn wildfly-swarm:run
* In your IDE run the `org.wildfly.swarm.examples.ds.subsystem.Main` class

== Use

http://localhost:8080/

0 comments on commit 7b48c9d

Please sign in to comment.