Category Archives: as7

Logging with JBoss as7 Logger

If you want log4j-style logging under a JBoss AS7 project, just include the seam solder jar which abstracts the log4j-logger and use the org.jboss.logging.Logger as log-provider.

For maven:

<dependency>
	<groupId>org.jboss.seam.solder</groupId>
	<artifactId>seam-solder</artifactId>
	<version>3.0.0.Final</version>
</dependency>

And if you want to use all loglevels you can change your loglevel in your standalone.xml file by changing the subsystem. I set the root logger and my own package to debug and made the others less verbose.

<subsystem xmlns="urn:jboss:domain:logging:1.1">
	<console-handler name="CONSOLE" autoflush="true">
		<level name="DEBUG" />
		<formatter>
			<pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n" />
		</formatter>
	</console-handler>
	<periodic-rotating-file-handler name="FILE"
		autoflush="true">
		<level name="INFO" />
		<formatter>
			<pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n" />
		</formatter>
		<file relative-to="jboss.server.log.dir" path="server.log" />
		<suffix value=".yyyy-MM-dd" />
		<append value="true" />
	</periodic-rotating-file-handler>
	<logger category="at.coffeebeans">
		<level name="DEBUG" />
	</logger>
	<logger category="com">
		<level name="INFO" />
	</logger>
	<logger category="sun">
		<level name="INFO" />
	</logger>
	<logger category="org">
		<level name="INFO" />
	</logger>
	<logger category="javax">
		<level name="INFO" />
	</logger>
	<root-logger>
		<level name="DEBUG" />
		<handlers>
			<handler name="CONSOLE" />
			<handler name="FILE" />
		</handlers>
	</root-logger>
</subsystem>

Don’t forget to restart your server after changing standalone.xml.

SOAP WebServices with JBoss AS7

If you want to create Soap WebServices you first have to extend your standalone.xml file. Add the module and it’s configuration:

...
<extensions>
	...
	<extension module="org.jboss.as.webservices"/>
	...
</extensions>
...
<profile>
	...
	<subsystem xmlns="urn:jboss:domain:webservices:1.0" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:jaxwsconfig="urn:jboss:jbossws-jaxws-config:4.0">
            <wsdl-host>
                localhost
            </wsdl-host>
            <modify-wsdl-address>
                true
            </modify-wsdl-address>
            <endpoint-config>
                <jaxwsconfig:config-name>
                    Standard-Endpoint-Config
                </jaxwsconfig:config-name>
            </endpoint-config>
            <endpoint-config>
                <jaxwsconfig:config-name>
                    Recording-Endpoint-Config
                </jaxwsconfig:config-name>
                <jaxwsconfig:pre-handler-chains>
                    <javaee:handler-chain>
                        <javaee:protocol-bindings>
                            ##SOAP11_HTTP ##SOAP11_HTTP_MTOM ##SOAP12_HTTP                                                                                                                                                                                                                         							##SOAP12_HTTP_MTOM
                        </javaee:protocol-bindings>
                        <javaee:handler>
                            <javaee:handler-name>
                                RecordingHandler
                            </javaee:handler-name>
                            <javaee:handler-class>
                             org.jboss.ws.common.invocation.RecordingServerHandler
                            </javaee:handler-class>
                        </javaee:handler>
                    </javaee:handler-chain>
                </jaxwsconfig:pre-handler-chains>
            </endpoint-config>
        </subsystem>
        ...
</profile>

You find a template in standalone-ha.xml.

And here an example WebService with Interface.

import java.util.logging.Logger;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.jws.WebService;
import javax.xml.ws.soap.Addressing;
import javax.xml.ws.soap.MTOM;

/**
 *
 * @author manuel
 *
 */
@Stateless
@WebService(endpointInterface = "at.coffeebeans.shop.soap.SoapServiceIface", serviceName = "SoapService", portName = "SoapServicePort")
@MTOM(enabled = true)
@Addressing(enabled = true, required = true)
public class SoapService implements SoapServiceIface {

