This repository has been archived by the owner on Jun 7, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
850e854
commit 7b48c9d
Showing
3 changed files
with
226 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/ |
Oops, something went wrong.