Class OortPrimaryLong
- java.lang.Object
-
- org.eclipse.jetty.util.component.AbstractLifeCycle
-
- org.cometd.oort.OortService<R,C>
-
- org.cometd.oort.OortPrimaryService<java.lang.Long,OortPrimaryLong.Context>
-
- org.cometd.oort.OortPrimaryLong
-
- All Implemented Interfaces:
java.util.EventListener
,Bayeux.BayeuxListener
,ConfigurableServerChannel.ServerChannelListener
,ServerChannel.MessageListener
,org.eclipse.jetty.util.component.LifeCycle
public class OortPrimaryLong extends OortPrimaryService<java.lang.Long,OortPrimaryLong.Context>
A distributed counter service to be deployed on a Oort cluster that modifies a
long
value hosted in a "primary" node.Instances of this service may be used as unique ID generator, or as unique counter across the cluster, for example to aggregate values from different nodes, such as the number of users connected to each node.
Applications may call methods
addAndGet(long, Callback)
orgetAndAdd(long, Callback)
providing the amount to add (it may be negative) and aOortPrimaryLong.Callback
object that will be invoked on the requesting node when the result has been computed and transmitted back by the "primary" node.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
OortPrimaryLong.Callback
Callback invoked when the result of the operation on the counter is available, or when the operation failed.protected static class
OortPrimaryLong.Context
-
Nested classes/interfaces inherited from class org.cometd.oort.OortService
OortService.Request, OortService.Result<U>, OortService.ServerContext
-
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener, org.eclipse.jetty.util.component.AbstractLifeCycle.StopException
-
Nested classes/interfaces inherited from interface org.cometd.bayeux.server.ConfigurableServerChannel.ServerChannelListener
ConfigurableServerChannel.ServerChannelListener.Weak
-
-
Constructor Summary
Constructors Constructor Description OortPrimaryLong(Oort oort, java.lang.String name, boolean primary)
OortPrimaryLong(Oort oort, java.lang.String name, boolean primary, long initial)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addAndGet(long delta, OortPrimaryLong.Callback callback)
Adds the givendelta
and then invokes the givencallback
with the counter value after the addition.boolean
get(OortPrimaryLong.Callback callback)
Retrieves the value from the "primary" node and then invokes the givencallback
with the result value.boolean
getAndAdd(long delta, OortPrimaryLong.Callback callback)
Adds the givendelta
and then invokes the givencallback
with the counter value before the addition.protected long
getValue()
protected OortService.Result<java.lang.Long>
onForward(OortService.Request request)
Subclasses must implement this method, that runs on the owner node, to implement the action functionality.protected void
onForwardFailed(java.lang.Object failure, OortPrimaryLong.Context context)
Subclasses must implement this method, that runs on the requesting node, to complete the functionality after the action failed on the owner node.protected void
onForwardSucceeded(java.lang.Long result, OortPrimaryLong.Context context)
Subclasses must implement this method, that runs on the requesting node, to complete the functionality after the action has been successfully run on the owner node.-
Methods inherited from class org.cometd.oort.OortPrimaryService
doStart, doStop, getPrimaryOortURL, isPrimary
-
Methods inherited from class org.cometd.oort.OortService
forward, getLocalSession, getName, getOort, getTimeout, onForwardMessage, onMessage, onResultMessage, setTimeout, toString
-
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
addEventListener, getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeEventListener, setEventListeners, start, stop
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.cometd.bayeux.server.ServerChannel.MessageListener
onMessage
-
-
-
-
Constructor Detail
-
OortPrimaryLong
public OortPrimaryLong(Oort oort, java.lang.String name, boolean primary)
-
OortPrimaryLong
public OortPrimaryLong(Oort oort, java.lang.String name, boolean primary, long initial)
- Parameters:
oort
- the oort this instance is associated toname
- the name of this serviceprimary
- whether this service lives on the "primary" nodeinitial
- the initial local value
-
-
Method Detail
-
getValue
protected long getValue()
- Returns:
- the local value, which makes sense only if called on the "primary" node.
-
get
public boolean get(OortPrimaryLong.Callback callback)
Retrieves the value from the "primary" node and then invokes the givencallback
with the result value.- Parameters:
callback
- the callback invoked when the result is available- Returns:
- whether the request could be forwarded to the "primary" node
-
addAndGet
public boolean addAndGet(long delta, OortPrimaryLong.Callback callback)
Adds the givendelta
and then invokes the givencallback
with the counter value after the addition. The counter value may be already out of date at the moment of the invocation of thecallback
.- Parameters:
delta
- the value to add, may be negativecallback
- the callback invoked when the result is available- Returns:
- whether the request could be forwarded to the "primary" node
- See Also:
getAndAdd(long, Callback)
-
getAndAdd
public boolean getAndAdd(long delta, OortPrimaryLong.Callback callback)
Adds the givendelta
and then invokes the givencallback
with the counter value before the addition.- Parameters:
delta
- the value to add, may be negativecallback
- the callback invoked when the result is available- Returns:
- whether the request could be forwarded to the "primary" node
- See Also:
addAndGet(long, Callback)
-
onForward
protected OortService.Result<java.lang.Long> onForward(OortService.Request request)
Description copied from class:OortService
Subclasses must implement this method, that runs on the owner node, to implement the action functionality.
The result to return is
OortService.Result.success(Object)
orOortService.Result.failure(Object)
if the implementation of this method was able to find the entity on which the action functionality was meant to be applied, orOortService.Result.ignore(Object)
if the entity was not found.- Specified by:
onForward
in classOortService<java.lang.Long,OortPrimaryLong.Context>
- Parameters:
request
- the request containing the parameter passed fromOortService.forward(String, Object, Object)
- Returns:
- the result containing the data that will be passed to
OortService.onForwardSucceeded(Object, Object)
-
onForwardSucceeded
protected void onForwardSucceeded(java.lang.Long result, OortPrimaryLong.Context context)
Description copied from class:OortService
Subclasses must implement this method, that runs on the requesting node, to complete the functionality after the action has been successfully run on the owner node.- Specified by:
onForwardSucceeded
in classOortService<java.lang.Long,OortPrimaryLong.Context>
- Parameters:
result
- the result of the actioncontext
- the opaque context fromOortService.forward(String, Object, Object)
-
onForwardFailed
protected void onForwardFailed(java.lang.Object failure, OortPrimaryLong.Context context)
Description copied from class:OortService
Subclasses must implement this method, that runs on the requesting node, to complete the functionality after the action failed on the owner node.- Specified by:
onForwardFailed
in classOortService<java.lang.Long,OortPrimaryLong.Context>
- Parameters:
failure
- the failure of the actioncontext
- the opaque context fromOortService.forward(String, Object, Object)
-
-