Ate Douma pushed to branch feature/CMS-10897 at cms-community / 
hippo-services-api


Commits:
f1e8869a by Ate Douma at 2017-10-15T23:07:37+02:00
CMS-10897 adding LockManagerUtils to support waiting for a lock

Unit test will be provided through hippo-repository/test module

- - - - -


3 changed files:

- + src/main/java/org/onehippo/cms7/services/lock/AlreadyLockedException.java
- src/main/java/org/onehippo/cms7/services/lock/LockManagerException.java
- + src/main/java/org/onehippo/cms7/services/lock/LockManagerUtils.java


Changes:

=====================================
src/main/java/org/onehippo/cms7/services/lock/AlreadyLockedException.java
=====================================
--- /dev/null
+++ b/src/main/java/org/onehippo/cms7/services/lock/AlreadyLockedException.java
@@ -0,0 +1,39 @@
+/*
+ *  Copyright 2017 Hippo B.V. (http://www.onehippo.com)
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.onehippo.cms7.services.lock;
+
+public class AlreadyLockedException extends LockException {
+
+    public AlreadyLockedException() {
+        super();
+    }
+
+    public AlreadyLockedException(final String message) {
+        super(message);
+    }
+
+    public AlreadyLockedException(final String message, final Throwable cause) 
{
+        super(message, cause);
+    }
+
+    public AlreadyLockedException(final Throwable cause) {
+        super(cause);
+    }
+
+    public AlreadyLockedException(final String message, final Throwable cause, 
final boolean enableSuppression, final boolean writableStackTrace) {
+        super(message, cause, enableSuppression, writableStackTrace);
+    }
+}


=====================================
src/main/java/org/onehippo/cms7/services/lock/LockManagerException.java
=====================================
--- a/src/main/java/org/onehippo/cms7/services/lock/LockManagerException.java
+++ b/src/main/java/org/onehippo/cms7/services/lock/LockManagerException.java
@@ -15,7 +15,7 @@
  */
 package org.onehippo.cms7.services.lock;
 
-public class LockManagerException extends Exception {
+public class LockManagerException extends LockException {
     public LockManagerException() {
         super();
     }


=====================================
src/main/java/org/onehippo/cms7/services/lock/LockManagerUtils.java
=====================================
--- /dev/null
+++ b/src/main/java/org/onehippo/cms7/services/lock/LockManagerUtils.java
@@ -0,0 +1,70 @@
+/*
+ *  Copyright 2017 Hippo B.V. (http://www.onehippo.com)
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.onehippo.cms7.services.lock;
+
+import java.util.concurrent.TimeoutException;
+
+public class LockManagerUtils {
+
+    /**
+     * Utility method to create and if needed wait indefinitely (unless 
interrupted) for a {@link LockManager#lock(String)}
+     * @param lockManager lockManager
+     * @param key the key for the {@link Lock} where {@code key} is now 
allowed to exceed 256 chars
+     * @param waitInterval time in milliseconds to wait before retrying 
creating the lock
+     * @return {@link LockResource} such that this {@link #lock(String)} 
method can be used in a try-with-resources statement
+     *         where the {@link LockResource#close()} results in the lock 
being freed.
+     * @throws LockException if the lock could not be created (other then 
{@link AlreadyLockedException})
+     * @throws InterruptedException when the thread is interrupted while 
waiting before retrying to create the lock
+     */
+    public static LockResource waitForLock(final LockManager lockManager, 
final String key, final long waitInterval)
+            throws LockException, InterruptedException {
+        try {
+            return waitForLock(lockManager, key, waitInterval, 0);
+        } catch (TimeoutException ignore) {
+            // not going to happen, needed to keep the compiler happy
+            return null;
+        }
+    }
+
+    /**
+     * Utility method to create and if needed wait for a maximum amount of 
time (unless interrupted) for a {@link LockManager#lock(String)}
+     * @param lockManager lockManager
+     * @param key the key for the {@link Lock} where {@code key} is now 
allowed to exceed 256 chars
+     * @param waitInterval time in milliseconds to wait before retrying 
creating the lock
+     * @param maxWait maximum time in milliseconds for trying to create the 
lock, will throw TimeoutException when exceeded.
+     * @return {@link LockResource} such that this {@link #lock(String)} 
method can be used in a try-with-resources statement
+     *         where the {@link LockResource#close()} results in the lock 
being freed.
+     * @throws LockException if the lock could not be created (other then 
{@link AlreadyLockedException})
+     * @throws TimeoutException when the maxWait time has exceeded while 
trying to create the lock
+     * @throws InterruptedException when the thread is interrupted while 
waiting before retrying to create the lock
+     */
+    public static LockResource waitForLock(final LockManager lockManager, 
final String key, final long waitInterval, final long maxWait)
+            throws LockException, TimeoutException, InterruptedException {
+        final long timeoutTime = maxWait > 0 ? System.currentTimeMillis() + 
maxWait : 0;
+        while (true) {
+            try {
+                return lockManager.lock(key);
+            } catch (AlreadyLockedException e) {
+                if (timeoutTime > 0 &&
+                        (System.currentTimeMillis() > timeoutTime ||
+                                (System.currentTimeMillis() + waitInterval) > 
timeoutTime)) {
+                    throw new TimeoutException();
+                }
+                Thread.sleep(waitInterval);
+            }
+        }
+    }
+}



View it on GitLab: 
https://code.onehippo.org/cms-community/hippo-services-api/commit/f1e8869ac6e7fe410b67ea4c5227ef19e88f9377

---
View it on GitLab: 
https://code.onehippo.org/cms-community/hippo-services-api/commit/f1e8869ac6e7fe410b67ea4c5227ef19e88f9377
You're receiving this email because of your account on code.onehippo.org.
_______________________________________________
Hippocms-svn mailing list
Hippocms-svn@lists.onehippo.org
https://lists.onehippo.org/mailman/listinfo/hippocms-svn

Reply via email to