Program guide > Programming with system APIs and plug-ins
Plug-ins for communicating with persistent stores
With an eXtreme Scale Loader plug-in, an ObjectGrid map can behave as a memory cache for data that is typically kept in a persistent store on either the same system or some other system. Typically, a database or file system is used as the persistent store. A remote Java™ virtual machine (JVM) can also be used as the source of data, allowing hub-based caches to be built using ObjectGrid. A loader has the logic for reading and writing data to and from a persistent store.
Loaders are backing map plug-ins that are invoked when changes are made to the backing map or when the backing map is unable to satisfy a data request (a cache miss).
See Cache concepts for more information.
Figure 1. Loader
primary shard. The loader communicates with the database and the BackingMap." />
WebSphere eXtreme Scale includes two built-in loaders to integrate with relational database back ends. The Java Persistence API (JPA) loaders use the Object-Relational Mapping (ORM) capabilities of both the OpenJPA and Hibernate implementations of the JPA specification.
Use a loader
To add a loader into the BackingMap configuration, you can use programmatic configuration or XML configuration. A loader has the following relationship with a backing map:
- A backing map can have only one loader.
- A client backing map (near cache) cannot have a loader.
- A loader definition can be applied to multiple backing maps, but each backing map has its own loader instance.
Programmatically plug in a Loader
The following snippet of code demonstrates how to plug an application-provided Loader into the backing map for map1 using the ObjectGrid API:
import com.ibm.websphere.objectgrid.ObjectGridManagerFactory; import com.ibm.websphere.objectgrid.ObjectGridManager; import com.ibm.websphere.objectgrid.ObjectGrid; import com.ibm.websphere.objectgrid.BackingMap; ObjectGridManager ogManager = ObjectGridManagerFactory.getObjectGridManager(); ObjectGrid og = ogManager.createObjectGrid( "grid" ); BackingMap bm = og.defineMap( "map1" ); MyLoader loader = new MyLoader(); loader.setDataBaseName("testdb"); loader.setIsolationLevel("read committed"); bm.setLoader( loader );
This snippet assumes that the MyLoader class is the application-provided class that implements the com.ibm.websphere.objectgrid.plugins.Loader interface. Because the association of a Loader with a backing map cannot be changed after ObjectGrid is initialized, the code must be run before invoking the initialize method of the ObjectGrid interface that is being called. An IllegalStateException exception occurs on a setLoader method call if it is called after initialization has occurred.
The application-provided Loader can have set properties. In the example, the MyLoader loader is used to read and write data from a table in a relational database. The loader must specify the name of the database and the SQL isolation level. The MyLoader loader has the setDataBaseName and setIsolationLevel methods that allow the application to set these two Loader properties.
XML configuration approach to plug in a Loader
An application-provided Loader can also be plugged in by using an XML file. The following example demonstrates how to plug the MyLoader loader into the map1 backing map with the same database name and isolation level Loader properties:
<?xml version="1.0" encoding="UTF-8" ?> <objectGridConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ibm.com/ws/objectgrid/config ../objectGrid.xsd" xmlns="http://ibm.com/ws/objectgrid/config"> <objectGrids> <objectGrid name="grid"> <backingMap name="map1" pluginCollectionRef="map1" lockStrategy="OPTIMISTIC" /> </objectGrid> </objectGrids> <backingMapPluginCollections> <backingMapPluginCollection id="map1"> <bean id="Loader" className="com.myapplication.MyLoader"> <property name="dataBaseName" type="java.lang.String" value="testdb" description="database name" /> <property name="isolationLevel" type="java.lang.String" value="read committed" description="iso level" /> </bean> </backingMapPluginCollection> </backingMapPluginCollections> </objectGridConfig>
- Write a loader
You can write the own loader plug-in implementation in your applications, which must follow the common WebSphere eXtreme Scale plug-in conventions.
- JPA loader programming considerations
A Java Persistence API (JPA) Loader is a loader plug-in implementation that uses JPA to interact with the database. Use the following considerations when you develop an application that uses a JPA loader.
- JPAEntityLoader plug-in
The JPAEntityLoader plug-in is a built-in Loader implementation that uses Java Persistence API (JPA) to communicate with the database when you are using the EntityManager API. When you are using the ObjectMap API, use the JPALoader loader.
- Use a loader with entity maps and tuples
The entity manager converts all entity objects into tuple objects before they are stored in an WebSphere eXtreme Scale map. Every entity has a key tuple and a value tuple. This key-value pair is stored in the associated eXtreme Scale map for the entity. When you are using an eXtreme Scale map with a loader, the loader must interact with the tuple objects.
- Write a loader with a replica preload controller
A Loader with a replica preload controller is a Loader that implements the ReplicaPreloadController interface in addition to the Loader interface.
Parent topic:Program with system APIs and plug-ins
Introduction to plug-ins
Plug-ins for evicting cache objects
Plug-ins for transforming cached objects
Plug-ins for versioning and comparing cache objects
Plug-ins for custom indexing of cache objects
Plug-ins for managing transaction life cycle events
Configure write-behind support
Plug-ins for providing event listeners