-
Notifications
You must be signed in to change notification settings - Fork 913
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARTEMIS-856 - Support consumersBeforeDispatch and delayBeforeDispatch
https://issues.apache.org/jira/browse/ARTEMIS-856 This is equivalent to consumersBeforeDispatchStarts and timeBeforeDispatchStarts in ActiveMQ 5.x http://activemq.apache.org/message-groups.html This is addressing one of the items on the artemis roadmap: http://activemq.apache.org/activemq-artemis-roadmap.html
- Loading branch information
1 parent
ae29edf
commit aae5493
Showing
29 changed files
with
1,145 additions
and
51 deletions.
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
...is-commons/src/main/java/org/apache/activemq/artemis/utils/AtomicBooleanFieldUpdater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.activemq.artemis.utils; | ||
|
||
import java.util.concurrent.atomic.AtomicLongFieldUpdater; | ||
import sun.reflect.CallerSensitive; | ||
|
||
public class AtomicBooleanFieldUpdater<T> { | ||
|
||
/** | ||
* Creates and returns an updater for objects with the given field. | ||
* The Class argument is needed to check that reflective types and | ||
* generic types match. | ||
* | ||
* @param tclass the class of the objects holding the field | ||
* @param fieldName the name of the field to be updated | ||
* @param <U> the type of instances of tclass | ||
* @return the updater | ||
* @throws IllegalArgumentException if the field is not a | ||
* volatile long type | ||
* @throws RuntimeException with a nested reflection-based | ||
* exception if the class does not hold field or is the wrong type, | ||
* or the field is inaccessible to the caller according to Java language | ||
* access control | ||
*/ | ||
@CallerSensitive | ||
public static <U> AtomicBooleanFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) { | ||
return new AtomicBooleanFieldUpdater<>(AtomicLongFieldUpdater.newUpdater(tclass, fieldName)); | ||
} | ||
|
||
private static long toLong(boolean value) { | ||
return value ? 1 : 0; | ||
} | ||
|
||
private static boolean toBoolean(long value) { | ||
return value != 0; | ||
} | ||
|
||
private final AtomicLongFieldUpdater<T> atomicLongFieldUpdater; | ||
|
||
/** | ||
* Protected do-nothing constructor for use by subclasses. | ||
*/ | ||
protected AtomicBooleanFieldUpdater(AtomicLongFieldUpdater<T> atomicLongFieldUpdater) { | ||
this.atomicLongFieldUpdater = atomicLongFieldUpdater; | ||
} | ||
|
||
/** | ||
* Atomically sets the field of the given object managed by this updater | ||
* to the given updated value if the current value {@code ==} the | ||
* expected value. This method is guaranteed to be atomic with respect to | ||
* other calls to {@code compareAndSet} and {@code set}, but not | ||
* necessarily with respect to other changes in the field. | ||
* | ||
* @param obj An object whose field to conditionally set | ||
* @param expect the expected value | ||
* @param update the new value | ||
* @return {@code true} if successful | ||
* @throws ClassCastException if {@code obj} is not an instance | ||
* of the class possessing the field established in the constructor | ||
*/ | ||
public boolean compareAndSet(T obj, boolean expect, boolean update) { | ||
return atomicLongFieldUpdater.compareAndSet(obj, toLong(expect), toLong(update)); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Atomically sets the field of the given object managed by this updater | ||
* to the given updated value if the current value {@code ==} the | ||
* expected value. This method is guaranteed to be atomic with respect to | ||
* other calls to {@code compareAndSet} and {@code set}, but not | ||
* necessarily with respect to other changes in the field. | ||
* | ||
* <p><a href="package-summary.html#weakCompareAndSet">May fail | ||
* spuriously and does not provide ordering guarantees</a>, so is | ||
* only rarely an appropriate alternative to {@code compareAndSet}. | ||
* | ||
* @param obj An object whose field to conditionally set | ||
* @param expect the expected value | ||
* @param update the new value | ||
* @return {@code true} if successful | ||
* @throws ClassCastException if {@code obj} is not an instance | ||
* of the class possessing the field established in the constructor | ||
*/ | ||
public boolean weakCompareAndSet(T obj, boolean expect, boolean update) { | ||
return atomicLongFieldUpdater.weakCompareAndSet(obj, toLong(expect), toLong(update)); | ||
} | ||
|
||
/** | ||
* Sets the field of the given object managed by this updater to the | ||
* given updated value. This operation is guaranteed to act as a volatile | ||
* store with respect to subsequent invocations of {@code compareAndSet}. | ||
* | ||
* @param obj An object whose field to set | ||
* @param newValue the new value | ||
*/ | ||
public void set(T obj, boolean newValue) { | ||
atomicLongFieldUpdater.set(obj, toLong(newValue)); | ||
} | ||
|
||
/** | ||
* Eventually sets the field of the given object managed by this | ||
* updater to the given updated value. | ||
* | ||
* @param obj An object whose field to set | ||
* @param newValue the new value | ||
* @since 1.6 | ||
*/ | ||
public void lazySet(T obj, boolean newValue) { | ||
atomicLongFieldUpdater.lazySet(obj, toLong(newValue)); | ||
} | ||
|
||
/** | ||
* Gets the current value held in the field of the given object managed | ||
* by this updater. | ||
* | ||
* @param obj An object whose field to get | ||
* @return the current value | ||
*/ | ||
public boolean get(T obj) { | ||
return toBoolean(atomicLongFieldUpdater.get(obj)); | ||
} | ||
|
||
/** | ||
* Atomically sets the field of the given object managed by this updater | ||
* to the given value and returns the old value. | ||
* | ||
* @param obj An object whose field to get and set | ||
* @param newValue the new value | ||
* @return the previous value | ||
*/ | ||
public boolean getAndSet(T obj, boolean newValue) { | ||
return toBoolean(atomicLongFieldUpdater.getAndSet(obj, toLong(newValue))); | ||
} | ||
|
||
public String toString(T obj) { | ||
return Boolean.toString(get(obj)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.