	@Inject
	private Logger log;

	@Override
	public String ping(final String payload) throws SoapServiceError {
		if (null == payload) {
			log.warning("ping with no payload");
			throw new SoapServiceError("payload must not be null");
		}
		log.info("ping from " + payload);
		return "pong from " + payload;
	}

}
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author manuel
 *
 */
@WebService(targetNamespace = "http://coffeebeans.at/shop/soap")
public interface SoapServiceIface {

	@WebMethod(operationName = "ping")
	public String ping(@WebParam(name = "payload") final String payload) throws SoapServiceError;

}
import javax.xml.ws.WebFault;

/**
 * @author manuel
 *
 */
@WebFault
public class SoapServiceError extends Exception {

	/** UID. */
	private static final long """"serialVersionUID"""" = 5063725310269125585L;

	public SoapServiceError() {
		super();
	}

	public SoapServiceError(final Exception exc) {
		super(exc);
	}

	public SoapServiceError(final String msg) {
		super(msg);
	}

}

In my example, the project is accessible over http://localhost:8080/shop and the WebService WSDL is under http://localhost:8080/shop/SoapService/SoapService?wsdl

If you use maven, the following dependencies should add full WebService support that is included in JBoss AS7:

<dependencies>
	<dependency>
		<groupId>org.jboss.ws</groupId>
		<artifactId>jbossws-api</artifactId>
		<version>1.0.0.Beta2</version>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>org.apache.ws.security</groupId>
		<artifactId>wss4j</artifactId>
		<version>1.6.1</version>
		<scope>provided</scope>
	</dependency>
</dependencies>

PostgreSQL Datasource in JBoss AS7

First add a postgresql module to your modules folder as described in my former post. Then open standalone.xml and insert a datasource and the postgresql driver.

Search for subsystem xmlns=”urn:jboss:domain:datasources:1.0″ and insert the datasource.

<datasource jndi-name="java:jboss/datasources/TestDS"
	pool-name="TESTDS" enabled="true" jta="true" use-java-context="true"
	use-ccm="true">
	<connection-url>
		jdbc:postgresql:test
	</connection-url>
	<driver>
		org.postgresql
	</driver>
	<pool>
		<min-pool-size>
			5
		</min-pool-size>
		<max-pool-size>
			25
		</max-pool-size>
		<prefill>
			false
		</prefill>
		<use-strict-min>
			false
		</use-strict-min>
		<flush-strategy>
			FailingConnectionOnly
		</flush-strategy>
	</pool>
	<security>
		<user-name>
			someuser
		</user-name>
		<password>
			somepass
		</password>
	</security>
	<validation>
		<check-valid-connection-sql>
			SELECT 1
		</check-valid-connection-sql>
		<validate-on-match>
			false
		</validate-on-match>
		<background-validation>
			false
		</background-validation>
		<use-fast-fail>
			false
		</use-fast-fail>
	</validation>
</datasource>

Then insert the driver under the driver section below.

<driver name="org.postgresql" module="org.postgresql">
	<xa-datasource-class>
		org.postgresql.xa.PGXADataSource
	</xa-datasource-class>
</driver>

JEE6 CDI and JSF annotation hell

If you are in a JEE6 environment with CDI and JSF be very careful with choosing annotations. Always prefer the ones from javax.enterprise.context.* if there are also the same ones under javax.faces.. as JSF you surely run into troubles if you mix them. CDI annoteted beans are managed by the container and beans annotated with the annotations from faces are managed by the JSF container. E.g. this can result in SessionScoped beans that are initialized more than once for a single session.

Generally I think you should not use the @ManagedBean annotation in a JEE6 environment. I prefer @Named.

JBoss Postgresql Module

Just download the latest jdbc4 driver and place it into JBOSS_HOME/modules/org/postgresql/main. Then create a file module.xml in the same folder with the following content (replace filename if necessary):

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="org.postgresql">
    <resources>
        <resource-root path="postgresql-9.1-901.jdbc4.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>