Search in sources :

Example 1 with ThreadDumpStrategy

use of org.apache.wicket.settings.ExceptionSettings.ThreadDumpStrategy in project wicket by apache.

the class PageAccessSynchronizer method lockPage.

/**
 * Acquire a lock to a page
 *
 * @param pageId
 *            page id
 * @throws CouldNotLockPageException
 *             if lock could not be acquired
 */
public void lockPage(int pageId) throws CouldNotLockPageException {
    final Thread thread = Thread.currentThread();
    final PageLock lock = new PageLock(pageId, thread);
    final Time start = Time.now();
    boolean locked = false;
    final boolean isDebugEnabled = logger.isDebugEnabled();
    PageLock previous = null;
    Duration timeout = getTimeout(pageId);
    while (!locked && start.elapsedSince().lessThan(timeout)) {
        if (isDebugEnabled) {
            logger.debug("'{}' attempting to acquire lock to page with id '{}'", thread.getName(), pageId);
        }
        previous = locks.get().putIfAbsent(pageId, lock);
        if (previous == null || previous.thread == thread) {
            // first thread to acquire lock or lock is already owned by this thread
            locked = true;
        } else {
            // wait for a lock to become available
            long remaining = remaining(start, timeout);
            if (remaining > 0) {
                previous.waitForRelease(remaining, isDebugEnabled);
            }
        }
    }
    if (locked) {
        if (isDebugEnabled) {
            logger.debug("{} acquired lock to page {}", thread.getName(), pageId);
        }
    } else {
        if (logger.isWarnEnabled()) {
            logger.warn("Thread '{}' failed to acquire lock to page with id '{}', attempted for {} out of allowed {}." + " The thread that holds the lock has name '{}'.", thread.getName(), pageId, start.elapsedSince(), timeout, previous.thread.getName());
            if (Application.exists()) {
                ThreadDumpStrategy strategy = Application.get().getExceptionSettings().getThreadDumpStrategy();
                switch(strategy) {
                    case ALL_THREADS:
                        Threads.dumpAllThreads(logger);
                        break;
                    case THREAD_HOLDING_LOCK:
                        Threads.dumpSingleThread(logger, previous.thread);
                        break;
                    case NO_THREADS:
                    default:
                }
            }
        }
        throw new CouldNotLockPageException(pageId, thread.getName(), timeout);
    }
}
Also used : ThreadDumpStrategy(org.apache.wicket.settings.ExceptionSettings.ThreadDumpStrategy) Time(org.apache.wicket.util.time.Time) Duration(org.apache.wicket.util.time.Duration)

Aggregations

ThreadDumpStrategy (org.apache.wicket.settings.ExceptionSettings.ThreadDumpStrategy)1 Duration (org.apache.wicket.util.time.Duration)1 Time (org.apache.wicket.util.time.Time)1