R
- the result typeC
- the opaque context typepublic abstract class OortService<R,C> extends org.eclipse.jetty.util.component.AbstractLifeCycle implements ServerChannel.MessageListener
An OortService
allows applications to forward actions to Oort
nodes that own the entity onto which the action should be applied.
An OortService
builds on the concept introduced by OortObject
that the ownership of a particular entity belongs only to one node.
Any node can read the entity, but only the owner can create/modify/delete it.
In order to perform actions that modify the entity, a node has to know what is the node that owns the entity, and then forward the action to the owner node.
OortService
provides the facilities to forward the action to the
owner node and return the result of the action, or its failure.
OortService
s are usually created at application startup, but may
be created and destroyed on-the-fly.
In both cases, they must be started
to make them functional
and stopped
when they are no longer needed.
Usage of OortService
follows these steps:
forward(String, Object, Object)
, passing the Oort URL of
the owner node, the action data, and an opaque context
onForward(Request)
, which is
executed in the owner node. In this method the action
data is used to perform the action that modifies the entity, and
the result is returned (or an exception thrown in case of failure).
onForwardSucceeded(Object, Object)
(which is executed in the requesting node)
which provides as parameters the result of the action from the
second step and the opaque context from the first step.
onForwardFailed(Object, Object)
(which is executed in the requesting node)
which provides as parameters the failure from the second step and
the opaque context from the first step.
The failure object is the message string of a generic exception,
or the failure returned by OortService.Result.failure(Object)
.
The steps above do not change if the requesting node and the owner node are the same.
Modifier and Type | Class and Description |
---|---|
static class |
OortService.Request
Encapsulates a forwarded request.
|
static class |
OortService.Result<U>
Encapsulates the result of a forwarded request returned by
onForward(Request) . |
static class |
OortService.ServerContext
Utility context that stores the
ServerSession and the ServerMessage . |
org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener
ConfigurableServerChannel.ServerChannelListener.Weak
Modifier | Constructor and Description |
---|---|
protected |
OortService(Oort oort,
String name)
Creates an
OortService with the given name. |
Modifier and Type | Method and Description |
---|---|
protected void |
doStart() |
protected void |
doStop() |
protected boolean |
forward(String targetOortURL,
Object parameter,
C context)
Subclasses must call this method to forward the action to the owner node.
|
LocalSession |
getLocalSession() |
String |
getName() |
Oort |
getOort() |
long |
getTimeout() |
protected abstract OortService.Result<R> |
onForward(OortService.Request request)
Subclasses must implement this method, that runs on the owner node,
to implement the action functionality.
|
protected abstract void |
onForwardFailed(Object failure,
C 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 |
onForwardMessage(Map<String,Object> data,
boolean broadcast) |
protected abstract void |
onForwardSucceeded(R result,
C 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.
|
boolean |
onMessage(ServerSession from,
ServerChannel channel,
ServerMessage.Mutable message)
|
protected void |
onResultMessage(Map<String,Object> data) |
void |
setTimeout(long timeout) |
String |
toString() |
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, setStopTimeout, start, stop
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
onMessage
protected OortService(Oort oort, String name)
OortService
with the given name.oort
- the Oort where this service livesname
- the unique name across the cluster of this servicepublic Oort getOort()
public String getName()
public LocalSession getLocalSession()
public long getTimeout()
public void setTimeout(long timeout)
timeout
- the timeout, in milliseconds, for an action to return a resultprotected void doStart() throws Exception
doStart
in class org.eclipse.jetty.util.component.AbstractLifeCycle
Exception
protected void doStop() throws Exception
doStop
in class org.eclipse.jetty.util.component.AbstractLifeCycle
Exception
protected boolean forward(String targetOortURL, Object parameter, C context)
Subclasses must call this method to forward the action to the owner node.
If the targetOortURL
is null
, then the action is broadcast to all nodes.
Nodes that receive an action request that they can't fullfill because they don't own the
entity the action should be applied to must return OortService.Result.ignore(Object)
.
targetOortURL
- the owner node Oort URL, or null to broadcast the action to all nodesparameter
- the action parameter that will be passed to onForward(Request)
context
- the opaque context passed to onForwardSucceeded(Object, Object)
public boolean onMessage(ServerSession from, ServerChannel channel, ServerMessage.Mutable message)
ServerChannel.MessageListener
Blocking version of ServerChannel.MessageListener.onMessage(ServerSession, ServerChannel, ServerMessage.Mutable, Promise)
.
onMessage
in interface ServerChannel.MessageListener
from
- the session that publishes the messagechannel
- the channel the message is published tomessage
- the message to be publishedprotected abstract OortService.Result<R> onForward(OortService.Request request)
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)
or OortService.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, or OortService.Result.ignore(Object)
if the entity
was not found.
request
- the request containing the parameter passed from forward(String, Object, Object)
onForwardSucceeded(Object, Object)
protected abstract void onForwardSucceeded(R result, C context)
result
- the result of the actioncontext
- the opaque context from forward(String, Object, Object)
protected abstract void onForwardFailed(Object failure, C context)
failure
- the failure of the actioncontext
- the opaque context from forward(String, Object, Object)
public String toString()
toString
in class org.eclipse.jetty.util.component.AbstractLifeCycle
Copyright © 2008–2024 The CometD Project. All rights reserved.