Skip to content

Instantly share code, notes, and snippets.

@CorneAussems
Last active November 4, 2025 11:40
Show Gist options
  • Select an option

  • Save CorneAussems/b4331104cb93fd681c6e5afcbb0475fc to your computer and use it in GitHub Desktop.

Select an option

Save CorneAussems/b4331104cb93fd681c6e5afcbb0475fc to your computer and use it in GitHub Desktop.
Liferay 7.4 check multiVM Cache working
# from https://liferay.dev/b/how-to-diagnose-and-recover-liferay-cluster
import com.liferay.portal.kernel.cache.MultiVMPool;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import com.liferay.portal.kernel.module.util.SystemBundleUtil;
import com.liferay.portal.kernel.cache.PortalCache;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.cluster.ClusterExecutorUtil;
import com.liferay.portal.kernel.cluster.ClusterMasterExecutorUtil;
private MultiVMPool getMultiVMPool() {
// Registry registry = RegistryUtil.getRegistry();
//ServiceReference serviceReference = registry.getServiceReference(MultiVMPool.class);
BundleContext bundleContext = SystemBundleUtil.getBundleContext();
ServiceReference serviceReference = bundleContext.getServiceReference(MultiVMPool.class.name)
return bundleContext.getService(serviceReference)
}
try {
// Get local cluster node information
String localClusterNode = ClusterExecutorUtil.getLocalClusterNode();
boolean isMaster = ClusterMasterExecutorUtil.isMaster();
// Check if clustering information is available
if (localClusterNode != null) {
println("=== Execution in: " + localClusterNode);
} else {
println("=== Clustering information is not available.");
}
println("=== isMaster: " + isMaster);
MultiVMPool multiVMPool = getMultiVMPool();
if (multiVMPool != null) {
now = new Date()
println(now)
PortalCache<String, String> portalCache1 = multiVMPool.getPortalCache("CACHE-TEST-1");
println("=== Value for testKey before (SHOULD BE INVALIDATED AND NULL): " + portalCache1.get("testKey"));
portalCache1.put("testKey", "testValue:"+now);
println("=== Value for testKey after: " + portalCache1.get("testKey"));
} else {
println("MultiVMPool service is not available.");
}
} catch (Exception e) {
println(e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment