Bootstrapping a Spring Application Context in a JEE RAR/EAR

The way of loading a Spring Application Context in Java Web Applications and JSE applications is commonly known. For web applications packaged in WAR files, in a web.xml file need to be declared a ContextLoaderListener and a contextConfigLocation context parameter which refers to XML Spring Application Context configurations. On the other hand, for JSE applications a Spring Application Context is loaded manually in code.
This article tries to present how to design a Java EE application that gives a possibility to load a Spring Application Context without using a WebApplicationContext. It is not always required to have a web nature of a Java enterprise application.

Background

It is feasible to load a Spring Application Context through a RAR. According to the Java EE specification the Resource Adapter Archive (RAR) is an archive that contains certain resources for Java EE Connector Architecture (JCA) applications. The exact explanation is available on [1]. The structure and packaging is presented on [2]. In a META-INF/ra.xml file inside a RAR, there is declared SpringContextResourceAdapter and specified a ContextConfigLocation property with value addressed to a Spring Application Context XML configuration. The generic idea and configuration is given on [3].

<?xml version="1.0" encoding="UTF-8"?>
<connector xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/connector_1_5.xsd"
 version="1.5">
 <vendor-name>Spring Framework</vendor-name>
 <eis-type>Spring Connector</eis-type>
 <resourceadapter-version>1.0</resourceadapter-version>
 <resourceadapter>
 <resourceadapter-class>org.springframework.jca.context.SpringContextResourceAdapter</resourceadapter-class>
 <config-property>
 <config-property-name>ContextConfigLocation</config-property-name>
 <config-property-type>java.lang.String</config-property-type>
 <config-property-value>classpath:META-INF/com.softexploration.lab.transfer.money.listener-context.xml</config-property-value>
 </config-property>
 </resourceadapter>
</connector>

Figure 1: RAR/META-INF/ra.xml example

In this example context configuration is placed somewhere under META-INF in a classpath.

1. Preparing a RAR

In order to prepare a RAR with a loadable Spring Application Context:

  1. Prepare a spring artifact with a Spring Application Context.
  2. Prepare a RAR resources following official guideline [2].
  3. In a RAR/META-INF/ra.xml set appropriate ContextConfigLocation to a Spring Application Context configuration location. The Spring Application Context can be included inside a RAR itself or can be placed in an outside resource when a RAR is deployed in an EAR.
  4. Assemble a RAR as a standalone archive or inside an EAR.

2. Demo system

A pretty simple system was designed to illustrate how a loadable Spring Application Context inside an EAR works in action.

There are three nodes:

JEE Application Server to run the com.softexploration.lab.transfer.money.app,

JSE Application Client to run com.softexploration.lab.transfer.money.client,

ActiveMQ Server to transfer messages between com.softexploration.lab.transfer.money.app and com.softexploration.lab.transfer.money.client
where
com.softexploration.lab.transfer.money.app plays role of server and com.softexploration.lab.transfer.money.client plays role of client. The purpose of this system is to allow transfer money on server side on requests received from clients.

Figure 2: Deployment diagram of the demo system

System requirements:

  1. JEE 6+ JDK
  2. JEE 5+ compliant Application Server
  3. Apache ActiveMQ TM Server 5.8.0
  4. Apache Maven 3

It was developed and ran using followings:

  1. Oracle JDK javac 1.7.0_01
  2. Oracle WebLogic Server Version: 10.3.3.0
  3. Apache ActiveMQ TM Server 5.8.0
  4. Apache Maven 3.0.3

Server [com.softexploration.lab.transfer.money.app]

server source code [1]

There is presented a simple com.softexploration.lab.transfer.money.app which uses a RAR to load a Spring Application Context. This is an EAR application (com.softexploration.lab.transfer.money.app) which contains two spring artifacts (com.softexploration.lab.transfer.money.service, com.softexploration.lab.transfer.money.listener) and a RAR (com.softexploration.lab.transfer.money.resource) which refers directly to the main artifact (com.softexploration.lab.transfer.money.listener). Application receives requests asynchronously through a MessageListener and then responses sending a JMS message to a destination specified by a requester. Project has a Maven structure and is presented in Figure 3.

