|
| 1 | +## Introduction |
| 2 | + |
| 3 | +This application is intended to run inside of a Java EE application server such as |
| 4 | +JBoss Wildfly. It demonstrates Spring Boot's auto-configuration defaulting for a |
| 5 | +container-managed `TransactionManager` and `DataSource`. This example unfortunately |
| 6 | +requires a fully configured Wildfly installation. You'll need to configure a PostgreSQL |
| 7 | +XA `DataSource` and an XA `ConnectionFactory` in the Java EE application server's |
| 8 | +JNDI machinery. |
| 9 | + |
| 10 | +## Setup |
| 11 | + |
| 12 | +### Postgres |
| 13 | +We will use postgres as the underlying database, v9.3.5 or above is recommend. Follow |
| 14 | +the installation instructions from http://www.postgresql.org/[postgresql.org] or use |
| 15 | +a package manager to install the appropriate binaries. |
| 16 | + |
| 17 | +Once installed you will need to initialize and start the server. |
| 18 | + |
| 19 | +[source,indent=0] |
| 20 | +---- |
| 21 | + $ initdb /usr/local/var/postgres -E utf8 |
| 22 | + $ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start |
| 23 | +---- |
| 24 | + |
| 25 | +With the server running you can create a user and a database: |
| 26 | + |
| 27 | +[source,indent=0] |
| 28 | +---- |
| 29 | + $ createuser springboot |
| 30 | + $ createdb bootdemo |
| 31 | +---- |
| 32 | + |
| 33 | +Finally you can type `psql bootdemo` to configure a password: |
| 34 | + |
| 35 | +[source,indent=0] |
| 36 | +---- |
| 37 | + ALTER USER springboot WITH PASSWORD 'springboot'; |
| 38 | + \q |
| 39 | +---- |
| 40 | + |
| 41 | + |
| 42 | +### WildFly 8.1 |
| 43 | +Download an install WildFly 8.1 from http://wildfly.org/downloads/[wildfly.org]. Once |
| 44 | +installed you will need to add a management user by running `$JBOSS_HOME/bin/add-user.sh` |
| 45 | +(see the WildFly documentation for details). |
| 46 | + |
| 47 | +You will also need to add a postgresql module. The following commands setup the basic |
| 48 | +structure: |
| 49 | + |
| 50 | +[source,indent=0] |
| 51 | +---- |
| 52 | + $ cd $JBOSS_HOME |
| 53 | + mkdir -p modules/org/postgresql/main |
| 54 | + wget http://jdbc.postgresql.org/download/postgresql-9.3-1102.jdbc41.jar |
| 55 | + mv postgresql-9.3-1102.jdbc41.jar modules/org/postgresql/main |
| 56 | +---- |
| 57 | + |
| 58 | +You can then add the following to `$JBOSS_HOME/modules/org/postgresql/main/module.xml`: |
| 59 | + |
| 60 | +[source,indent=0] |
| 61 | +---- |
| 62 | + <?xml version="1.0" encoding="UTF-8"?> |
| 63 | + <module xmlns="urn:jboss:module:1.0" name="org.postgresql"> |
| 64 | + <resources> |
| 65 | + <resource-root path="postgresql-9.3-1102.jdbc41.jar"/> |
| 66 | + </resources> |
| 67 | + <dependencies> |
| 68 | + <module name="javax.api"/> |
| 69 | + <module name="javax.transaction.api"/> |
| 70 | + </dependencies> |
| 71 | + </module> |
| 72 | +---- |
| 73 | + |
| 74 | +## Configuration |
| 75 | +A custom WildFly configuration is required for the XA `DataSource` and `ConnectionFactory` |
| 76 | +elements. The `$JBOSS_HOME/standalone/configuration/standalone-full.xml` is a good |
| 77 | +starting point, copy this file to |
| 78 | +`$JBOSS_HOME/standalone/configuration/standalone-boot-demo.xml` then make the following |
| 79 | +changes. |
| 80 | + |
| 81 | +### DataSource |
| 82 | +You need to register a PostgreSQL XA `Driver` and then configure an `xa-datasource`. |
| 83 | + |
| 84 | +Here's a complete listing of the `xa-datasource` contribution to the `datasources` |
| 85 | +element, and the `driver` contribution to the `drivers` element to configure a PostgreSQL |
| 86 | +DB connection to localhost. |
| 87 | +https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6/html-single/Administration_and_Configuration_Guide/index.html#Install_a_JDBC_Driver_with_the_Management_Console[You can learn more from the documentation]. |
| 88 | + |
| 89 | +[source,xml,indent=0,subs="verbatim,attributes"] |
| 90 | +---- |
| 91 | + <datasources> |
| 92 | + ... |
| 93 | + <xa-datasource |
| 94 | + jndi-name="java:jboss/datasources/bootdemo" |
| 95 | + pool-name="CrmXADS" |
| 96 | + enabled="true"> |
| 97 | + <xa-datasource-property name="url">jdbc:postgresql://localhost:5432/crm</xa-datasource-property> |
| 98 | + <driver>postgres</driver> |
| 99 | + <xa-pool> |
| 100 | + <min-pool-size>10</min-pool-size> |
| 101 | + <max-pool-size>20</max-pool-size> |
| 102 | + <prefill>true</prefill> |
| 103 | + </xa-pool> |
| 104 | + <security> |
| 105 | + <user-name>springboot</user-name> |
| 106 | + <password>springboot</password> |
| 107 | + </security> |
| 108 | + </xa-datasource> |
| 109 | + <drivers> |
| 110 | + ... |
| 111 | + <driver name="postgres" module="org.postgresql"> |
| 112 | + <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class> |
| 113 | + </driver> |
| 114 | + </drivers> |
| 115 | + </datasources> |
| 116 | +---- |
| 117 | + |
| 118 | +### JMS Destination |
| 119 | +You will also need to configure a `javax.jms.Destination` by contributing the following to |
| 120 | +the `hornetq-server` element: |
| 121 | + |
| 122 | +[source,xml,indent=0,subs="verbatim,attributes"] |
| 123 | +---- |
| 124 | + <jms-destinations> |
| 125 | + <jms-queue name="accounts"> |
| 126 | + <entry name="java:/jms/queue/bootdemo"/> |
| 127 | + </jms-queue> |
| 128 | + ... |
| 129 | + </jms-destinations> |
| 130 | +---- |
| 131 | + |
| 132 | + |
| 133 | +## Running and deploying the sample |
| 134 | +Run Wildfly with the following command: |
| 135 | + |
| 136 | +[source,indent=0] |
| 137 | +---- |
| 138 | + $JBOSS_HOME/bin/standalone.sh -c standalone-boot-demo.xml |
| 139 | +---- |
| 140 | + |
| 141 | +Once running you can deploy the application by copying |
| 142 | +`target/spring-boot-sample-jta-jndi.war` to `$JBOSS_HOME/standalone/deployments`. |
| 143 | + |
| 144 | +Open a browser to http://localhost:8080/spring-boot-sample-jta-jndi to trigger the |
| 145 | +sample. You should see the current count (it will increment by one on each refresh). If |
| 146 | +you check the logs you should see a `----> Josh` message and some counts. Notice how the |
| 147 | +`error` message triggers an exception with causes both the database insert and the JMS |
| 148 | +message to be rolled back. |
0 commit comments