Class Aeron
- All Implemented Interfaces:
AutoCloseable
Publication
s and Subscription
s.
Use an Aeron.Context
to configure the Aeron object.
A client application requires only one Aeron object per Media Driver.
Note: If Aeron.Context.errorHandler(ErrorHandler)
is not set and a DriverTimeoutException
occurs then the process will face the wrath of System.exit(int)
.
See Aeron.Configuration.DEFAULT_ERROR_HANDLER
.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Configuration options for theAeron
client.static class
Provides a means to override configuration for anAeron
client via theconnect(Aeron.Context)
method and its overloads. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Used to represent a null value for when some value is not yet set. -
Method Summary
Modifier and TypeMethodDescriptionlong
Add a handler to the list be called whenCounter
s become available.long
addCloseHandler
(Runnable handler) Add a handler to the list be called when the Aeron client is closed.addCounter
(int typeId, String label) Allocate a counter on the media driver and return aCounter
for it.addCounter
(int typeId, DirectBuffer keyBuffer, int keyOffset, int keyLength, DirectBuffer labelBuffer, int labelOffset, int labelLength) Allocate a counter on the media driver and return aCounter
for it.addExclusivePublication
(String channel, int streamId) Add anExclusivePublication
for publishing messages to subscribers from a single thread.addPublication
(String channel, int streamId) Add aPublication
for publishing messages to subscribers.addStaticCounter
(int typeId, String label, long registrationId) Allocates or returns an existing static counter instance using specifiedtypeId
andregistrationId
pair.addStaticCounter
(int typeId, DirectBuffer keyBuffer, int keyOffset, int keyLength, DirectBuffer labelBuffer, int labelOffset, int labelLength, long registrationId) Allocates or returns an existing static counter instance using specifiedtypeId
andregistrationId
pair.addSubscription
(String channel, int streamId) Add a newSubscription
for subscribing to messages from publishers.addSubscription
(String channel, int streamId, AvailableImageHandler availableImageHandler, UnavailableImageHandler unavailableImageHandler) Add a newSubscription
for subscribing to messages from publishers.long
Add a handler to the list be called whenCounter
s become unavailable.long
asyncAddExclusivePublication
(String channel, int streamId) Asynchronously add aPublication
for publishing messages to subscribers from a single thread.long
asyncAddPublication
(String channel, int streamId) Asynchronously add aPublication
for publishing messages to subscribers.long
asyncAddSubscription
(String channel, int streamId) Add a newSubscription
for subscribing to messages from publishers.long
asyncAddSubscription
(String channel, int streamId, AvailableImageHandler availableImageHandler, UnavailableImageHandler unavailableImageHandler) Add a newSubscription
for subscribing to messages from publishers.void
asyncRemovePublication
(long registrationId) Asynchronously remove aPublication
.void
asyncRemoveSubscription
(long registrationId) Asynchronously remove aSubscription
.long
clientId()
Get the client identity that has been allocated for communicating with the media driver.void
close()
Clean up and release all Aeron client resources and shutdown conductor thread if not usingAeron.Context.useConductorAgentInvoker(boolean)
.Get theAgentInvoker
for the client conductor.static Aeron
connect()
Create an Aeron instance and connect to the media driver with a defaultAeron.Context
.static Aeron
connect
(Aeron.Context ctx) Create an Aeron instance and connect to the media driver.context()
Get theAeron.Context
that is used by this client.Get theCountersReader
for the Aeron media driver counters.getExclusivePublication
(long registrationId) Get a single threadedPublication
for publishing messages to subscribers.getPublication
(long registrationId) Get aPublication
for publishing messages to subscribers.getSubscription
(long registrationId) Get aSubscription
for subscribing to messages from publishers.boolean
Does the client have any active asynchronous commands?boolean
isClosed()
Has the client been closed? If not then the CnC file may not be unmapped.boolean
isCommandActive
(long correlationId) Is the command still active for a given correlation id.long
Generate the next correlation id that is unique for the connected Media Driver.void
printCounters
(PrintStream out) Print out the values fromcountersReader()
which can be useful for debugging.boolean
removeAvailableCounterHandler
(long registrationId) Remove a previously added handler to the list be called whenCounter
s become available.boolean
Deprecated.boolean
removeCloseHandler
(long registrationId) Remove a previously added handler to the list be called when the Aeron client is closed.boolean
removeCloseHandler
(Runnable handler) Deprecated.please useremoveCloseHandler(long)
.boolean
removeUnavailableCounterHandler
(long registrationId) Remove a previously added handler to the list be called whenCounter
s become unavailable.boolean
Deprecated.please useremoveUnavailableCounterHandler(long)
.toString()
-
Field Details
-
NULL_VALUE
public static final int NULL_VALUEUsed to represent a null value for when some value is not yet set.- See Also:
-
-
Method Details
-
connect
Create an Aeron instance and connect to the media driver with a defaultAeron.Context
.Threads required for interacting with the media driver are created and managed within the Aeron instance.
- Returns:
- the new
Aeron
instance connected to the Media Driver.
-
connect
Create an Aeron instance and connect to the media driver.Threads required for interacting with the media driver are created and managed within the Aeron instance.
If an exception occurs while trying to establish a connection then the
Aeron.Context.close()
method will be called on the passed context.- Parameters:
ctx
- for configuration of the client.- Returns:
- the new
Aeron
instance connected to the Media Driver.
-
printCounters
Print out the values fromcountersReader()
which can be useful for debugging.- Parameters:
out
- to where the counters get printed.
-
isClosed
public boolean isClosed()Has the client been closed? If not then the CnC file may not be unmapped.- Returns:
- true if the client has been explicitly closed otherwise false.
-
context
Get theAeron.Context
that is used by this client.- Returns:
- the
Aeron.Context
that is use by this client.
-
clientId
public long clientId()Get the client identity that has been allocated for communicating with the media driver.- Returns:
- the client identity that has been allocated for communicating with the media driver.
-
conductorAgentInvoker
Get theAgentInvoker
for the client conductor.- Returns:
- the
AgentInvoker
for the client conductor.
-
isCommandActive
public boolean isCommandActive(long correlationId) Is the command still active for a given correlation id.- Parameters:
correlationId
- to check if it is still active.- Returns:
- true in the command is still in active processing or false if completed successfully or errored.
- See Also:
-
hasActiveCommands
public boolean hasActiveCommands()Does the client have any active asynchronous commands?When close operations are performed on
Publication
s,Subscription
s, andCounter
s the commands are sent asynchronously to the driver. The client tracks active commands in case errors need to be reported. If you wish to wait for acknowledgement of close operations then wait for this method to return false.- Returns:
- true if any commands are currently active otherwise false.
-
close
public void close()Clean up and release all Aeron client resources and shutdown conductor thread if not usingAeron.Context.useConductorAgentInvoker(boolean)
.This will close all currently open
Publication
s,Subscription
s, andCounter
s created from this client. To check for the command being acknowledged by the driver- Specified by:
close
in interfaceAutoCloseable
-
addPublication
Add aPublication
for publishing messages to subscribers. The publication returned is threadsafe.- Parameters:
channel
- for sending the messages known to the media layer.streamId
- within the channel scope.- Returns:
- a new
ConcurrentPublication
.
-
addExclusivePublication
Add anExclusivePublication
for publishing messages to subscribers from a single thread.- Parameters:
channel
- for sending the messages known to the media layer.streamId
- within the channel scope.- Returns:
- a new
ExclusivePublication
.
-
asyncAddPublication
Asynchronously add aPublication
for publishing messages to subscribers. The added publication returned is threadsafe.- Parameters:
channel
- for sending the messages known to the media layer.streamId
- within the channel scope.- Returns:
- the registration id of the publication which can be used to get the added publication.
- See Also:
-
asyncAddExclusivePublication
Asynchronously add aPublication
for publishing messages to subscribers from a single thread.- Parameters:
channel
- for sending the messages known to the media layer.streamId
- within the channel scope.- Returns:
- the registration id of the publication which can be used to get the added exclusive publication.
- See Also:
-
asyncRemovePublication
public void asyncRemovePublication(long registrationId) Asynchronously remove aPublication
.- Parameters:
registrationId
- to be of the publication removed.- See Also:
-
getPublication
Get aPublication
for publishing messages to subscribers. The publication returned is threadsafe.- Parameters:
registrationId
- returned fromasyncAddPublication(String, int)
.- Returns:
- a new
ConcurrentPublication
when available otherwise null. - See Also:
-
getExclusivePublication
Get a single threadedPublication
for publishing messages to subscribers.- Parameters:
registrationId
- returned fromasyncAddExclusivePublication(String, int)
.- Returns:
- a new
ExclusivePublication
when available otherwise null. - See Also:
-
addSubscription
Add a newSubscription
for subscribing to messages from publishers.The method will set up the
Subscription
to use theAeron.Context.availableImageHandler(AvailableImageHandler)
andAeron.Context.unavailableImageHandler(UnavailableImageHandler)
from theAeron.Context
.- Parameters:
channel
- for receiving the messages known to the media layer.streamId
- within the channel scope.- Returns:
- the
Subscription
for the channel and streamId pair.
-
asyncAddSubscription
Add a newSubscription
for subscribing to messages from publishers.- Parameters:
channel
- for receiving the messages known to the media layer.streamId
- within the channel scope.- Returns:
- the registration id of the subscription which can be used to get the added subscription.
- See Also:
-
asyncRemoveSubscription
public void asyncRemoveSubscription(long registrationId) Asynchronously remove aSubscription
.- Parameters:
registrationId
- to be of the subscription removed.- See Also:
-
getSubscription
Get aSubscription
for subscribing to messages from publishers.- Parameters:
registrationId
- returned fromasyncAddSubscription(String, int, AvailableImageHandler, UnavailableImageHandler)
orasyncAddSubscription(String, int)
- Returns:
- a new
Subscription
when available otherwise null. - See Also:
-
nextCorrelationId
public long nextCorrelationId()Generate the next correlation id that is unique for the connected Media Driver.This is useful generating correlation identifiers for pairing requests with responses in a clients own application protocol.
This method is thread safe and will work across processes that all use the same media driver.
- Returns:
- next correlation id that is unique for the Media Driver.
-
countersReader
Get theCountersReader
for the Aeron media driver counters.- Returns:
- new
CountersReader
for the Aeron media driver in use.
-
addCounter
public Counter addCounter(int typeId, DirectBuffer keyBuffer, int keyOffset, int keyLength, DirectBuffer labelBuffer, int labelOffset, int labelLength) Allocate a counter on the media driver and return aCounter
for it.The counter should be freed by calling
Counter.close()
.- Parameters:
typeId
- for the counter.keyBuffer
- containing the optional key for the counter.keyOffset
- within the keyBuffer at which the key begins.keyLength
- of the key in the keyBuffer.labelBuffer
- containing the mandatory label for the counter. The label should not be length prefixed.labelOffset
- within the labelBuffer at which the label begins.labelLength
- of the label in the labelBuffer.- Returns:
- the newly allocated counter.
- See Also:
-
addCounter
Allocate a counter on the media driver and return aCounter
for it.The counter should be freed by calling
Counter.close()
.- Parameters:
typeId
- for the counter.label
- for the counter. It should be US-ASCII.- Returns:
- the newly allocated counter.
- See Also:
-
addStaticCounter
public Counter addStaticCounter(int typeId, DirectBuffer keyBuffer, int keyOffset, int keyLength, DirectBuffer labelBuffer, int labelOffset, int labelLength, long registrationId) Allocates or returns an existing static counter instance using specifiedtypeId
andregistrationId
pair. Such counter cannot be deleted and its lifecycle is decoupled from thisAeron
instance, i.e. won't be closed when this instance is closed or times out.Note: calling
Counter.close()
will only close the counter instance itself but will not free the counter in the CnC file.- Parameters:
typeId
- for the counter.keyBuffer
- containing the optional key for the counter.keyOffset
- within the keyBuffer at which the key begins.keyLength
- of the key in the keyBuffer.labelBuffer
- containing the mandatory label for the counter. The label should not be length prefixed.labelOffset
- within the labelBuffer at which the label begins.labelLength
- of the label in the labelBuffer.registrationId
- that uniquely identifies the static counter for a giventypeId
.- Returns:
- the static counter instance.
- Since:
- 1.45.0
- See Also:
-
addStaticCounter
Allocates or returns an existing static counter instance using specifiedtypeId
andregistrationId
pair. Such counter cannot be deleted and its lifecycle is decoupled from thisAeron
instance, i.e. won't be closed when this instance is closed or times out.Note: calling
Counter.close()
will only close the counter instance itself but will not free the counter in the CnC file.- Parameters:
typeId
- for the counter.label
- for the counter. It should be US-ASCII.registrationId
- that uniquely identifies the static counter for a giventypeId
.- Returns:
- the static counter.
- Since:
- 1.45.0
- See Also:
-
addAvailableCounterHandler
Add a handler to the list be called whenCounter
s become available.- Parameters:
handler
- to be called whenCounter
s become available.- Returns:
- registration id for the handler which can be used to remove it.
-
removeAvailableCounterHandler
public boolean removeAvailableCounterHandler(long registrationId) Remove a previously added handler to the list be called whenCounter
s become available.- Parameters:
registrationId
- to be removed which was returned from add method.- Returns:
- true if found and removed otherwise false.
-
removeAvailableCounterHandler
Deprecated.please useremoveAvailableCounterHandler(long)
.Remove a previously added handler to the list be called whenCounter
s become available.- Parameters:
handler
- to be removed.- Returns:
- true if found and removed otherwise false.
-
addCloseHandler
Add a handler to the list be called when the Aeron client is closed.- Parameters:
handler
- to be called when the Aeron client is closed.- Returns:
- registration id for the handler which can be used to remove it.
-
removeCloseHandler
public boolean removeCloseHandler(long registrationId) Remove a previously added handler to the list be called when the Aeron client is closed.- Parameters:
registrationId
- of the handler from when it was added.- Returns:
- true if found and removed otherwise false.
-
removeCloseHandler
Deprecated.please useremoveCloseHandler(long)
.Remove a previously added handler to the list be called when the Aeron client is closed.- Parameters:
handler
- to be removed.- Returns:
- true if found and removed otherwise false.
-
toString
-
removeAvailableCounterHandler(long)
.