不幸的是,没有内置的方法来实现你想要的东西,但是Glassfish基于其集群的浅滩框架可以帮助你。您可以通过向集群成员发送通知以更新其高速缓存或将当前高速缓存替换为分布式高速缓存来解决此问题。
下面是一个使用浅滩发送通知的示例:
@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();
放置在缓存中的项目将复制到其他群集节点。