Created
February 9, 2016 23:04
-
-
Save ullgren/0b41bc7180ec1a0be8e1 to your computer and use it in GitHub Desktop.
CacheInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.redpill_linpro.mule.cache; | |
import java.io.NotSerializableException; | |
import java.io.Serializable; | |
import org.mule.DefaultMuleEvent; | |
import org.mule.DefaultMuleMessage; | |
import org.mule.api.MessagingException; | |
import org.mule.api.MuleEvent; | |
import org.mule.api.MuleEventKeyGenerator; | |
import org.mule.api.MuleException; | |
import org.mule.api.MuleMessage; | |
import org.mule.api.interceptor.Interceptor; | |
import org.mule.keygenerator.ExpressionMuleEventKeyGenerator; | |
import org.mule.keygenerator.SHA256MuleEventKeyGenerator; | |
import org.mule.processor.AbstractInterceptingMessageProcessor; | |
import org.springframework.beans.factory.annotation.Required; | |
import org.springframework.cache.Cache; | |
import org.springframework.cache.Cache.ValueWrapper; | |
import org.springframework.cache.CacheManager; | |
/** | |
* An intercepting message processor that is used to look up message payloads | |
* (responses) in cache and return it instead of calling the next message processor in the flow. | |
* | |
*/ | |
public class CacheInterceptor extends AbstractInterceptingMessageProcessor | |
implements Interceptor { | |
private CacheManager cacheManager; | |
private String cacheName; | |
private MuleEventKeyGenerator keyGenerator; | |
private String keyGeneratorExpression; | |
/** | |
* | |
* @param manager | |
* Cache manager | |
*/ | |
@Required | |
public void setCacheManager(CacheManager manager) { | |
cacheManager = manager; | |
} | |
public CacheManager getCacheManager() { | |
return cacheManager; | |
} | |
@Required | |
public void setCacheName(String cacheName) { | |
this.cacheName = cacheName; | |
} | |
public String getCacheName() { | |
return cacheName; | |
} | |
public void setKeyGenerator(MuleEventKeyGenerator keyGenerator) { | |
this.keyGenerator = keyGenerator; | |
} | |
public MuleEventKeyGenerator getKeyGenerator() { | |
return keyGenerator; | |
} | |
public void setKeyGeneratorExpression(String keyGeneratorExpression) { | |
this.keyGeneratorExpression = keyGeneratorExpression; | |
} | |
public String getKeyGeneratorExpression() { | |
return keyGeneratorExpression; | |
} | |
/** | |
* Handle event and intercept next message processor in order to return | |
* cached payload if cache was hit. | |
*/ | |
@Override | |
public MuleEvent process(MuleEvent event) throws MuleException { | |
try { | |
MuleMessage currentMessage = event.getMessage(); | |
Serializable key = createKey(event); | |
Cache cache = this.cacheManager.getCache(this.getCacheName()); | |
ValueWrapper wrapper = cache.get(key); | |
if (wrapper != null) { | |
Object value = wrapper.get(); | |
if (value != null) { | |
return new DefaultMuleEvent(new DefaultMuleMessage(value, | |
currentMessage, event.getMuleContext()), event); | |
} | |
} | |
MuleEvent result = processNext(event); | |
cache.put(key, result.getMessage().getPayload()); | |
return result; | |
} catch (NotSerializableException e) { | |
throw new MessagingException(event, e); | |
} | |
} | |
private Serializable createKey(MuleEvent event) throws NotSerializableException { | |
// Make sure we have a MuleEventKeyGenerator defined | |
if ( this.keyGenerator == null) { | |
if ( this.keyGeneratorExpression != null ) { | |
ExpressionMuleEventKeyGenerator kg = new ExpressionMuleEventKeyGenerator(); | |
kg.setExpression(keyGeneratorExpression); | |
this.keyGenerator = kg; | |
} else { | |
this.keyGenerator = new SHA256MuleEventKeyGenerator(); | |
} | |
} | |
return this.keyGenerator.generateKey(event); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment