This article shows you a way to bootstrap GemFire from a Java Servlet container.
Follow the following steps to bootstrap GemFire from a Java Servlet container and execute region operations from your Servlet:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>MyServeltAppsOnJetty</display-name> <!-- specify your ServletContextListener class --> <listener> <listener-class>io.pivotal.gemfire.GemFireLoaderListener</listener-class> </listener> <!-- set your Servlet --> <servlet> <servlet-name>SessionServlet</servlet-name> <servlet-class>io.pivotal.gemfire.SessionServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SessionServlet</servlet-name> <url-pattern>/SessionServlet</url-pattern> </servlet-mapping> <!-- set context parameters for specifying paths for gemfire.properties and cache.xml (optional) --> <context-param> <param-name>gemfirePropsLocation</param-name> <param-value>/WEB-INF/gemfire.properties</param-value> </context-param> <context-param> <param-name>gemfireCacheConfigLocation</param-name> <param-value>/WEB-INF/cache.xml</param-value> </context-param> </web-app>
package io.pivotal.gemfire; import com.gemstone.gemfire.cache.Cache; import com.gemstone.gemfire.cache.CacheFactory; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class GemFireLoaderListener implements ServletContextListener { public static final String GEMFIRE_PROPERTIES = "gemfirePropsLocation"; public static final String GEMFIRE_CACHE_XML = "gemfireCacheConfigLocation"; public static final String REGION_ATTRIBUTE = "regionAttribute"; public void contextInitialized(ServletContextEvent event) { ServletContext context = event.getServletContext(); System.setProperty("gemfirePropertyFile", context.getRealPath(context.getInitParameter(GEMFIRE_PROPERTIES))); Cache cache = new CacheFactory() .set("name", "ServletCache") .set("cache-xml-file", context.getRealPath(context.getInitParameter(GEMFIRE_CACHE_XML))) .create(); context.setAttribute(REGION_ATTRIBUTE, cache.getRegion("ServletRegion")); } public void contextDestroyed(ServletContextEvent event) { ServletContext context = event.getServletContext(); // implement something if you need (e.g. closing your cache) } }
public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Region<String,Integer> region = (Region) getServletContext().getAttribute(GemFireLoaderListener.REGION_ATTRIBUTE); HttpSession session = request.getSession(true); Integer cnt = region.get(session.getId()); cnt++; region.put(session.getId(),cnt); :
Environment
Bootstrapping GemFire from an Application Server- This is the original article related to this topic. While slightly old in terms of API usage, it may still prove useful for more complex use cases.