improved conversation locking

This commit is contained in:
Keith Donald
2008-03-31 18:21:10 +00:00
parent b40c7fd8f0
commit 7e350051cd
4 changed files with 35 additions and 4 deletions

View File

@@ -101,7 +101,6 @@ class ContainedConversation implements Conversation, Serializable {
logger.debug("Unlocking conversation " + id);
}
lock.unlock();
// re-bind the conversation container in the session
// this is required to make session replication work correctly in
// a clustered environment

View File

@@ -16,6 +16,7 @@
package org.springframework.webflow.conversation.impl;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@@ -32,8 +33,21 @@ class JdkConcurrentConversationLock implements ConversationLock, Serializable {
*/
private Lock lock = new ReentrantLock();
private int timeoutSeconds = 30;
public void setTimeoutSeconds(int timeoutSeconds) {
this.timeoutSeconds = timeoutSeconds;
}
public void lock() {
lock.lock();
try {
boolean acquired = lock.tryLock(timeoutSeconds, TimeUnit.SECONDS);
if (!acquired) {
throw new LockException("Unable to acquire conversation lock after " + timeoutSeconds + " seconds");
}
} catch (InterruptedException e) {
throw new IllegalStateException("Unable to acquire conversation lock - thread interrupted", e);
}
}
public void unlock() {

View File

@@ -0,0 +1,9 @@
package org.springframework.webflow.conversation.impl;
import org.springframework.webflow.conversation.ConversationException;
public class LockException extends ConversationException {
public LockException(String message) {
super(message);
}
}

View File

@@ -34,15 +34,24 @@ class UtilConcurrentConversationLock implements ConversationLock {
*/
private final ReentrantLock lock = new ReentrantLock();
private int timeoutMills;
public void setTimeoutSeconds(int timeoutSeconds) {
this.timeoutMills = timeoutSeconds * 1000;
}
/**
* Acquires the lock.
* @throws SystemInterruptedException if the lock cannot be acquired due to interruption
*/
public void lock() {
try {
lock.acquire();
boolean acquired = lock.attempt(timeoutMills);
if (!acquired) {
throw new LockException("Unable to acquire conversation lock after " + timeoutMills / 1000 + " seconds");
}
} catch (InterruptedException e) {
throw new SystemInterruptedException("Unable to acquire lock.", e);
throw new SystemInterruptedException("Unable to acquire conversation lock - thread interrupted", e);
}
}