我正在使用 spring 应用程序,我的 gigaspace 在启动时正在连接。如果 gigaspace 出现故障,我不会遇到任何异常。
@Override
public void onContextRefreshed(ContextRefreshedEvent event) {
String gigaSpaceURL = null;
LOGGER.info("({}) initializing gigaspaces client", getName());
try {
initGSProxy();
Iterator<Map.Entry<ConfiguredSpace, Space>> entries = spaces.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<ConfiguredSpace, Space> entry = entries.next();
LOGGER.info("({}) initialing space- key=" +
entry.getKey() + ", value = " + entry.getValue(),
getName());
// TODO : Need to verify Boolean Value Input
gigaspace.createSpace(entry.getKey().name(),
entry.getValue().getURL(), false);
gigaSpaceURL = entry.getValue().getURL();
}
} catch (Exception e) {
return;
}
GenericUtil.updateLogLevel("INFO",
"com.renovite.ripps.ap.gs.Spaces");
LOGGER.info("\n************************************\nConnected with Gigaspace successfully:URL:" + gigaSpaceURL
+ "\n************************************\n");
GenericUtil.updateLogLevel("ERROR",
"com.renovite.ripps.ap.gs.Spaces");
}
最佳答案
使用 getGigaSpace() 方法引用 Gigaspace,该方法以 spacekey 作为参数。如果在运行时抛出异常,则意味着应用程序无法连接指定的 Gigaspace url。
或更优雅的方式,在您的 Gigaspace 代理类(实际上实现 IGigaspace)中重写 getGigaSpace() 方法,以便在无法连接时返回 null。
/** 空格。 */ 私有(private) transient 映射空间 = new HashMap<>();
@覆盖 公共(public) GigaSpace getGigaSpace(String spaceKey) {
if(spaces.get(spaceKey) != null){
return spaces.get(spaceKey).getGigaSpace();
}
return null;
}
spaces 是在 Gigapsace 中注册的所有 url 的映射。如果没有人注册,我们将在上述方法中返回 null。
关于java - 如何在启动时知道 Java 应用程序中的 Gigaspace 已连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41610339/