HelloWorld Resource Adapter tutorial

[Updated] In this tutorial we will show how you can create build and deploy an HelloWorld Resource Adapter and deploy it on WildFly 10.

The Java Connector Architecture (JCA) defines a standard architecture for connecting the Java EE platform to heterogeneous Enterprise Information Systems (EIS). Examples of EISs include Enterprise Resource Planning (ERP), mainframe transaction processing (TP), database and messaging systems.

The IronJacamar project implements the Java Connector Architecture 1.6 specification and is available in a stable version, and a developer version.

In order to create your own Adapter, you need to define your Contract in a Resource Adapter Connection class, define a Factory for creating your Connection to the Resource Adapter and provide the Connector Managed classes which are used to creates a new physical connection to the underlying EIS resource manager and communicate with it.

Create a Resource Adapter from a Maven project

We will first show how to create a new Resource Adapter from a Maven project. Create a new Maven Java Project as follows:

$ mvn archetype:generate -DgroupId=com.sample -DartifactId=jcademo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

We will not expose all the project classes but just the core parts of it (You can the project on github at. https://github.com/fmarchioni/mastertheboss/tree/master/jca-demo).

The first step will be defining your connector exposed methods in the HelloWorldConnection class

package com.sample.adapter;
 
public interface HelloWorldConnection
{

  public String helloWorld();
  public String helloWorld(String name);

  public void close();
}

Next, your main concern will be implementing the Managed Connection class which contains the actual implementation for our method write which writes our Hello World message to a Stream:

package com.sample.adapter;


import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.resource.NotSupportedException;
import javax.resource.ResourceException;
import javax.resource.spi.ConnectionEvent;
import javax.resource.spi.ConnectionEventListener;
import javax.resource.spi.ConnectionRequestInfo;
import javax.resource.spi.LocalTransaction;
import javax.resource.spi.ManagedConnection;
import javax.resource.spi.ManagedConnectionMetaData;

import javax.security.auth.Subject;
import javax.transaction.xa.XAResource;


public class HelloWorldManagedConnection implements ManagedConnection
{
   /** The logger */
   private static Logger log = Logger.getLogger("HelloWorldManagedConnection");

   /** MCF */
   private HelloWorldManagedConnectionFactory mcf;

   /** Log writer */
   private PrintWriter logWriter;

   /** Listeners */
   private List<ConnectionEventListener> listeners;

   /** Connection */
   private Object connection;


   public HelloWorldManagedConnection(HelloWorldManagedConnectionFactory mcf)
   {
      this.mcf = mcf;
      this.logWriter = null;
      this.listeners = new ArrayList<ConnectionEventListener>(1);
      this.connection = null;
   }


   public Object getConnection(Subject subject,
                               ConnectionRequestInfo cxRequestInfo) 
      throws ResourceException
   {
      connection = new HelloWorldConnectionImpl(this, mcf);

      return connection;
   }


   public void associateConnection(Object connection) throws ResourceException
   {
      this.connection = connection;
   }


   public void cleanup() throws ResourceException
   {
   }


   public void destroy() throws ResourceException
   {
      this.connection = null;
   }


   public void addConnectionEventListener(ConnectionEventListener listener)
   {
      if (listener == null)
         throw new IllegalArgumentException("Listener is null");

      listeners.add(listener);
   }

   public void removeConnectionEventListener(ConnectionEventListener listener)
   {
      if (listener == null)
         throw new IllegalArgumentException("Listener is null");

      listeners.remove(listener);
   }


   public PrintWriter getLogWriter() throws ResourceException
   {
      return logWriter;
   }


   public void setLogWriter(PrintWriter out) throws ResourceException
   {
      this.logWriter = out;
   }


   public LocalTransaction getLocalTransaction() throws ResourceException
   {
      throw new NotSupportedException("LocalTransaction not supported");
   }


   public XAResource getXAResource() throws ResourceException
   {
      throw new NotSupportedException("GetXAResource not supported");
   }


   public ManagedConnectionMetaData getMetaData() throws ResourceException
   {
      return new HelloWorldManagedConnectionMetaData();
   }


   String helloWorld(String name)
   {
      return "Hello World, " + name + " !";
   }


   void closeHandle(HelloWorldConnection handle)
   {
      ConnectionEvent event = new ConnectionEvent(this, ConnectionEvent.CONNECTION_CLOSED);
      event.setConnectionHandle(handle);

      for (ConnectionEventListener cel : listeners)
      {
         cel.connectionClosed(event);
      }
   }
}

Finally, the ConnectionImpl class which sends data to the server:

package com.sample.adapter;

import java.util.logging.Logger;


public class HelloWorldConnectionImpl implements HelloWorldConnection
{
   /** The logger */
   private static Logger log = Logger.getLogger("HelloWorldConnectionImpl");

   /** ManagedConnection */
   private HelloWorldManagedConnection mc;

   /** ManagedConnectionFactory */
   private HelloWorldManagedConnectionFactory mcf;


   public HelloWorldConnectionImpl(HelloWorldManagedConnection mc,
                                   HelloWorldManagedConnectionFactory mcf)
   {
      this.mc = mc;
      this.mcf = mcf;
   }


   public String helloWorld()
   {
      return helloWorld(((HelloWorldResourceAdapter)mcf.getResourceAdapter()).getName());
   }


   public String helloWorld(String name)
   {
      return mc.helloWorld(name);
   }

   public void close()
   {
      mc.closeHandle(this);
   }
}

Follow us on Twitter