com.googlecode.objectify.cache.MemcacheServiceRetryProxy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of objectify Show documentation
Show all versions of objectify Show documentation
*** THIS VERSION UPLOADED FOR USE WITH CEDAR-COMMON, TO AVOID DEPENDENCIES ON GOOGLE CODE-BASED MAVEN REPOSITORIES. *** The simplest convenient interface to the Google App Engine datastore
The newest version!
package com.googlecode.objectify.cache;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.appengine.api.memcache.ErrorHandler;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.StrictErrorHandler;
/**
* Dynamic proxy which wraps a MemcacheService and adds retries when an exception occurs.
* It logs and masks exceptions on complete failure.
*
* @author Jeff Schnitzer
*/
public class MemcacheServiceRetryProxy implements InvocationHandler
{
/** */
private static final Logger log = Logger.getLogger(MemcacheServiceRetryProxy.class.getName());
/** */
private static final int DEFAULT_RETRIES = 4;
/**
* We use this on the memcacheservice for writes so that we get real exceptions when something goes wrong.
*/
private static final ErrorHandler DOES_NOT_MASK_EXCEPTIONS = new StrictErrorHandler();
/**
* Create the proxy that does retries. Adds a strict error handler to the service.
*/
public static MemcacheService createProxy(MemcacheService raw)
{
return createProxy(raw, DEFAULT_RETRIES);
}
/**
* Create the proxy that does retries. Adds a strict error handler to the service.
*/
public static MemcacheService createProxy(MemcacheService raw, int retryCount)
{
raw.setErrorHandler(DOES_NOT_MASK_EXCEPTIONS);
return (MemcacheService)java.lang.reflect.Proxy.newProxyInstance(
raw.getClass().getClassLoader(),
raw.getClass().getInterfaces(),
new MemcacheServiceRetryProxy(raw, retryCount));
}
/** */
private MemcacheService raw;
/** */
private int retries;
/** */
public MemcacheServiceRetryProxy(MemcacheService raw, int retries)
{
this.raw = raw;
this.retries = retries;
}
/* (non-Javadoc)
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
@Override
public Object invoke(Object proxy, Method meth, Object[] args) throws Throwable
{
for (int i=0; i