Accessing data with JNDI in Liberty

Use Java™ Directory Naming Interface APIs to access a data grid that runs with the Liberty server.

Before you begin

To configure and use JNDI with the product, you must first complete the following actions:

About this task

You can use @Resource annotation or JNDI APIs in your application to access the data grid. To do so, you use the server.xml file to enable the client feature, specify binding information, and identify client domains that include the connection information for the bindings.

Procedure

  1. In the Liberty server.xml file, specify the Liberty client feature.
    The client feature contains most of the programming model for eXtreme Scale. Add the client feature when you have an application that is running in the Liberty that is going to use eXtreme Scale APIs.
    <featureManager> 
       <feature>jndi-1.0</feature>
       <feature>eXtremeScale.client-1.1</feature>
    </featureManager>
  2. Specify the xsBindings tag in your server.xml file so that the Liberty can identify which data grids can be accessed with JNDI.
    In the following example, the xsBindings tag includes two data grids, wxs/myGrid and wxs/myGrid2.
    <xsBindings>
          <xsGrid jndiName="wxs/myGrid" gridName="Grid" clientDomain="test"/>
          <xsGrid jndiName="wxs/myGrid2" gridName="Grid" />
        </xsBindings>
    
    In the previous example, myGrid2 uses the default domain. When you do not specify a domain, by default, you use the default domain, which is included in the example from the next step. For more information about the xsGrid tag, see Liberty profile xsWebGrid feature properties
  3. In your server.xml file, use client domains to specify the connection information for the bindings.
    <xsClientDomain default="dev">    
          <endpointConfig> dev ; localhost:2809 </endpointConfig>    
          <endpointConfig> test ; testhost1:4444,testhost2:4444 </endpointConfig>    
    </xsClientDomain>
  4. Access the data grid with JNDI. You can access the data grid using either the @Resource annotation or standard JNDI APIs.
    • In your servlet file, specify the @Resource annotation with an ObjectGrid variable declaration; for example:
      @Resource(name="wxs/myGrid")
      ObjectGrid og;
    • Alternatively, in your application, use JNDI APIs to retrieve the ObjectGrid object:
      InitialContext ic = new InitialContext();
      ObjectGrid og = (ObjectGrid) ic.lookup("wxs/myGrid");

Example

In the following server.xml file, the xsGrid tags contain the JNDI information for the corresponding data grid. In this example, the default client domain endpoint is dev. If you do not want to use the default client domain endpoint, then in the xsGrid tag, you must specify which client domain to use to connect to the data grid. In the following example, dev is the default client endpoint, which is used to connect to the data grid at wxs/myGrid2, and test is the client endpoint for wxs/myGrid2.
<server>
  <featureManager>     
      <feature>jndi-1.0</feature>
      <feature>eXtremeScale.client-1.1</feature>
  </featureManager>
  <xsBindings>
      <xsGrid jndiName="wxs/myGrid" gridName="Grid" clientDomain="test"/>
      <xsGrid jndiName="wxs/myGrid2" gridName="Grid" />
</xsBindings>
  <xsClientDomain default="dev">    
      <endpointConfig> dev ; localhost:2809 </endpointConfig>    
      <endpointConfig> test ; testhost1:4444,testhost2:4444 </endpointConfig>    
  </xsClientDomain> 
</server>