不幸的是,没有实现您想要的内置方法,但是 Glassfish 集群所基于的shoal框架可以帮助您。您可以通过向集群成员发送通知以更新其缓存或将当前缓存替换为分布式缓存来解决此问题。
下面是一个使用 shoal 发送通知的例子:
@Startup
@Singleton
public class Test {
private String groupName = "mygroup";
private String serverName = System.getProperty("HTTP_LISTENER_PORT");
private GroupManagementService gms;
@PostConstruct
public void init() {
Runnable gmsRunnable = GMSFactory.startGMSModule(serverName, groupName,
GroupManagementService.MemberType.CORE, null);
gms = (GroupManagementService) gmsRunnable;
try {
gms.join();
gms.addActionFactory(new MessageActionFactory() {
@Override
public Action produceAction() {
return new MessageAction() {
@Override
public void consumeSignal(Signal signal)
throws ActionException {
// Update your cache here
}
};
}
}, groupName);
} catch (GMSException e) {
Logger.getAnonymousLogger().severe(e.getMessage());
}
}
@PreDestroy
public void cleanup() {
gms.shutdown(GMSConstants.shutdownType.INSTANCE_SHUTDOWN);
}
/**
* Call this from your button click.
*/
public void updateCache() {
try {
byte[] message = new byte[] {};
gms.getGroupHandle().sendMessage(groupName, message);
} catch (GMSException e) {
Logger.getAnonymousLogger().severe(e.getMessage());
}
}
}
如果您想改用分布式缓存:
DistributedStateCache cache = gms.getGroupHandle().getDistributedStateCache();
放置在缓存中的项目将被复制到其他集群节点。