JBoss application server tutorials

  • Full Screen
  • Wide Screen
  • Narrow Screen
  • Increase font size
  • Default font size
  • Decrease font size

Create your first JCA connector tutorial

 

In this tutorial we will show how you can create your own JCA Connector project to connect to a remote TCP Server and deploy it on JBoss AS 7.1.1.

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.

The following picture shows the list of classes which we will create in our project:

jboss jca ironjacamar tutorial

For this purpose create 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 JBoss ):

jboss jca eclipse ironjacamar tutorial

Choose a meaningful name for your Connector project and click Finish

jboss ironjacamar connector jca tutorial

We will not expose all the project classes but just the core parts of it (You can download all the project classes at the bottom of this article).

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

public interface RAConnection
{
   public String write();
   public String write(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 to a Socket stream and returns the data from the stream.

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

    /** MCF */
    private RAManagedConnectionFactory mcf;

    /** Log writer */
    private PrintWriter logWriter;

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

    /** Connection */
    private Object connection;

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

    /**
     * Creates a new connection handle for the underlying physical connection
     * represented by the ManagedConnection instance.
     *
     * @param subject Security context as JAAS subject
     * @param cxRequestInfo ConnectionRequestInfo instance
     * @return generic Object instance representing the connection handle.
     * @throws ResourceException generic exception if operation fails
     */
    public Object getConnection(Subject subject,
            ConnectionRequestInfo cxRequestInfo)
                    throws ResourceException
                    {
        connection = new RAConnectionImpl(this, mcf);
        return connection;
                    }

    /**
     * Used by the container to change the association of an
     * application-level connection handle with a ManagedConneciton instance.
     *
     * @param connection Application-level connection handle
     * @throws ResourceException generic exception if operation fails
     */
    public void associateConnection(Object connection) throws ResourceException
    {
        this.connection = connection;
    }
    public void cleanup() throws ResourceException
    {
    }

    /**
     * Destroys the physical connection to the underlying resource manager.
     *
     * @throws ResourceException generic exception if operation fails
     */
    public void destroy() throws ResourceException
    {
        this.connection = null;
    }

    /**
     * Adds a connection event listener to the ManagedConnection instance.
     *
     * @param listener A new ConnectionEventListener to be registered
     */
    public void addConnectionEventListener(ConnectionEventListener listener)
    {
        if (listener == null)
            throw new IllegalArgumentException("Listener is null");
        listeners.add(listener);
    }

    /**
     * Removes an already registered connection event listener
     * from the ManagedConnection instance.
     *
     * @param listener Already registered connection event listener to be removed
     */
    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;
    }

    /**
     * Returns an <code>javax.resource.spi.LocalTransaction</code> instance.
     *
     * @return LocalTransaction instance
     * @throws ResourceException generic exception if operation fails
     */
    public LocalTransaction getLocalTransaction() throws ResourceException
    {
        throw new NotSupportedException("LocalTransaction not supported");
    }

    /**
     * Returns an <code>javax.transaction.xa.XAresource</code> instance.
     * @return XAResource instance
     * @throws ResourceException generic exception if operation fails
     */
    public XAResource getXAResource() throws ResourceException
    {
        throw new NotSupportedException("GetXAResource not supported");
    }

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

    /**
     * Calls write
     * @param name String text
     * @return returnVal
     */
    String write(String text)
    {
        PrintWriter out = null;
        BufferedReader in = null;
        String returnVal = null;
        RAConnectionImpl conn = (RAConnectionImpl)connection;
        Socket socket = conn.getSocket();
        try {
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out.println(text);
            returnVal = in.readLine();
            out.close();
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return returnVal;
    }
    void closeHandle(RAConnection handle)
    {
        ConnectionEvent event = new ConnectionEvent(this, ConnectionEvent.CONNECTION_CLOSED);
        event.setConnectionHandle(handle);

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

Finally, the RAConnectionImpl holds the actual Socket connection to the remote server, which sends data to the server and reads back the output from the remote socket.

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

    /** ManagedConnection */
    private RAManagedConnection mc;

    /** ManagedConnectionFactory */
    private RAManagedConnectionFactory mcf;
    Socket  socket = null;
    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public RAConnectionImpl(RAManagedConnection mc,
            RAManagedConnectionFactory mcf)
    {
        this.mc = mc;
        this.mcf = mcf;
        try {
            socket = new Socket("127.0.0.1", 6789);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public String write()
    {
        return write(((SocketResourceAdapter)mcf.getResourceAdapter()).getName());
    }

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

    /**
     * Close
     */
    public void close()
    {
        mc.closeHandle(this);
        try {
            this.socket.close();
            log.info("Socket closed!");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}



Francesco Google+
Top Programming Sites
You are here Projects IronJacamar (JCA) Create your first JCA connector tutorial