Created
June 30, 2014 08:12
-
-
Save linusfoldemo/754149111c500bcdd0ba to your computer and use it in GitHub Desktop.
Configure Infinispan as Spring @Cacheable provider including Spring-boot actuator endpoint
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
@Configuration | |
@EnableCaching | |
public class CacheableConfiguration { | |
@Inject | |
private Environment environment; | |
@Bean(destroyMethod = "stop") | |
public SpringEmbeddedCacheManager cacheManager() { | |
final boolean clustered = environment.getRequiredProperty("cache.clustered", Boolean.class); | |
final String multicastAddress = environment.getRequiredProperty("cache.mcast.address"); | |
final String clusterName = environment.getRequiredProperty("cache.cluster.name"); | |
final GlobalConfigurationBuilder configurationBuilder = new GlobalConfigurationBuilder(); | |
if (clustered) { | |
System.setProperty("jgroups.udp.mcast_addr", multicastAddress); | |
configurationBuilder.transport().defaultTransport() | |
.clusterName(clusterName) | |
.addProperty("configurationFile", "jgroups-udp.xml"); | |
} | |
DefaultCacheManager infinispanCacheManager = new DefaultCacheManager(configurationBuilder.build()); | |
// caches goes here | |
infinispanCacheManager.defineConfiguration("user", new ConfigurationBuilder() | |
.clustering().cacheMode(cacheMode(clustered)).build()); | |
infinispanCacheManager.startCache("user"); // optional to start cache, otherwise lazy | |
return new SpringEmbeddedCacheManager(infinispanCacheManager); | |
} | |
private CacheMode cacheMode(boolean clustered) { | |
return clustered ? CacheMode.REPL_ASYNC : CacheMode.LOCAL; | |
} | |
} |
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
public class CacheInfoEndpoint extends AbstractEndpoint<ObjectNode> { | |
public static final boolean DO_NOT_CREATE_IF_MISSING = false; | |
private final SpringEmbeddedCacheManager springEmbeddedCacheManager; | |
public CacheInfoEndpoint(final SpringEmbeddedCacheManager springEmbeddedCacheManager) { | |
super("cache", false, true); | |
this.springEmbeddedCacheManager = springEmbeddedCacheManager; | |
} | |
@Override | |
public ObjectNode invoke() { | |
final ObjectNode root = JsonNodeFactory.instance.objectNode(); | |
final EmbeddedCacheManager cacheManager = springEmbeddedCacheManager.getNativeCacheManager(); | |
root.put("status", cacheManager.getStatus().name()); | |
root.put("cluster", extractClusterInfo(cacheManager)); | |
root.put("caches", extractCaches(cacheManager)); | |
return root; | |
} | |
private ObjectNode extractClusterInfo(EmbeddedCacheManager cacheManager) { | |
final ObjectNode result = JsonNodeFactory.instance.objectNode(); | |
boolean clustered = cacheManager.getCacheManagerConfiguration().isClustered(); | |
result.put("clustered", clustered); | |
if (clustered) { | |
result.put("clusterName", cacheManager.getClusterName()); | |
} | |
result.put("me", unknownIfNull(cacheManager.getAddress())); | |
result.put("members", unknownIfNull(cacheManager.getMembers())); | |
return result; | |
} | |
private ObjectNode extractCaches(EmbeddedCacheManager cacheManager) { | |
ObjectNode result = JsonNodeFactory.instance.objectNode(); | |
for (String cacheName : cacheManager.getCacheNames()) { | |
final ObjectNode cacheNode = JsonNodeFactory.instance.objectNode(); | |
if (cacheManager.cacheExists(cacheName)) { | |
final Cache<Object, Object> cache = cacheManager.getCache(cacheName, DO_NOT_CREATE_IF_MISSING); | |
cacheNode.put("status", cache.getStatus().name()); | |
int numberOfItems = cache.size(); | |
cacheNode.put("numberOfItems", numberOfItems); | |
cacheNode.put("md5-checksum", numberOfItems > 0 ? md5Hash(cache) : "N/A"); | |
} else { | |
cacheNode.put("status", "Not existing"); | |
} | |
result.put(cacheName, cacheNode); | |
} | |
return result; | |
} | |
private String md5Hash(Cache<Object, Object> cache) { | |
Hasher md5 = Hashing.md5().newHasher(); | |
for (Map.Entry<Object, Object> entry : cache.entrySet()) { | |
md5.putInt(entry.getKey().hashCode()); | |
md5.putInt(entry.getValue().hashCode()); | |
} | |
return md5.hash().toString(); | |
} | |
private String unknownIfNull(Object object) { | |
return object != null ? object.toString() : "unknown"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment