use of org.alfresco.repo.lock.JobLockService.JobLockRefreshCallback in project alfresco-repository by Alfresco.
the class NodeStringLengthWorker method execute.
/**
* Performs the work, including logging details of progress.
*/
public NodeStringLengthWorkResult execute() {
// Build refresh callback
final NodeStringLengthWorkResult progress = new NodeStringLengthWorkResult();
JobLockRefreshCallback lockCallback = new JobLockRefreshCallback() {
@Override
public void lockReleased() {
progress.inProgress.set(false);
}
@Override
public boolean isActive() {
return progress.inProgress.get();
}
};
String lockToken = null;
try {
progress.inProgress.set(true);
// Get the lock
lockToken = jobLockService.getLock(LOCK, LOCK_TTL);
// Start the refresh timer
jobLockService.refreshLock(lockToken, LOCK, LOCK_TTL, lockCallback);
// Now we know that we'll do something
if (logger.isInfoEnabled()) {
logger.info("NodeStringLengthWorker: Starting");
}
// Do the work
doWork(progress);
// Done
if (logger.isInfoEnabled()) {
logger.info("NodeStringLengthWorker: " + progress);
}
} catch (LockAcquisitionException e) {
if (logger.isDebugEnabled()) {
logger.debug("Skipping node string length job: " + e.getMessage());
}
} catch (Exception e) {
progress.inProgress.set(false);
logger.error("Node string length job " + progress);
logger.error("Stopping node string length job with exception.", e);
} finally {
if (lockToken != null) {
jobLockService.releaseLock(lockToken, LOCK);
}
// The background
progress.inProgress.set(false);
}
// Done
return progress;
}
use of org.alfresco.repo.lock.JobLockService.JobLockRefreshCallback in project alfresco-repository by Alfresco.
the class JobLockServiceTest method testLockCallbackReleaseInactive.
public synchronized void testLockCallbackReleaseInactive() throws Exception {
final QName lockQName = QName.createQName(NAMESPACE, getName());
final long lockTTL = 1000L;
final String lockToken = jobLockService.getLock(lockQName, lockTTL);
final int[] checked = new int[1];
final int[] released = new int[1];
// Immediately-inactive job
JobLockRefreshCallback callback = new JobLockRefreshCallback() {
@Override
public boolean isActive() {
checked[0]++;
return false;
}
@Override
public void lockReleased() {
released[0]++;
}
};
jobLockService.refreshLock(lockToken, lockQName, lockTTL, callback);
// The first refresh will occur in 500ms
wait(1000L);
// Should have been released by now
assertTrue("Expected lockReleased to have been called", released[0] > 0);
try {
jobLockService.getLock(lockQName, lockTTL);
} catch (LockAcquisitionException e) {
fail("Lock should have been released by callback infrastructure");
}
// Check that the timed callback is killed properly
int checkedCount = checked[0];
int releasedCount = released[0];
wait(2000L);
assertEquals("Lock callback timer was not terminated", checkedCount, checked[0]);
assertEquals("Lock callback timer was not terminated", releasedCount, released[0]);
}
use of org.alfresco.repo.lock.JobLockService.JobLockRefreshCallback in project alfresco-repository by Alfresco.
the class JobLockServiceTest method testLockCallbackReleaseSelf.
public synchronized void testLockCallbackReleaseSelf() throws Exception {
// ACE-4347 extra debug logging just for this test so we can see what's going on when it next fails
Level saveLogLevel = Logger.getLogger("org.alfresco.repo.lock").getLevel();
Logger.getLogger("org.alfresco.repo.lock").setLevel(Level.ALL);
try {
final QName lockQName = QName.createQName(NAMESPACE, getName());
final long lockTTL = 1000L;
final String lockToken = jobLockService.getLock(lockQName, lockTTL);
final int[] checked = new int[1];
final int[] released = new int[1];
// Immediately-inactive job, releasing the lock
JobLockRefreshCallback callback = new JobLockRefreshCallback() {
@Override
public boolean isActive() {
checked[0]++;
jobLockService.releaseLock(lockToken, lockQName);
return false;
}
@Override
public void lockReleased() {
released[0]++;
}
};
jobLockService.refreshLock(lockToken, lockQName, lockTTL, callback);
// The first refresh will occur in 500ms
wait(1000L);
// Should NOT get a callback saying that the lock has been released
assertFalse("Lock should be optimistically released", released[0] > 0);
try {
jobLockService.getLock(lockQName, lockTTL);
} catch (LockAcquisitionException e) {
fail("Lock should have been released by callback infrastructure");
}
// Check that the timed callback is killed properly
int checkedCount = checked[0];
int releasedCount = released[0];
if (logger.isDebugEnabled()) {
logger.debug("checkedCount=" + checkedCount + ",releasedCount=" + releasedCount);
}
wait(10000L);
assertEquals("Lock callback timer was not terminated", checkedCount, checked[0]);
assertEquals("Lock callback timer was not terminated", releasedCount, released[0]);
} finally {
Logger.getLogger("org.alfresco.repo.lock").setLevel(saveLogLevel);
}
}
use of org.alfresco.repo.lock.JobLockService.JobLockRefreshCallback in project alfresco-repository by Alfresco.
the class JobLockServiceTest method testLockCallbackReleaseTimed.
/**
* Lets job "run" for 3 seconds and checks at 2s and 4s.
*/
public synchronized void testLockCallbackReleaseTimed() throws Exception {
final QName lockQName = QName.createQName(NAMESPACE, getName());
final long lockTTL = 1000L;
final long lockNow = System.currentTimeMillis();
final String lockToken = jobLockService.getLock(lockQName, lockTTL);
final int[] checked = new int[1];
final int[] released = new int[1];
// Do not release and remain active
JobLockRefreshCallback callback = new JobLockRefreshCallback() {
@Override
public boolean isActive() {
checked[0]++;
if (System.currentTimeMillis() - lockNow > 3000L) {
return false;
} else {
return true;
}
}
@Override
public void lockReleased() {
released[0]++;
}
};
jobLockService.refreshLock(lockToken, lockQName, lockTTL, callback);
// The first refresh will occur in 500ms
wait(2000L);
assertTrue("Expected at least 2 active checks; only got " + checked[0], checked[0] >= 2);
assertFalse("lockReleased should NOT have been called", released[0] > 0);
try {
jobLockService.getLock(lockQName, lockTTL);
fail("Lock should still be held");
} catch (LockAcquisitionException e) {
// Expected
}
// Wait for another 2s to be sure that the lock is run to completion
wait(2000L);
// Check that the timed callback is killed properly
int checkedCount = checked[0];
int releasedCount = released[0];
wait(2000L);
assertEquals("Lock callback timer was not terminated", checkedCount, checked[0]);
assertEquals("Lock callback timer was not terminated", releasedCount, released[0]);
}
Aggregations