Java Proxy Pattern – Caching proxy

Proxy pattern applied to enable caching – a very simple Web Application

Recently I switched to work for another customer (Bank\Trading) and one of the first activities, in order to offer a more structured and well-designed product to users, was to reduce the overhead and the Database accesses.Considering that I am working on a web portal with 2 millions of contacts per month and hundreds of user accounts, designed and developed more than 10 years ago, it is very difficult to adopt any Java Framework and, in general, to implement whatever big modification.I studied several solutions to optimize the system’s performances.One of the pattern that comes to help in my work is the Proxy Pattern.I’m going to show you some aspects of this pattern, related to caching objects and data.This article is divided into two parts, one for each solution that I found to this issue.We’ll go into a very simple web application: NO dependency injection this time, NO Spring, NO Frameworks, only very simple code.We’ll create a simple web app that on home page will print the timestamp of last created instance of cached object.Consider that this object may be a complex data structure from a database, or a big image or something very painful for server.For our purpose, things are simplified.Well, we can summarize:1. user requires an object2. system check: an instance of this object already is created?3. If no, it create a new instance, otherwise it give back the already made instance.4. The user, transparently, receives the object.Let’s go!Configure Eclipse.Just create a New Dynamic Web Project, I called proxycacheapp.I used this project for both solutions that I’m going to implement.UML Class Diagram of a classical Proxy pattern from original GoF book, is:



And, briefly: the JSP contains the code for execute the request,that is wrapped by Servlet Filter (declared in web.xml as I hope you already know),Servlet Filter calls our ProxyService to achieve what user wants.ProxyService is only a surrogate, if the object required is ready and already created, it give back to user the cached instance, otherwise Proxy calls concrete RealService and require to create a new one.Object (either brand new or cached) is sent to output of ServletFilter, that incapsulate it into HttpRequest and, later Jsp prints it on Web page.Simple.This is a general scenario, but pragmatic implementation, can be made using two different solutions.Solution 1Plain, classical, proxy pattern applied.This solution uses only diagrams and the previous explanation, no more complication.Domain ObjectIt is very useless here, but I want to show you that it is possible to use any custom Object in this approach.Let’s create a new object, a POJO, with an ID and a timestamp..later we’ll print timestamp on JSP.Create new Java Class on Eclipse and write th wolling code (I use it.nickg.utils package):


package it.nickg.utils;

public class Timestamp {
 private long id;
 private String timestamp;

public long getId() {
 return id;
 }

public void setId(long id) {
 this.id = id;
 }

public String getTimestamp() {
 return timestamp;
 }

public void setTimestamp(String timestamp) {
 this.timestamp = timestamp;
 }
}

This is our Domain object.Now let’s implement the Proxy Pattern, first create a new interface (package is it.nickg.services):

package it.nickg.services;

import it.nickg.utils.Timestamp;

public interface TimestampService {

/**
 * retrieve an istance of Timestamp object
 *
 * @return Timestamp object
 */
 public Timestamp getTimestamp();

}

Then let’s write the Real implementationof Service, that creates instance of our Domain Object, it implements our previously created interface:

package it.nickg.services;

import it.nickg.utils.Timestamp;

import java.util.Date;

public class TimestampServiceReal implements TimestampService {

@Override
 public Timestamp getTimestamp() {

Date time = new Date();
 Timestamp timestamp = new Timestamp();

timestamp.setId(time.getTime());
 timestamp.setTimestamp(time.toString());

return timestamp;
 }

}

Not best solution for getting a timestamp,I know, but it is only for a learning purpose. Look at the concept.

package it.nickg.services;

import it.nickg.utils.Timestamp;

public class TimestampServiceProxy implements TimestampService {

private TimestampServiceReal timestampServiceReal;
 public Timestamp timestamp;

@Override
 public Timestamp getTimestamp() {

// Proxy checks if a real instance of service (and of timestamp object)
 // already is created
 if (timestampServiceReal == null) {
 timestampServiceReal = new TimestampServiceReal();
 timestamp = timestampServiceReal.getTimestamp();
 }

// return required object
 return timestamp;
 }
}

Now, It’s time to code our front-end!
Most of the work is done through a ServletFilter that wraps request, calls our services and sets a parameter inHttpResponse, with results.

