summaryrefslogtreecommitdiffstats
path: root/base/common/src
diff options
context:
space:
mode:
authorEndi Sukma Dewata <edewata@redhat.com>2012-10-18 19:30:54 -0500
committerEndi Sukma Dewata <edewata@redhat.com>2012-10-23 15:25:35 -0500
commit7ec6c918b20fc6feea531c2bd6cea48d62b06e36 (patch)
treed90f2ccb76355631d8a723a42c845307942a63d2 /base/common/src
parent2d3d5611143ea3c75823a156ff4ebd331a305253 (diff)
downloadpki-7ec6c918b20fc6feea531c2bd6cea48d62b06e36.tar.gz
pki-7ec6c918b20fc6feea531c2bd6cea48d62b06e36.tar.xz
pki-7ec6c918b20fc6feea531c2bd6cea48d62b06e36.zip
Fixed error handling in RetrieveModificationsTask.
The RetrieveModificationsTask has been modified such that it can recover from errors while still allowing graceful shutdown. The task is scheduled to run once. When it's done it will schecule another one depending on the situation. If the search is abandoned or the connection is closed it will wait one minute before reconnecting. If the system is being shutdown it will not schedule any more task. Ticket #365
Diffstat (limited to 'base/common/src')
-rw-r--r--base/common/src/com/netscape/cmscore/dbs/CertificateRepository.java117
1 files changed, 70 insertions, 47 deletions
diff --git a/base/common/src/com/netscape/cmscore/dbs/CertificateRepository.java b/base/common/src/com/netscape/cmscore/dbs/CertificateRepository.java
index 97bc72ab3..ea041bfe5 100644
--- a/base/common/src/com/netscape/cmscore/dbs/CertificateRepository.java
+++ b/base/common/src/com/netscape/cmscore/dbs/CertificateRepository.java
@@ -247,17 +247,12 @@ public class CertificateRepository extends Repository
return;
}
- CMS.debug("In setCertStatusUpdateInterval listenToCloneModifications=" + listenToCloneModifications);
+ CMS.debug("In setCertStatusUpdateInterval listenToCloneModifications=" + listenToCloneModifications);
if (listenToCloneModifications) {
CMS.debug("In setCertStatusUpdateInterval listening to modifications");
- try {
- retrieveModificationsTask = new RetrieveModificationsTask(this);
- retrieveModificationsTask.start();
- } catch (EBaseException e) {
- retrieveModificationsTask = null;
- e.printStackTrace();
- }
+ retrieveModificationsTask = new RetrieveModificationsTask(this);
+ retrieveModificationsTask.start();
}
CMS.debug("In setCertStatusUpdateInterval scheduling cert status update every " + interval + " seconds.");
@@ -1818,19 +1813,10 @@ public class CertificateRepository extends Repository
return e;
}
- LDAPSearchResults searchForModifiedCertificateRecords(IDBSSession session) {
- CMS.debug("searchForModifiedCertificateRecords");
- LDAPSearchResults results = null;
-
+ LDAPSearchResults searchForModifiedCertificateRecords(IDBSSession session) throws EBaseException {
+ CMS.debug("Starting persistent search.");
String filter = "(" + CertRecord.ATTR_CERT_STATUS + "=*)";
- try {
- results = session.persistentSearch(getDN(), filter, null);
-
- } catch (Exception e) {
- CMS.debug("startSearchForModifiedCertificateRecords persistentSearch Exception=" + e);
- }
-
- return results;
+ return session.persistentSearch(getDN(), filter, null);
}
public void getModifications(LDAPEntry entry) {
@@ -1983,53 +1969,90 @@ class RetrieveModificationsTask implements Runnable {
public RetrieveModificationsTask(CertificateRepository repository) {
this.repository = repository;
+
+ executorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+ public Thread newThread(Runnable r) {
+ return new Thread(r, "RetrieveModificationsTask");
+ }
+ });
+ }
+
+ public void start() {
+
+ // schedule task to run immediately
+ executorService.schedule(this, 0, TimeUnit.MINUTES);
}
- public void start() throws EBaseException {
- // start persistent search
+ public void connect() throws EBaseException {
+
+ if (session != null) return;
+
try {
session = repository.getDBSubsystem().createSession();
results = repository.searchForModifiedCertificateRecords(session);
+
} catch (EBaseException e) {
- stop(); // avoid leaks
+ close(); // avoid leaks
throw e;
}
+ }
- // schedule task to run immediately
- executorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
- public Thread newThread(Runnable r) {
- return new Thread(r, "RetrieveModificationsTask");
- }
- });
- executorService.schedule(this, 0, TimeUnit.MICROSECONDS);
+ public void close() {
+
+ if (session == null) return;
+
+ // make sure the search is abandoned
+ if (results != null) try { session.abandon(results); } catch (Exception e) { e.printStackTrace(); }
+
+ // close session
+ try { session.close(); } catch (Exception e) { e.printStackTrace(); }
+
+ session = null;
}
public void run() {
- CMS.debug("Inside run method of RetrieveModificationsThread");
-
try {
- // results.hasMoreElements() will block until next element becomes available
- // or return false if the search is abandoned or connection is closed
- while (results.hasMoreElements()) {
+ // make sure it's connected
+ connect();
+
+ // results.hasMoreElements() will block until next result becomes available
+ // or return false if the search is abandoned or the connection is closed
+
+ CMS.debug("Waiting for next result.");
+ if (results.hasMoreElements()) {
LDAPEntry entry = results.next();
+
+ CMS.debug("Processing "+entry.getDN()+".");
repository.getModifications(entry);
+ CMS.debug("Done processing "+entry.getDN()+".");
+
+ // wait for next result immediately
+ executorService.schedule(this, 0, TimeUnit.MINUTES);
+
+ } else {
+ if (executorService.isShutdown()) {
+ CMS.debug("Task has been shutdown.");
+
+ } else {
+ CMS.debug("Persistence search ended.");
+ close();
+
+ CMS.debug("Retrying in 1 minute.");
+ executorService.schedule(this, 1, TimeUnit.MINUTES);
+ }
}
+
} catch (Exception e) {
- CMS.debug("Exception: " + e.toString());
+ CMS.debug(e);
+ close();
+
+ CMS.debug("Retrying in 1 minute.");
+ executorService.schedule(this, 1, TimeUnit.MINUTES);
}
- CMS.debug("Done with run method of RetrieveModificationsThread");
}
public void stop() {
- if (executorService != null) executorService.shutdown();
-
- if (session != null) {
- // closing the session doesn't actually close the connection,
- // so the search needs to be abandoned explicitly
- if (results != null) try { session.abandon(results); } catch (Exception e) { e.printStackTrace(); }
-
- // close session
- try { session.close(); } catch (Exception e) { e.printStackTrace(); }
- }
+ executorService.shutdown();
+ close();
}
}