Controlling the lifecycle of an ObjectGrid with the ObjectGridManager interface
You can use the ObjectGridManager interface to control the lifecycle of an ObjectGrid instance using either a startup bean or a servlet.
Managing lifecycle with a startup bean
A startup bean is used to control the lifecycle of an ObjectGrid instance. A startup bean loads
when an application starts. With a startup bean, code can run whenever an application starts or
stops as expected. To create a startup bean, use the home
com.ibm.websphere.startupservice.AppStartUpHome interface and use the remote
com.ibm.websphere.startupservice.AppStartUp interface. Implement the
start and stop methods on the bean. The
start method is invoked whenever the application starts up. The
stop method is invoked when the application shuts down. The
start method is used to create ObjectGrid instances. The stop
method is used to remove ObjectGrid instances. A code snippet that demonstrates this ObjectGrid
lifecycle management in a startup bean
follows:
public class MyStartupBean implements javax.ejb.SessionBean {
private ObjectGridManager objectGridManager;
/* The methods on the SessionBean interface have been
* left out of this example for the sake of brevity */
public boolean start(){
// Starting the startup bean
// This method is called when the application starts
objectGridManager = ObjectGridManagerFactory.getObjectGridManager();
try {
// create 2 ObjectGrids and cache these instances
ObjectGrid bookstoreGrid = objectGridManager.createObjectGrid("bookstore", true);
bookstoreGrid.defineMap("book");
ObjectGrid videostoreGrid = objectGridManager.createObjectGrid("videostore", true);
// within the JVM,
// these ObjectGrids can now be retrieved from the
//ObjectGridManager using the getObjectGrid(String) method
} catch (ObjectGridException e) {
e.printStackTrace();
return false;
}
return true;
}
public void stop(){
// Stopping the startup bean
// This method is called when the application is stopped
try {
// remove the cached ObjectGrids and destroy them
objectGridManager.removeObjectGrid("bookstore", true);
objectGridManager.removeObjectGrid("videostore", true);
} catch (ObjectGridException e) {
e.printStackTrace();
}
}
}
After the start method is called, the newly created ObjectGrid instances are
retrieved from the ObjectGridManager interface. For example, if a servlet is
included in the application, the servlet accesses the eXtreme Scale using the following code
snippet:
ObjectGridManager objectGridManager =
ObjectGridManagerFactory.getObjectGridManager();
ObjectGrid bookstoreGrid = objectGridManager.getObjectGrid("bookstore");
ObjectGrid videostoreGrid = objectGridManager.getObjectGrid("videostore");
Managing lifecycle with a servlet
To manage the lifecycle of an ObjectGrid in a servlet, you can use the init method to create an ObjectGrid instance and the destroy method to remove the ObjectGrid instance. If the ObjectGrid instance is cached, it is retrieved and manipulated in the servlet code. Sample code that demonstrates ObjectGrid creation, manipulation, and destruction within a servlet follows:public class MyObjectGridServlet extends HttpServlet implements Servlet {
private ObjectGridManager objectGridManager;
public MyObjectGridServlet() {
super();
}
public void init(ServletConfig arg0) throws ServletException {
super.init();
objectGridManager = ObjectGridManagerFactory.getObjectGridManager();
try {
// create and cache an ObjectGrid named bookstore
ObjectGrid bookstoreGrid =
objectGridManager.createObjectGrid("bookstore", true);
bookstoreGrid.defineMap("book");
} catch (ObjectGridException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
ObjectGrid bookstoreGrid = objectGridManager.getObjectGrid("bookstore");
Session session = bookstoreGrid.getSession();
ObjectMap bookMap = session.getMap("book");
// perform operations on the cached ObjectGrid
// ...
// Close the session (optional in Version 7.1.1 and later) for improved performance
session.close();
}
public void destroy() {
super.destroy();
try {
// remove and destroy the cached bookstore ObjectGrid
objectGridManager.removeObjectGrid("bookstore", true);
} catch (ObjectGridException e) {
e.printStackTrace();
}
}
}