Servlet Filter must be declared inweb.xml:
(complete web.xml code)


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <filter>
 <filter-name>TimestampFilter</filter-name>
 <filter-class>it.nickg.servlets.TimestampFilter</filter-class>
 </filter>

<filter-mapping>
 <filter-name>TimestampFilter</filter-name>
 <url-pattern>/index.jsp</url-pattern>
 </filter-mapping>

<welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

Here Servlet Filter code:


package it.nickg.servlets;

import it.nickg.services.TimestampService;
import it.nickg.services.TimestampServiceProxy;
import it.nickg.utils.Timestamp;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class TimestampFilter implements Filter {
 TimestampService tsService = new TimestampServiceProxy();

public TimestampFilter() {
 // TODO Auto-generated constructor stub
 }

@Override
 public void destroy() {
 // TODO Auto-generated method stub

}

@Override
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
 ServletException {

Timestamp timestamp = null;

// retrieve a timestamp from service
 timestamp = tsService.getTimestamp();
 request.setAttribute("timestamp", timestamp);

// forward output
 chain.doFilter(request, response);
 }

@Override
 public void init(FilterConfig filterConfig) throws ServletException {
 // TODO Auto-generated method stub

}

}

Last step is about editing index.jsp, andlet display the creation’s timestamp of Domain Object:


<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@page import="it.nickg.utils.Timestamp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>">

 <title>Proxy Pattern Example</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 </head>

 <body>
 <h3>Domain object is created:</h3>

<%
 Timestamp timestamp = (Timestamp)request.getAttribute("timestamp");
 out.print(timestamp.getTimestamp());
 %>
 </body>
</html>

I HATE to use scriplet and Java code inJSPs, use jstl, struts taglib, any other technology. Especially in productioncode.

That’s all, now run our server insideEclipse (I use tomcat)
Right click on project -> Run as  -> Server Application.

Now on welcome page, it displays firstcreation timestamp of the object and if you refresh the page, the timestampdoesn’t change:
It is cached!!

Simple, isn’t?

Well, It’s time for more complicatedissues.

Solution 2

A more experienced reader can found myprevious solution a bit like “reinventing the wheel” and it’s in part true,because java offers a Proxy class:

java.lang.reflect.Proxy

Using Java reflection it is possible to implement a more elegant representation of Proxy Pattern.
For a cleaner implementation, we define a TimestampServiceFactory:


package it.nickg.services;

import java.lang.reflect.Proxy;

public class TimestampServiceFactory {
 public TimestampService createService() {
 return (TimestampService) Proxy.newProxyInstance(
 TimestampService.class.getClassLoader(),
 new Class[] { TimestampService.class },
 new TimestampReflectProxy(new TimestampServiceReal()));
 }
}

Here we use Proxy.newProxyInstance() tocall a proxy. It returns an instance of a proxy class for the specifiedinterfaces that dispatches method invocations to the specified invocationhandler(TimestampReflectProxy).
And an interface CacheIFace:


package it.nickg.services;

public interface CacheIFace {

/**
 * Retrieve object from cache
 *
 * @param key
 * @return cached object
 */
 public Object getCache(Object key);

/**
 * Put a new object in cache
 *
 * @param key
 * @param value
 */
 public void putCache(Object key, Object value);
}

and an implementation class Cache:


package it.nickg.services;

import java.util.HashMap;
import java.util.Map;

public class Cache implements CacheIFace {
 private Map<Object, Object> values;

public Cache() {
 this.values = new HashMap<Object, Object>(8);
 }

public Object getCache(Object key) {
 return values.get(key);
 }

public void putCache(Object key, Object value) {
 values.put(key, value);
 }
}

These comes to let us perform cachingfeature through an HashMap:
HashMap IS the cache.
Create a new Java Class named TimestampReflectProxy that implements InvocationHandler:


package it.nickg.services;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;

