Class AeronCluster
- All Implemented Interfaces:
AutoCloseable
A client will attempt to open a session and then offer ingress messages which are replicated to clustered services for reliability. If the clustered service responds then response messages and events are sent via the egress stream.
Note: Instances of this class are not threadsafe.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classAllows for the async establishment of a cluster session.static final classConfiguration options for cluster client.static final classContext for cluster session and connection. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intLength of a session message header for cluster ingress or egress. -
Method Summary
Modifier and TypeMethodDescriptionstatic AeronCluster.AsyncConnectBegin an attempt at creating a connection which can be completed by callingAeronCluster.AsyncConnect.poll()until it returns the client, before complete it will return null.static AeronCluster.AsyncConnectBegin an attempt at creating a connection which can be completed by callingAeronCluster.AsyncConnect.poll()until it returns the client, before complete it will return null.voidclose()Close session and release associated resources.longCluster session id for the session that was opened as the result of a successful connect.static AeronClusterconnect()Connect to the cluster using default configuration.static AeronClusterConnect to the cluster providingAeronCluster.Contextfor configuration.context()Get the context used to launch this cluster client.voidcontrolledEgressListenerExtension(ControlledEgressListenerExtension listenerExtension) A ControlledEgressListener for extension schemas.intPoll theegressSubscription()for session messages which are dispatched toAeronCluster.Context.controlledEgressListener().voidegressListenerExtension(EgressListenerExtension listenerExtension) An EgressListener for extension schemas.Get the rawSubscriptionfor receiving from the cluster.Get the rawPublicationfor sending to the cluster.booleanisClosed()Is the client closed? The client can be closed by callingclose(), the cluster sending an event, or the client permanently losing a cluster connection.intGet the current leader member id for the cluster.longLeadership term identity for the cluster.longoffer(DirectBufferVector[] vectors) Non-blocking publish by gathering buffer vectors into a message.longoffer(DirectBuffer buffer, int offset, int length) Non-blocking publish of a partial buffer containing a message plus session header to a cluster.voidonNewLeader(long clusterSessionId, long leadershipTermId, int leaderMemberId, String ingressEndpoints) To be called when a new leader event is delivered.intPoll theegressSubscription()for session messages which are dispatched toAeronCluster.Context.egressListener().intPolls for client state changes.booleansendAdminRequestToTakeASnapshot(long correlationId) Sends an admin request to initiate a snapshot action in the cluster.booleanSend a keep alive message to the cluster to keep this session open.voidtrackIngressPublicationResult(long result) Updates the state of this client based on ingress publication result.longtryClaim(int length, BufferClaim bufferClaim) Try to claim a range in the publication log into which a message can be written with zero copy semantics.
-
Field Details
-
SESSION_HEADER_LENGTH
public static final int SESSION_HEADER_LENGTHLength of a session message header for cluster ingress or egress.- See Also:
-
-
Method Details
-
connect
Connect to the cluster using default configuration.- Returns:
- allocated cluster client if the connection is successful.
-
connect
Connect to the cluster providingAeronCluster.Contextfor configuration.- Parameters:
ctx- for configuration.- Returns:
- allocated cluster client if the connection is successful.
-
asyncConnect
Begin an attempt at creating a connection which can be completed by callingAeronCluster.AsyncConnect.poll()until it returns the client, before complete it will return null.- Returns:
- the
AeronCluster.AsyncConnectthat can be polled for completion.
-
asyncConnect
Begin an attempt at creating a connection which can be completed by callingAeronCluster.AsyncConnect.poll()until it returns the client, before complete it will return null.- Parameters:
ctx- for the cluster.- Returns:
- the
AeronCluster.AsyncConnectthat can be polled for completion.
-
egressListenerExtension
An EgressListener for extension schemas.- Parameters:
listenerExtension- listener extension.
-
controlledEgressListenerExtension
A ControlledEgressListener for extension schemas.- Parameters:
listenerExtension- listener extension.
-
close
public void close()Close session and release associated resources.- Specified by:
closein interfaceAutoCloseable
-
isClosed
public boolean isClosed()Is the client closed? The client can be closed by callingclose(), the cluster sending an event, or the client permanently losing a cluster connection.- Returns:
- true if closed otherwise false.
-
context
Get the context used to launch this cluster client.- Returns:
- the context used to launch this cluster client.
-
clusterSessionId
public long clusterSessionId()Cluster session id for the session that was opened as the result of a successful connect.- Returns:
- session id for the session that was opened as the result of a successful connect.
-
leadershipTermId
public long leadershipTermId()Leadership term identity for the cluster. Advances with changing leadership.- Returns:
- leadership term identity for the cluster.
-
leaderMemberId
public int leaderMemberId()Get the current leader member id for the cluster.- Returns:
- the current leader member id for the cluster.
-
ingressPublication
Get the rawPublicationfor sending to the cluster.This can be wrapped with a
IngressSessionDecoratorfor pre-pending the cluster session header to messages.SessionMessageHeaderEncodershould be used for raw access.Results of offering to this publication should be passed to
trackIngressPublicationResult(long).- Returns:
- the raw
Publicationfor connecting to the cluster.
-
egressSubscription
Get the rawSubscriptionfor receiving from the cluster.This can be wrapped with a
EgressAdapterfor dispatching events from the cluster.SessionMessageHeaderDecodershould be used for raw access.- Returns:
- the raw
Subscriptionfor receiving from the cluster.
-
tryClaim
Try to claim a range in the publication log into which a message can be written with zero copy semantics. Once the message has been written thenBufferClaim.commit()should be called thus making it available.On successful claim, the Cluster ingress header will be written to the start of the claimed buffer section. Clients MUST write into the claimed buffer region at offset +
SESSION_HEADER_LENGTH.final DirectBuffer srcBuffer = acquireMessage(); if (aeronCluster.tryClaim(length, bufferClaim) > 0L) { try { final MutableDirectBuffer buffer = bufferClaim.buffer(); final int offset = bufferClaim.offset(); // ensure that data is written at the correct offset buffer.putBytes(offset + AeronCluster.SESSION_HEADER_LENGTH, srcBuffer, 0, length); } finally { bufferClaim.commit(); } }- Parameters:
length- of the range to claim in bytes. The additional bytes for the session header will be added.bufferClaim- to be populated if the claim succeeds.- Returns:
- The new stream position, otherwise a negative error value as specified in
Publication.tryClaim(int, BufferClaim). - Throws:
IllegalArgumentException- if the length is greater thanPublication.maxPayloadLength().- See Also:
-
offer
Non-blocking publish of a partial buffer containing a message plus session header to a cluster.This version of the method will set the timestamp value in the header to zero.
- Parameters:
buffer- containing message.offset- offset in the buffer at which the encoded message begins.length- in bytes of the encoded message.- Returns:
- the same as
Publication.offer(DirectBuffer, int, int).
-
offer
Non-blocking publish by gathering buffer vectors into a message. The first vector will be replaced by the cluster session message header so must be left unused.- Parameters:
vectors- which make up the message.- Returns:
- the same as
Publication.offer(DirectBufferVector[]). - See Also:
-
sendKeepAlive
public boolean sendKeepAlive()Send a keep alive message to the cluster to keep this session open.Note: Sending keep-alive can fail during a leadership transition. The application should continue to call
pollEgress()to ensure a connection to the new leader is established.- Returns:
- true if successfully sent otherwise false if back pressured.
-
sendAdminRequestToTakeASnapshot
public boolean sendAdminRequestToTakeASnapshot(long correlationId) Sends an admin request to initiate a snapshot action in the cluster. This request requires elevated privileges.- Parameters:
correlationId- for the request.- Returns:
trueif the request was sent orfalseotherwise.- See Also:
-
pollEgress
public int pollEgress()Poll theegressSubscription()for session messages which are dispatched toAeronCluster.Context.egressListener(). Invoking this method, orcontrolledPollEgress(), frequently is important for detecting leadership changes in a cluster.Note: if
AeronCluster.Context.egressListener()is not set then aConfigurationExceptioncould result.- Returns:
- 0 if no work was available, or a positive number if work has been done, typically the number of fragments processed.
- See Also:
-
controlledPollEgress
public int controlledPollEgress()Poll theegressSubscription()for session messages which are dispatched toAeronCluster.Context.controlledEgressListener(). Invoking this method, orpollEgress(), frequently is important for detecting leadership changes in a cluster.Note: if
AeronCluster.Context.controlledEgressListener()is not set then aConfigurationExceptioncould result.- Returns:
- 0 if no work was available, a positive number if work has been done, typically the number of fragments processed.
- See Also:
-
pollStateChanges
public int pollStateChanges()Polls for client state changes. Needs to be called explicitly only by applications which useegressSubscription()directly instead of callingpollEgress()orcontrolledPollEgress().- Returns:
- 0 if state has not changed, a positive number otherwise.
-
onNewLeader
public void onNewLeader(long clusterSessionId, long leadershipTermId, int leaderMemberId, String ingressEndpoints) To be called when a new leader event is delivered. This method needs to be called when using theEgressAdapterorEgressPollerrather thanpollEgress()method.- Parameters:
clusterSessionId- which must matchclusterSessionId().leadershipTermId- that identifies the term for which the new leader has been elected.leaderMemberId- which has become the new leader.ingressEndpoints- comma separated list of cluster ingress endpoints to connect to with the leader first.
-
trackIngressPublicationResult
public void trackIngressPublicationResult(long result) Updates the state of this client based on ingress publication result. Should be called with everyofferandtryClaimresult wheningressPublication()is used directly. Methods of this class which send ingress messages call it automatically.- Parameters:
result- the result returned by the ingress publication.
-