Figure 3: Component diagram of the com.softexploration.lab.transfer.money.app

List of Maven artifacts:

  • com.softexploration.lab.transfer.money.service – business logic to make a money transfer
  • com.softexploration.lab.transfer.money.listener – listener which receives requests and delegates them to the com.softexploration.lab.transfer.money.service module and finally sends JMS responses; here is exposed the main Spring Application Context
  • com.softexploration.lab.transfer.money.resource – RAR module which declares SpringContextResourceAdapter and refers to the com.softexploration.lab.transfer.money.listener module; assembles a RAR
  • com.softexploration.lab.transfer.money.app – EAR module; assembles the whole application into an EAR
  • com.softexploration.lab.transfer.money.build – auxiliary artifact to build this project

In order to build this application:

  1. Go to com.softexploration.lab.transfer.money.build artifact location on a command line and execute: mvn clean install
  2. Under location of com.softexploration.lab.transfer.money.app in the target folder a com.softexploration.lab.transfer.money.app-0.0.1-SNAPSHOT.ear will be created

Client [com.softexploration.lab.transfer.money.client]

client source code [2]

This is a simple Java client which sends request to make a money transfer. App com.softexploration.lab.transfer.money.client has Maven artifact structure.

List of Maven artifacts:

  • com.softexploration.lab.transfer.money.client – this is the only artifact of the client side

In order to build this application:

  1. Go to com.softexploration.lab.transfer.money.client artifact location on a command line and execute: mvn clean install
  2. Under location of com.softexploration.lab.transfer.money.client in the target folder a com.softexploration.lab.transfer.money.client-0.0.1-SNAPSHOT-jar-with-dependencies.jar will be created

ActiveMQ Server

Apache ActiveMQ TM is the open source messaging and Integration Patterns server. It is used here to transfer messages between Money Transfer App and a JSE Client.

In order to run the messaging server:

  1. Prepare Apache ActiveMQ Server. It is possible to download it from [4]. We assume default settings, particularly broker URL – tcp://localhost:61616
  2. Start up the ActiveMQ Server by executing ACTIVE_MQ_SERVER_HOME_DIR/bin/ activemq.bat (activemq.sh)

Finishing

Now both the server and the client side are compiled and the messaging server is running.

The next steps:

  1. Deploy com.softexploration.lab.transfer.money.app-0.0.1-SNAPSHOT.ear on a JEE Application Server
  2. Run JSE Client, go to the target folder of the com.softexploration.lab.transfer.money.client artifact on a command line and execute: java -jar com.softexploration.lab.transfer.money.client-0.0.1-SNAPSHOT-jar-with-dependencies.jar

The output should be similar to this:
code [-1], msg [null]
code [0], msg [OK]

3. Summary

This post could give an approach to prepare a Java EE application with a loadable Spring Application Context. The pivotal part is a configuration inside a RAR/META-INF/ra.xml file. Simple demo is a complete solution which uses a RAR assembled within an EAR. If you are interested in the simple demo system you can go for source code in the Resources section.

References

[1] What Is a Resource Adapter? – http://docs.oracle.com/javaee/7/tutorial/doc/resources001.htm

[2] Packaging Resource Adapter Archives – http://docs.oracle.com/javaee/7/tutorial/doc/packaging004.htm

[3] Class Spring Application ContextResourceAdapter – http://static.springsource.org/spring-framework/docs/current/javadoc-api/index.html?org/springframework/jca/context/Spring Application ContextResourceAdapter.html

[4] ActiveMQ Server – http://activemq.apache.org/activemq-580-release.html

Resources

[1] Server [com.softexploration.lab.transfer.money.app] | GitHub [softexploration/lab]

[2] Client [com.softexploration.lab.transfer.money.client] | GitHub [softexploration/lab]

2 thoughts on “Bootstrapping a Spring Application Context in a JEE RAR/EAR

    1. admin Post author

      The source code is already here. See points [1] and [2] in Resources section of the article.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>