The JBossCX framework provides the application server architecture extension required for the use of JCA resource adaptors. This is primarily a connection-pooling and management extension, and it includes a number of MBeans for loading resource adaptors into the JBoss server. Figure 7.3 expands the generic view given by Figure 7.2 to illustrate how the JBoss JCA layer implements the application server-specific extension, along with an example of a file system resource adaptor that is discussed later in this chapter. Figure 7.3. The JBoss JCA implementation components.Three coupled MBeans make up a RAR deployment: org.jboss.resource.RARDeployment, org.jboss.resource.connectionmanager.RARDeployment, and org.jboss.resource.connectionmanager.BaseConnectionManager2. org.jboss.resource.RARDeployment is simply an encapsulation of the metadata of a RAR META-INF/ra.xml descriptor. It exposes this information as a DynamicMBean simply to make it available to the org.jboss.resource.connectionmanager.RARDeployment MBean. The RARDeployer service handles the deployment of archive files that contain resource adaptors (RARs). It creates the org.jboss.resource.RARDeployment MBean when a RAR file is deployed. Deploying the RAR file is the first step in making the resource adaptor available to application components. For each deployed RAR, one or more connection factories must be configured and bound into JNDI. You perform this task by using a JBoss service descriptor that sets up an org.jboss.resource.connectionmanager.BaseConnectionManager2 MBean implementation with an org.jboss.resource.connectionmgr.RARDeployment. The BaseConnectionManager2 MBeanThe org.jboss.resource.connectionmanager.BaseConnectionManager2 MBean is a base class for the various types of connection managers required by the JCA specification. Subclasses include NoTxConnectionManager, LocalTx-ConnectionManager, and XATxConnectionManager. These correspond to resource adaptors that support no transactions, local transactions, and XA transactions, respectively. You choose which subclass to use based on the type of transaction semantics you want, provided that the JCA resource adaptor supports the corresponding transaction capability. The BaseConnectionManager2 MBean supports the following common attributes:
The RARDeployment MBeanThe org.jboss.resource.connectionmanager.RARDeployment MBean manages configuration and instantiation ManagedConnectionFactory instances. It does this by using the resource adaptor metadata settings from the RAR META-INF/ra.xml descriptor, along with the RARDeployment attributes. These are the configurable attributes:
The JBossManagedConnectionPool MBeanThe org.jboss.resource.connectionmanager.JBossManagedConnectionPool MBean is a connection-pooling MBean. It is typically used as the embedded MBean value of the BaseConnectionManager2 ManagedConnectionPool attribute. When you set up a connection manager MBean, you typically embed the pool configuration in the connection manager descriptor. The following are the configurable attributes of JBossManagedConnectionPool:
The CachedConnectionManager MBeanThe org.jboss.resource.connectionmanager.CachedConnectionManager MBean manages associations between meta-aware objects (those accessed through interceptor chains) and connection handles, as well as between user transactions and connection handles. Normally there should be only one such MBean, and this is configured in the core jboss-service.xml descriptor. It is used by CachedConnectionInterceptor, the JTA UserTransaction implementation, and all BaseConnectionManager2 instances. These are the configurable attributes of the CachedConnectionManager MBean:
A Sample Skeleton of a JCA Resource AdaptorTo conclude our discussion of the JBoss JCA framework, let's create and deploy a single non-transacted resource adaptor that simply provides a skeleton implementation that stubs out the required interfaces and logs all method calls. This section does not discuss the details of the requirements of a resource adaptor provider because they are discussed in detail in the JCA specification. The purposes of the adaptor are to demonstrate the steps required to create and deploy a RAR in JBoss and to show how JBoss interacts with the adaptor. The adaptor described in this section could be used as the starting point for a non-transacted file system adaptor. You can find the source for the example adaptor in the src/main/org/jboss/chap7/ex1 directory of the book examples. Figure 7.4 shows a class diagram of the mapping from the required javax.resource.spi interfaces to the resource adaptor implementation. Figure 7.4. The file system RAR class diagram.In this section you will build the adaptor, deploy it to the JBoss server, and then run an example of the client against an EJB that uses the resource adaptor to demonstrate the basic steps in a complete context. You'll then take a look at the JBoss server log to see how the JBoss JCA framework interacts with the resource adaptor to help you better understand the components in the JCA system-level contract. To build the example and deploy the RAR to the JBoss server deploy/lib directory, you need to execute the following Ant command in the book examples directory: [examples]$ ant -Dchap=chap7 build-chap The deployed files include a chap7-ex1.sar file and a notxfs-service.xml service descriptor. Listing 7.1 shows an example of the resource adaptor deployment descriptor. Listing 7.1. A Non-transactional File System Resource Adaptor Deployment Descriptor<?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"> <display-name>File System Adapter</display-name> <vendor-name>JBoss</vendor-name> <eis-type>FileSystem</eis-type> <resourceadapter-version>1.0</resourceadapter-version> <license> <description>LGPL</description> <license-required>false</license-required> </license> <resourceadapter> <resourceadapter-class> org.jboss.resource.deployment.DummyResourceAdapter </resourceadapter-class> <outbound-resourceadapter> <connection-definition> <managedconnectionfactory-class> org.jboss.chap7.ex1.ra.FSManagedConnectionFactory </managedconnectionfactory-class> <config-property> <config-property-name>FileSystemRootDir</config-property-name> <config-property-type>java.lang.String</config-property-type> <config-property-value>/tmp/db/fs_store</config-property-value> </config-property> <config-property> <config-property-name>UserName</config-property-name> <config-property-type>java.lang.String</config-property-type> <config-property-value/> </config-property> <config-property> <config-property-name>Password</config-property-name> <config-property-type>java.lang.String</config-property-type> <config-property-value/> </config-property> <connectionfactory-interface> org.jboss.chap7.ex1.ra.DirContextFactory</connectionfactory-interface> <connectionfactory-impl-class> org.jboss.chap7.ex1.ra.DirContextFactoryImpl</connectionfactory-impl-class> <connection-interface> javax.naming.directory.DirContext </connection-interface> <connection-impl-class> org.jboss.chap7.ex1.ra.FSDirContext </connection-impl-class> </connection-definition> <transaction-support>NoTransaction</transaction-support> <authentication-mechanism> <authentication-mechanism-type>BasicPassword</authentication-mechanism-type> <credential-interface> javax.resource.spi.security.PasswordCredential </credential-interface> </authentication-mechanism> <reauthentication-support>true</reauthentication-support> </outbound-resourceadapter> <security-permission> <description> Read/Write access is required to the contents of the FileSystemRootDir </description> <security-permission-spec> permission java.io.FilePermission "/tmp/db/fs_store/*", "read,write"; </security-permission-spec> </security-permission> </resourceadapter> </connector> The key items in the resource adaptor deployment descriptor are highlighted in bold. These items define the classes of the resource adaptor, and the elements are as follows:
The RAR classes and deployment descriptor define only a resource adaptor. Before you can use the resource adaptor, it must be integrated into the JBoss application server, using a ds.xml descriptor file. An example of this file for the file system adaptor is shown in Listing 7.2. Listing 7.2. The notxfs-ds.xml Resource Adaptor MBeans Service Descriptor<!DOCTYPE connection-factories PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN" "http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd"> <!-- The non-transaction FileSystem resource adaptor service configuration --> <connection-factories> <no-tx-connection-factory> <jndi-name>NoTransFS</jndi-name> <rar-name>chap7-ex1.rar</rar-name> <connection-definition> org.jboss.chap7.ex1.ra.DirContextFactory </connection-definition> <config-property name="FileSystemRootDir" type="java.lang.String">/tmp/db/fs_store</config-property> </no-tx-connection-factory> </connection-factories> The main attributes are as follows:
To deploy the RAR and connection manager configuration to the JBoss server, you run the following: [examples]$ ant -Dchap=chap7 config The server console will display some logging output, indicating that the resource adaptor has been deployed. Now you need to test access of the resource adaptor by a J2EE component. To do this, you can create a trivial stateless session bean that has a single method called echo. Inside the echo method, the EJB accesses the resource adaptor connection factory, creates a connection, and then immediately closes the connection. The echo method code is shown in Listing 7.3. Listing 7.3. The Stateless Session Bean echo Method Code That Shows the Access of the Resource Adaptor Connection Factorypublic String echo(String arg) { log.info("echo, arg="+arg); try { InitialContext ctx = new InitialContext(); Object ref = ctx.lookup("java:comp/env/ra/DirContextFactory"); log.info("echo, ra/DirContextFactory=" + ref); DirContextFactory dcf = (DirContextFactory) ref; log.info("echo, found dcf=" + dcf); DirContext dc = dcf.getConnection(); log.info("echo, lookup dc=" + dc); dc.close(); } catch(NamingException e) { log.error("Failed during JNDI access", e); } return arg; } The EJB is not using the CCI interface to access the resource adaptor. Rather, it is using the resource adaptor-specific API based on the proprietary DirContextFactory interface that returns a JNDI DirContext object as the connection object. The EJB in the example is simply exercising the system contract layer by looking up the resource adaptor connection factory, creating a connection to the resource, and closing the connection. The EJB does not actually do anything with the connection, as that would only exercise the resource adaptor implementation because this is a non-transactional resource. You run the test client, which calls the EchoBean.echo method, by running Ant as follows from the examples directory: [examples]$ ant -Dchap=chap7 -Dex=1 run-example You'll see some output from the bean in the system console, but you can find much more detailed logging output in the server/default/log/server.log file. Don't worry if you see exceptions. They are just stack traces to highlight the call path into parts of the adaptor. To help understand the interaction between the adaptor and the JBoss JCA layer, the sequence diagram shown in Figure 7.5 summarizes the events that occur when the EchoBean accesses the resource adaptor connection factory from JNDI and creates a connection. Figure 7.5. A sequence diagram that illustrates how EchoBean accesses the resource adaptor connection factory.The starting point is the client's invocation of the EchoBean.echo method. For the sake of conciseness of the diagram, the client is shown directly invoking the EchoBean.echo method, when in reality the JBoss EJB container handles the invocation. There are three distinct interactions between the EchoBean and the resource adaptor: the lookup of the connection factory, the creation of a connection, and the close of the connection. The lookup of the resource adaptor connection factory is illustrated by the 1.1 sequences of events, which are as follows.:
This concludes the resource adaptor example. This investigation into the interaction between the JBossCX layer and a trivial resource adaptor should give you sufficient understanding of the steps required to configure any resource adaptor. The adaptor in this example can also serve as a starting point for the creation of your own custom resource adaptors if you need to integrate non-JDBC resources into the JBoss server environment. |