HelloWorld Resource Adapter tutorial

Done with the Java classes. We will now add into the META-INF folder the ironjacamar.xml file which contains the connection definition which will be used by the resource adapter clients:

<ironjacamar>
  <connection-definitions>
    <connection-definition 
     class-name="com.sample.adapter.HelloWorldManagedConnectionFactory" 
     jndi-name="java:/eis/HelloWorld"/>
  </connection-definitions>
</ironjacamar>

In order to build the project, we will add the required dependencies to the pom.xml, including the RAR Maven plugin which will generate a RAR file, including the ironjacamar.xml descriptor:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.sample</groupId>
	<artifactId>jca-demo</artifactId>
	<version>1.0</version>
	<packaging>jar</packaging>

	<name>jca-demo</name>
	<url>http://maven.apache.org</url>

	<properties>
		<version.org.jboss.ironjacamar>1.0.9.Final</version.org.jboss.ironjacamar>
		 <version.wildfly.maven.plugin>1.0.2.Final</version.wildfly.maven.plugin>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.jboss.ironjacamar</groupId>
			<artifactId>ironjacamar-spec-api</artifactId>
			<version>${version.org.jboss.ironjacamar}</version>
			<scope>provided</scope>
		</dependency>

	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.jboss.ironjacamar</groupId>
				<artifactId>ironjacamar-depchain</artifactId>
				<version>${version.org.jboss.ironjacamar}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>

		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<excludes>
						<exclude>**/META-INF/*</exclude>
					</excludes>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-rar-plugin</artifactId>
				<version>2.2</version>
				<executions>
					<execution>
						<id>test</id>
						<goals>
							<goal>rar</goal>
						</goals>
						<configuration>

							<raXmlFile>src/main/resources/META-INF/ironjacamar.xml</raXmlFile>
						</configuration>
					</execution>
				</executions>
			</plugin>
            <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
                <version>${version.wildfly.maven.plugin}</version>
            </plugin>
		</plugins>
	</build>

</project>

Build and deploy the project with:

$ mvn clean install rar:rar wildfly:deploy

Finally, here's a Test Servlet which can be used to test our resource adapter:

package com.sample;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.resource.ResourceException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 import com.sample.adapter.HelloWorldConnection;
 import com.sample.adapter.HelloWorldConnectionFactory;

 
@WebServlet("/TestRA")
public class TestRA extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Resource(mappedName = "java:/eis/HelloWorld")
	private HelloWorldConnectionFactory connectionFactory;

	 public TestRA() {
	        super();
	        
	    }
	    /**
	     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	     */
	    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    	  String result = null;
	  	  HelloWorldConnection connection = null;
	        try {
	         	 connection = connectionFactory.getConnection();	  	       
		         result = connection.helloWorld();
		       
	        } catch (ResourceException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
 

	        PrintWriter out = response.getWriter();
	        out.println(result);

	        out.flush();
	      //  connection.close();
	    }

 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

In order to be able to run your client application provide a dependency through the WEB-INF/jboss-deployment-structure.xml file as follows:

<jboss-deployment-structure>
  <deployment>
    <dependencies>
      <module name="deployment.jca-demo-1.0.rar" />
    </dependencies>
  </deployment>
</jboss-deployment-structure> 

Now test your application:

$ curl http://localhost:8080/raclient/TestRA

HelloWorld WildFly!

Build the Project with Eclipse

If you are not using Maven to build your projects, you can still use a Wizard which is built-in with Eclipse. All you have to do is creating a new Connector Project from Eclipse (You can also use an ordinary Java Project, however the Connector project has the advantage of automatically publishing the project as RAR archive to WildFly ):

jca tutorial jboss rar resource adapter 

Your Connector classes will be, by default, placed into the connectorModule folder:

jca tutorial jboss rar resource adapter

Finally, the last screenshot allows you to include or not the standard descriptor for the resource adapter (ra.xml). This is not a mandatory step as we will use the custom resource adapter from the IronJacamer project named ironjacamar.xml.

jca tutorial jboss rar resource adapter

Finish by copying the source from guthub into the connectorModule folder and you are done!

References: http://www.ironjacamar.org/doc/userguide/1.2/en-US/html_single/index.html#sample_helloworld_resourceadpater

Follow us on Twitter