public class TimestampReflectProxy implements InvocationHandler {

private final Object obj;
 private CacheIFace caches;
 private static final Object NullKey = new Object();

public TimestampReflectProxy(Object toProxy) {
 this.obj = toProxy;
 this.caches = new Cache();
 }

@Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

CacheIFace cache = (CacheIFace) caches.getCache(method);

if (cache == null) {
 caches.putCache(method, cache = new Cache());
 }

Object argsList = args != null ? Arrays.asList(args) : NullKey;
 Object cacheValue = cache.getCache(argsList);

if (cacheValue == null) {
 cache.putCache(argsList, cacheValue = method.invoke(obj, args));
 }
 return cacheValue;
 }
}

And, finally, here is the ServletFilter modified:


package it.nickg.servlets;

import it.nickg.services.TimestampService;
import it.nickg.services.TimestampServiceFactory;
import it.nickg.utils.Timestamp;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class TimestampFilter implements Filter {
 // TimestampService tsService = new TimestampServiceProxy();
 TimestampServiceFactory tsFactory = new TimestampServiceFactory();
 TimestampService tsService = tsFactory.createService();

public TimestampFilter() {
 // TODO Auto-generated constructor stub
 }

@Override
 public void destroy() {
 // TODO Auto-generated method stub

}

@Override
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
 ServletException {

Timestamp timestamp = null;

// retrieve a timestamp from service
 timestamp = tsService.getTimestamp();
 request.setAttribute("timestamp", timestamp);

// forward output
 chain.doFilter(request, response);
 }

@Override
 public void init(FilterConfig filterConfig) throws ServletException {
 // TODO Auto-generated method stub

}
}

I try to explain this magic trick.
Our TimestampReflectProxy class has two private attributes, obj and caches.
When user requires the page, theTimestampFilter acts as wrapper, it catches the request and callsTimestampServiceFactory and TimestampService that requires to create theservice.

First time, it passes TimestampServiceRealas Object to  TimestampReflectProxy ‘sconstructor, so:
– obj is an instance ofTimestampServiceReal;
– a new instance of Cache is created;

Then the filter calls getTimestamp()method and this call is caught with reflection:
– it enters in invoke() method,
– it checks if an occurrence of method isalready present in caches,
– there isn’t the required occurrence andso cache is null,
– it puts a new entry in cache;
– then retrieve the args list and checksif there is an entry in cache with given key;
– if not, it create a new one invokingmethod;

From this time, every time the userrefreshs the web page, the application using reflection, finds existing entriesin cache, skips checks, and gives back to him.

Here the results, if you try to refreshthe page, display doesn’t change:

While this second solution seems veryinteresting, advanced, elegant and cool..there are several drawbacks:
(cut and paste from Sun)
Performance Overhead
Because reflection involves types that are dynamically resolved, certainJava virtual machine optimizations can not be performed. Consequently,reflective operations have slower performance than their non-reflectivecounterparts, and should be avoided in sections of code which are calledfrequently in performance-sensitive applications.
Security Restrictions
Reflection requires a runtime permission which may not be present whenrunning under a security manager. This is in an important consideration forcode which has to run in a restricted security context, such as in an Applet.
Exposure of Internals
Since reflection allows code to perform operations that would be illegalin non-reflective code, such as accessing private fields and methods, the useof reflection can result in unexpected side-effects, which may render codedysfunctional and may destroy portability. Reflective code breaks abstractionsand therefore may change behavior with upgrades of the platform.
Some interesting discussions are linkedabove between references.
Enough to avoid it in my particular usecase.
Last thing I want to show.
Perhaps more careful of you have observedthat when an instance of an object is created, it can live forever!
(Well, I know, it’s impossible, but “undefined”and “forever” are both unacceptable in my opinion).
Best way to give the capability ofperiodical refresh of object, is implementing a TimeToLive Strategy. (Otherpossible solutions are LRU and LFU).
I’ll not describe here now, but give yousome input:
Into our Cache class, it is necessary tocode a thread that periodically cleans the cache and you can use whateverpolicy you want.
That’s all folks!
Please, feel free to add comment andcritics to my article, they are very useful.
References:
http://java.dzone.com/articles/design-patterns-proxy
http://java.sys-con.com/node/171489
About Reflection:
http://java.sun.com/developer/technicalArticles/ALT/Reflection/
About reflection performances:
http://stackoverflow.com/questions/435553/java-reflection-performance
http://www.jguru.com/faq/view.jsp?EID=246569

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s