Archiv für den Monat: November 2017

Wildfly-Swarm – Database

Tried this with Wildfly Swarm version 2017.8.1.

Adding Oracle driver.

In the project pom.xml, add the driver as a dependency:

<dependency>
   <groupId>com.oracle</groupId>
   <artifactId>ojdbc7</artifactId>
   <version>12.1.0.2</version>
</dependency>

We have to tell Wildfly-Swarm, that there is a Oracle driver to use. Therefor we add module.xml to /src/main/resources/modules/com/oracle/ojdbc7/main/

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.3" name="com.oracle.ojdbc7">
   <resources>
      <artifact name="com.oracle:ojdbc7:12.1.0.2"/>
   </resources>
   <dependencies>
      <module name="javax.api"/>
      <module name="javax.transaction.api"/>
      <module name="javax.servlet.api" optional="true"/>
   </dependencies>
</module>

Add the datasource fraction as dependency:

<dependency>
   <groupId>org.wildfly.swarm</groupId>
   <artifactId>datasources</artifactId>
   <version>2017.8.1</version>
</dependency>

Define datasources

The files, which defines the datasources, are placed in /src/main/webapp/WEB-INF/ and have a naming convention *-ds.xml

example1-ds.xml:

<?xml version="1.0" encoding="UTF-8"?>
<datasources  
   xmlns="http://www.jboss.org/ironjacamar/schema"    
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
   xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
   <!-- The datasource is bound into JNDI at this location. We reference
        this in META-INF/persistence.xml -->
   <datasource
      jndi-name="java:jboss/datasources/example1"
      pool-name="example1-pool"
      enabled="true"
      use-java-context="true">
      <connection-url>jdbc:oracle:thin:@serverUrl1:1521:SID</connection-url>
      <driver>oracle</driver>
      <security>
         <user-name>username1</user-name>
         <password>password1</password>
      </security>
   </datasource>
</datasources>

example2-ds.xml:

<?xml version="1.0" encoding="UTF-8"?>
<datasources  
 xmlns="http://www.jboss.org/ironjacamar/schema"    
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
 xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
   <!-- The datasource is bound into JNDI at this location. We reference
        this in META-INF/persistence.xml -->
   <datasource
     jndi-name="java:jboss/datasources/example2"
     pool-name="example2-pool"
     enabled="true"
     use-java-context="true">
     <connection-url>jdbc:oracle:thin:@serverUrl2:1521:SID</connection-url>
      <driver>oracle</driver>
      <security>
         <user-name>username2</user-name>
         <password>password2</password>
      </security>
 </datasource>
</datasources>

Define the persistence unit

Place a persistence.xml into /src/main/resources/META-INF/. Here is an example with two datasources:

<?xml version="1.0" encoding="UTF-8"?>
<persistence
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">  
   <persistence-unit   
      name="example1PU"   
      transaction-type="JTA">
      <jta-data-source>java:jboss/datasources/example1</jta-data-source>
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
      <properties>
         <property name="javax.persistence.schema-generation.database.action" value=""/>
         <property name="javax.persistence.schema-generation.create-source" value="metadata"/>
         <property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
      </properties>
   </persistence-unit>
   <persistence-unit
      name="example2PU"
      transaction-type="JTA">
      <jta-data-source>java:jboss/datasources/example2</jta-data-source>
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
      <properties>
         <property name="javax.persistence.schema-generation.database.action" value=""/>
         <property name="javax.persistence.schema-generation.create-source" value="metadata"/>
         <property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
      </properties>
   </persistence-unit>
</persistence>

Reference the persistence unit

In your EJBs you can reference the persistence units:

@PersistenceContext(unitName = "example1PU" )
EntityManager em1;

@PersistenceContext(unitName = "example2PU" )
EntityManager em2;

Wildfly-Swarm – Logging

Tried this with Wildfly Swarm version 2017.8.1. Seems like in newer versions a new approach is used.

I added the logging artifact in the maven build file:

<dependency>
   <groupId>org.wildfly.swarm</groupId>
   <artifactId>logging</artifactId>
   <version>2017.8.1</version>
</dependency>

In /src/main/resources/ a file logging.properties with the following content is setup:

logger.level=INFO
logger.handlers=FILE

handler.FILE=org.jboss.logmanager.handlers.FileHandler
handler.FILE.level=INFO
handler.FILE.formatter=PATTERN
handler.FILE.properties=append,fileName,autoFlush
handler.FILE.append=false
handler.FILE.autoFlush=true
handler.FILE.fileName=./myapp.log

formatter.PATTERN=org.jboss.logmanager.formatters.PatternFormatter
formatter.PATTERN.properties=pattern
formatter.PATTERN.pattern=%d{HH:mm:ss,SSS} %-5p [%c] %s%E%n

In your code, you setup and use org.jboss.logging.Logger instances like this:

Logger LOG = Logger.getLogger(MyClass.class);
LOG.info("Example log message");
LOG.error("Something strange happend", new Exception() );

Wildfly-Swarm – Basic setup

Tried this with Wildfly Swarm version 2017.8.1.

In the maven pom.xml the following entries are added.

<dependencyManagement>
   <dependencies>
      <dependency>
         <groupId>org.wildfly.swarm</groupId>
         <artifactId>bom-all</artifactId>
         <version>2017.8.1</version>
         <scope>import</scope>
         <type>pom</type>
      </dependency>
   </dependencies>
</dependencyManagement>

 

For building the uberjar or executing the plugin needs to be added.

<plugins>
   <plugin>
      <groupId>org.wildfly.swarm</groupId>
      <artifactId>wildfly-swarm-plugin</artifactId>
      <version>${version.wildfly.swarm}</version>
      <executions>
         <execution>
            <goals>
               <goal>package</goal>
            </goals>
         </execution>
      </executions>
   </plugin>
</plugins>

 

Creation of the uberjar happens, when you call package goal:

mvn package

 

The uberjar could be found in the target folder. Execute it with java -jar:

java -jar ./target/myapp-swarm.jar

 

If you want to debug your code, run it from maven, add the following parameter to your maven command and do a remote debugging session in your ide. Beware, that the Wildfly-Swarm process stops until a connection is open. Here we open a debug port on 8888.

maven run -Ddebug=8888