Deploy a basic JCA ResourceAdapter

We can configure and deploy a basic Java EE Connector Architecture (JCA) ConnectionFactory and Resource Adapter.

We can install a resource adapter and configure instances of the resources it provides. This task uses an example resource adapter called ExampleRA.rar, which provides 3 types of resources: a connection factory and two types of administered objects.

  1. Edit...

      /path/to/liberty\wlp\usr\servers\server_name/server.xml

    ...and enable the JCA feature...

      <server>
        <featureManager>
          <feature>jca-1.6</feature>
          <feature>servlet-3.0</feature>
        </featureManager>
      </server>

    Stabilized feature: The jca-1.6 feature is stabilized. We can continue to use the jca-1.6 feature. However, consider using a later JCA feature.

  2. Place the resource adapter RAR file (ExampleRA.rar) into the dropins folder of the server.

    If the server is running, we will see the following message in the console log indicating that the resource adapter has been installed:

      [AUDIT ] J2CA7001I: Resource adapter ExampleRA installed in 1.306 seconds.

  3. Inspect the deployment descriptor, annotations, and other documentation from the resource adapter to identify which types of resources the adapter provides and the configuration properties that each adapter accepts.

    The example resource adapter, ExampleRA.rar, has this information in the deployment descriptor...

      /path/to/ExampleRA/META-INF/ra.xml

    The deployment descriptor identifies 3 types of resources we can configure.

      <connection-definition>
       <managedconnectionfactory-class>com.ibm.example.jca.adapter.ManagedConnectionFactoryImpl</managedconnectionfactory-class>
       <config-property>
        <config-property-name>tableName</config-property-name>
        <config-property-type>java.lang.String</config-property-type>
       </config-property>
       <connectionfactory-interface>javax.resource.cci.ConnectionFactory</connectionfactory-interface>
       ...
      </connection-definition>
      
          <adminobject>
            <adminobject-interface>javax.resource.cci.ConnectionSpec</adminobject-interface>
            <adminobject-class>com.ibm.example.jca.adapter.ConnectionSpecImpl</adminobject-class>
            <config-property>
              <config-property-name>readOnly</config-property-name> 
              <config-property-type>java.lang.Boolean</config-property-type>
              <config-property-value>false</config-property-value> 
            </config-property>
          </adminobject>
      
          <adminobject>
            <adminobject-interface>javax.resource.cci.InteractionSpec</adminobject-interface>
            <adminobject-class>com.ibm.example.jca.adapter.InteractionSpecImpl</adminobject-class>
            <config-property>
              <description>Function name. Supported values are: ADD, FIND, REMOVE</description>
              <config-property-name>functionName</config-property-name> 
              <config-property-type>java.lang.String</config-property-type> 
            </config-property>
          </adminobject>
      

  4. In the server.xml file, configure instances of the available resource types.

      <server>
        <featureManager>
          <feature>jca-1.6</feature>
          <feature>servlet-3.0</feature>
        </featureManager>
      
        <connectionFactory jndiName="eis/conFactory">
          <properties.ExampleRA tableName="TABLE1"/>
        </connectionFactory>
      
        <adminObject jndiName="eis/conSpec">
          <properties.ExampleRA.ConnectionSpec/>
        </adminObject>
      
        <adminObject jndiName="eis/iSpec_ADD">
          <properties.ExampleRA.InteractionSpec functionName="ADD"/>
        </adminObject>
      
        <adminObject jndiName="eis/iSpec_FIND">
          <properties.ExampleRA.InteractionSpec functionName="FIND"/>
        </adminObject>
      
      </server>
      

  5. Use resource injection to access the resources in your servlet; for example:

          @Resource(lookup = "eis/conFactory")
          private ConnectionFactory conFactory;
      
          @Resource(lookup = "eis/conSpec")
          private ConnectionSpec conSpec;
      
          @Resource(lookup = "eis/iSpec_ADD")
          private InteractionSpec iSpec_ADD;
      
          @Resource(lookup = "eis/iSpec_FIND")
          private InteractionSpec iSpec_FIND;
      
          ...
      
              MappedRecord input = conFactory.getRecordFactory().createMappedRecord("input");
              input.put("city", "Rochester");
              input.put("state", "Minnesota");
              input.put("population", 106769);
      
              Connection con = conFactory.getConnection(conSpec);
              try {     Interaction interaction = con.createInteraction();
          interaction.execute(iSpec_ADD, input);
          interaction.close();
              } finally {     con.close();
              }

    Note: We must enable the JNDI feature in the server.xml file to look up the resources from the namespace rather than using injection.