mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Fixed getLock implementation to handle zero retry parameters
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@22933 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -409,6 +409,11 @@ public class JobLockServiceImpl implements JobLockService
|
|||||||
*/
|
*/
|
||||||
private void getLockImpl(final String lockToken, final QName lockQName, final long timeToLive, long retryWait, int retryCount)
|
private void getLockImpl(final String lockToken, final QName lockQName, final long timeToLive, long retryWait, int retryCount)
|
||||||
{
|
{
|
||||||
|
if (retryCount < 0)
|
||||||
|
{
|
||||||
|
throw new IllegalArgumentException("Job lock retry count cannot be negative: " + retryCount);
|
||||||
|
}
|
||||||
|
|
||||||
RetryingTransactionCallback<Object> getLockCallback = new RetryingTransactionCallback<Object>()
|
RetryingTransactionCallback<Object> getLockCallback = new RetryingTransactionCallback<Object>()
|
||||||
{
|
{
|
||||||
public Object execute() throws Throwable
|
public Object execute() throws Throwable
|
||||||
@@ -453,13 +458,14 @@ public class JobLockServiceImpl implements JobLockService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Does the high-level retrying around the callback
|
* Does the high-level retrying around the callback. At least one attempt is made to call the
|
||||||
|
* provided callback.
|
||||||
*/
|
*/
|
||||||
private int doWithRetry(RetryingTransactionCallback<? extends Object> callback, long retryWait, int retryCount)
|
private int doWithRetry(RetryingTransactionCallback<? extends Object> callback, long retryWait, int retryCount)
|
||||||
{
|
{
|
||||||
int iteration = 0;
|
int lockAttempt = 0;
|
||||||
LockAcquisitionException lastException = null;
|
LockAcquisitionException lastException = null;
|
||||||
while (iteration++ < retryCount)
|
while (lockAttempt++ <= retryCount) // lockAttempt incremented after check i.e. 1 for first iteration
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -472,10 +478,10 @@ public class JobLockServiceImpl implements JobLockService
|
|||||||
{
|
{
|
||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
{
|
{
|
||||||
logger.debug("Lock attempt " + iteration + " of " + retryCount + " failed: " + e.getMessage());
|
logger.debug("Lock attempt " + lockAttempt + " of " + retryCount + " failed: " + e.getMessage());
|
||||||
}
|
}
|
||||||
lastException = e;
|
lastException = e;
|
||||||
if (iteration >= retryCount)
|
if (lockAttempt >= retryCount)
|
||||||
{
|
{
|
||||||
// Avoid an unnecessary wait if this is the last attempt
|
// Avoid an unnecessary wait if this is the last attempt
|
||||||
break;
|
break;
|
||||||
@@ -490,7 +496,7 @@ public class JobLockServiceImpl implements JobLockService
|
|||||||
if (lastException == null)
|
if (lastException == null)
|
||||||
{
|
{
|
||||||
// Success
|
// Success
|
||||||
return iteration;
|
return lockAttempt;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@@ -88,6 +88,25 @@ public class JobLockServiceTest extends TestCase
|
|||||||
assertNotNull(jobLockService);
|
assertNotNull(jobLockService);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testSimpleLock()
|
||||||
|
{
|
||||||
|
String lockToken = jobLockService.getLock(lockAAA, 20L);
|
||||||
|
jobLockService.refreshLock(lockToken, lockAAA, 20L);
|
||||||
|
jobLockService.releaseLock(lockToken, lockAAA);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
jobLockService.refreshLock(lockToken, lockAAA, 20L);
|
||||||
|
fail("Lock refresh should have failed after release");
|
||||||
|
}
|
||||||
|
catch (LockAcquisitionException e)
|
||||||
|
{
|
||||||
|
// Expected
|
||||||
|
}
|
||||||
|
lockToken = jobLockService.getLock(lockAAA, 20L, 5L, 0); // No retries
|
||||||
|
jobLockService.refreshLock(lockToken, lockAAA, 20L);
|
||||||
|
jobLockService.releaseLock(lockToken, lockAAA);
|
||||||
|
}
|
||||||
|
|
||||||
public void testEnforceTxn()
|
public void testEnforceTxn()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
Reference in New Issue
Block a user