Reputation: 502
In the Cyc API example code at The Cyc API example code page, System.exit() is used to terminate the Cyc connection. Generally, this seems as if it may be using a mallet to crack an egg. Is there a more narrowly focused way to terminate the thread that is maintaining the Cyc connection, so that simply falling through to the end of main (e.g.) causes the program to terminate?
Upvotes: 3
Views: 76
Reputation: 71
(Disclaimer: I am one of the developers of the Cyc APIs...)
Terminating connections was more difficult than it needed to be in earlier versions of the Cyc Core APIs, but the 1.0.0-rc4 release adds some functionality to address this issue.
The Session API's SessionManager is intended to be ultimately responsible for overseeing connection pool management. Connections may be reused across sessions, and in most cases, you should not need to worry about directly creating, caching, or closing connections to Cyc servers. The rationale is this: the underlying CycAccess connections are actually fairly heavyweight (creating one can take 100ms or more), so you don't want to terminate them casually, and you probably don't want the hassle of directly managing them in, e.g., multi-threaded environments like servlets.
Normally, you should only need to close individual CycSessions when you are done with them; the SessionManager will close the underlying connection once there are no resources which rely upon it. The CycSession interface extends java.io.Closeable, so you can do the following:
try {
// Some calls to KB API, Query API, etc...
} finally {
CycSessionManager.getCurrentSession().close();
}
In Java 7 and later, this means that CycSessions implement java.lang.AutoCloseable, so you can wrap them in try-with-resources statements instead:
try (CycSession session = CycSessionManager.getCurrentSession()) {
// Some calls to KB API, Query API, etc...
}
// The session has been automatically closed. The next call will return a new session:
CycSession newSession = CycSessionManager.getCurrentSession();
It's also possible to close the SessionManager, forcing it to close all connections. This is useful when, e.g., exiting applications:
public static void main(String[] args) throws IOException {
try (SessionManager sessionMgr = CycSessionManager.getInstance()) {
// Some calls to KB API, Query API, etc...
}
}
Or, you can close it manually:
try {
// Some calls to KB API, Query API, etc...
} finally {
CycSessionManager.getInstance().close();
}
Once a SessionManager instance has been closed, it can no longer be used to create or retrieve sessions. However, you may instruct the CycSessionManager to load a new SessionManager instance:
CycSessionManager.getInstance().close();
CycSessionManager.getInstance().isClosed(); // true
CycSessionManager.reloadInstance();
CycSessionManager.getInstance().isClosed(); // false
It's actually redundant (albeit harmless) to call SessionManager#close() before CycSessionManager#reloadInstance(); reloadInstance makes sure that the previous SessionManager was closed before replacing it. In any case, I wouldn't normally recommend writing code that relies heavily on reloading the SessionManager (for the reasons described above) but it's there if you need it.
That's the gist of it. There's further documentation here: http://dev.cyc.com/api/core/session/connection-management/
Upvotes: 4