You can use the ObjectGridManager interface to control the life cycle of an ObjectGrid instance using either a startup bean or a servlet.
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(); } } }
ObjectGridManager objectGridManager = ObjectGridManagerFactory.getObjectGridManager(); ObjectGrid bookstoreGrid = objectGridManager.getObjectGrid("bookstore"); ObjectGrid videostoreGrid = objectGridManager.getObjectGrid("videostore");
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 // ... } public void destroy() { super.destroy(); try { // remove and destroy the cached bookstore ObjectGrid objectGridManager.removeObjectGrid("bookstore", true); } catch (ObjectGridException e) { e.printStackTrace(); } } }