Here's how the cache monitoring module works. Table 2 shows the steps involved when a client requests the data for the very first time and for subsequent requests.
Table 2: The table shows the steps required for both a first (uncached) request and for subsequent (cached) requests.
| #
|
Request
|
Description
|
Cache Monitoring Details
|
| 1 |
Initial request |
Client requests the data for the first time. |
Intercept the client request using a pointcut. Check if data is in the cache using around advice. When no match is found (because this is the first request), call the proceed() method of the join point to get the data from backend database. Store the result set in the object cache, update CacheMonitorMBean with miss count and other cache statistics, and finally, return data to the client. |
| 2 |
Subsequent requests |
Client requests the same data again. |
Intercept the data request using the pointcut. Check if data is already present in the cache using the around advice. When you find a match update the CacheMonitorMBean with the hit count and other cache statistics, and return the data to client. |
| 3 |
Data Expiry |
Cached data expires after a pre-defined timeout. |
This is pretty much same as step 1. First check to see if the data is in the cache. When no match is found call the proceed() method of the join point to get the data from the database. |
Table 3 shows the pointcuts and advice you would write in an AOP Cache Monitoring framework.
Table 3: Here are the pointcuts and advice you would write in an AOP Cache Monitoring framework.
| Aspect Type
|
Aspect Name
|
PointCut
|
Advice
|
| Caching |
ObjectCache.aj |
contextInitialized |
After |
| Caching |
JBossCache.aj |
contextDestroyed |
Before |
| Caching |
JBossCache.aj |
getInterestRates |
Around |
| Monitoring |
CacheMonitor.aj |
getInterestRates |
Around |
You instantiate the object cache when initializing the Web application context , using a servlet context listener class called LoanAppContextListener. The code below shows the contextInitialized method in the listener class. The method doesn't do anything other than display a debug message, but you can use this method as a join point to write an after advice to load the cache.
//This method is invoked when the Web Application
//is ready to service requests
public void contextInitialized(
ServletContextEvent event) {
//Output a simple message to the server's console
sLog.debug("LoanApp context is created.");
}
Here's the pointcut and after advice to call the initObjectCache method.
@Pointcut("execution(void
LoanAppContextListener.contextInitialized(
ServletContextEvent)) && args(event)")
void contextInitialized(
ServletContextEvent event) {};
@After("contextInitialized(event)")
public void afterContextInitialized(
ServletContextEvent event) {
System.out.println("afterContextInitialized");
initObjectCache();
}
Similarly, you want to remove the cache object when the servlet context is destroyed. The code snippets below show the contextDestroyed method in the LoanAppContextListener class and the pointcut and before advice to cleanup the cache respectively.
//This method is invoked when the Web Application
//has been removed and is no longer able to accept
//requests
public void contextDestroyed(
ServletContextEvent event) {
//Output a simple message to the server's console
sLog.debug("LoanApp context is removed.");
}
@Pointcut("execution(void
LoanAppContextListener.contextDestroyed(
ServletContextEvent)) && args(event)")
void contextDestroyed(
ServletContextEvent event) {};
@Before("contextDestroyed(event)")
public void beforeContextDestroyed(
ServletContextEvent event) {
System.out.println("beforeContextDestroyed");
cleanupObjectCache();
}