Class Aeron
- All Implemented Interfaces:
AutoCloseable
Publications and Subscriptions.
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 classConfiguration options for theAeronclient.static classProvides a means to override configuration for anAeronclient via theconnect(Aeron.Context)method and its overloads. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intUsed to represent a null value for when some value is not yet set. -
Method Summary
Modifier and TypeMethodDescriptionlongAdd a handler to the list be called whenCounters become available.longaddCloseHandler(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 aCounterfor 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 aCounterfor it.addExclusivePublication(String channel, int streamId) Add anExclusivePublicationfor publishing messages to subscribers from a single thread.addPublication(String channel, int streamId) Add aPublicationfor publishing messages to subscribers.addStaticCounter(int typeId, String label, long registrationId) Allocates or returns an existing static counter instance using specifiedtypeIdandregistrationIdpair.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 specifiedtypeIdandregistrationIdpair.addSubscription(String channel, int streamId) Add a newSubscriptionfor subscribing to messages from publishers.addSubscription(String channel, int streamId, AvailableImageHandler availableImageHandler, UnavailableImageHandler unavailableImageHandler) Add a newSubscriptionfor subscribing to messages from publishers.longAdd a handler to the list be called whenCounters become unavailable.longasyncAddCounter(int typeId, String label) Asynchronously allocate a counter on the media driver.longasyncAddCounter(int typeId, DirectBuffer keyBuffer, int keyOffset, int keyLength, DirectBuffer labelBuffer, int labelOffset, int labelLength) Asynchronously allocate a counter on the media driver.longasyncAddExclusivePublication(String channel, int streamId) Asynchronously add aPublicationfor publishing messages to subscribers from a single thread.longasyncAddPublication(String channel, int streamId) Asynchronously add aPublicationfor publishing messages to subscribers.longasyncAddStaticCounter(int typeId, String label, long registrationId) Asynchronously allocates or returns an existing static counter instance using specifiedtypeIdandregistrationIdpair.longasyncAddStaticCounter(int typeId, DirectBuffer keyBuffer, int keyOffset, int keyLength, DirectBuffer labelBuffer, int labelOffset, int labelLength, long registrationId) Asynchronously allocates or returns an existing static counter instance using specifiedtypeIdandregistrationIdpair.longasyncAddSubscription(String channel, int streamId) Add a newSubscriptionfor subscribing to messages from publishers.longasyncAddSubscription(String channel, int streamId, AvailableImageHandler availableImageHandler, UnavailableImageHandler unavailableImageHandler) Add a newSubscriptionfor subscribing to messages from publishers.voidasyncRemoveCounter(long registrationId) Asynchronously remove aCounter.voidasyncRemovePublication(long registrationId) Asynchronously remove aPublication.voidasyncRemoveSubscription(long registrationId) Asynchronously remove aSubscription.longclientId()Get the client identity that has been allocated for communicating with the media driver.voidclose()Clean up and release all Aeron client resources and shutdown conductor thread if not usingAeron.Context.useConductorAgentInvoker(boolean).Get theAgentInvokerfor the client conductor.static Aeronconnect()Create an Aeron instance and connect to the media driver with a defaultAeron.Context.static Aeronconnect(Aeron.Context ctx) Create an Aeron instance and connect to the media driver.context()Get theAeron.Contextthat is used by this client.Get theCountersReaderfor the Aeron media driver counters.getCounter(long correlationId) Get aCounterthat was created asynchronously.getExclusivePublication(long registrationId) Get a single threadedPublicationfor publishing messages to subscribers.getPublication(long registrationId) Get aPublicationfor publishing messages to subscribers.getSubscription(long registrationId) Get aSubscriptionfor subscribing to messages from publishers.booleanDoes the client have any active asynchronous commands?booleanisClosed()Has the client been closed? If not then the CnC file may not be unmapped.booleanisCommandActive(long correlationId) Is the command still active for a given correlation id.longGenerate the next correlation id that is unique for the connected media driver.intnextSessionId(int streamId) Get next available session id from the media driver.voidprintCounters(PrintStream out) Print out the values fromcountersReader()which can be useful for debugging.booleanremoveAvailableCounterHandler(long registrationId) Remove a previously added handler to the list be called whenCounters become available.booleanDeprecated.booleanremoveCloseHandler(long registrationId) Remove a previously added handler to the list be called when the Aeron client is closed.booleanremoveCloseHandler(Runnable handler) Deprecated.please useremoveCloseHandler(long).booleanremoveUnavailableCounterHandler(long registrationId) Remove a previously added handler to the list be called whenCounters become unavailable.booleanDeprecated.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
Aeroninstance 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
Aeroninstance 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.Contextthat is used by this client.- Returns:
- the
Aeron.Contextthat 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 theAgentInvokerfor the client conductor.- Returns:
- the
AgentInvokerfor 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
Publications,Subscriptions, andCounters 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
Publications,Subscriptions, andCounters created from this client. To check for the command being acknowledged by the driver- Specified by:
closein interfaceAutoCloseable
-
addPublication
Add aPublicationfor 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 anExclusivePublicationfor 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 aPublicationfor 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 aPublicationfor 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- of the publication to be removed.- See Also:
-
getPublication
Get aPublicationfor publishing messages to subscribers. The publication returned is threadsafe.- Parameters:
registrationId- returned fromasyncAddPublication(String, int).- Returns:
- a new
ConcurrentPublicationwhen available otherwise null. - See Also:
-
getExclusivePublication
Get a single threadedPublicationfor publishing messages to subscribers.- Parameters:
registrationId- returned fromasyncAddExclusivePublication(String, int).- Returns:
- a new
ExclusivePublicationwhen available otherwise null. - See Also:
-
addSubscription
Add a newSubscriptionfor subscribing to messages from publishers.The method will set up the
Subscriptionto 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
Subscriptionfor the channel and streamId pair.
-
asyncAddSubscription
Add a newSubscriptionfor 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 aSubscriptionfor subscribing to messages from publishers.- Parameters:
registrationId- returned fromasyncAddSubscription(String, int, AvailableImageHandler, UnavailableImageHandler)orasyncAddSubscription(String, int)- Returns:
- a new
Subscriptionwhen 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.
-
nextSessionId
public int nextSessionId(int streamId) Get next available session id from the media driver. The session id will be unique for the connected media driver and givenstreamId.If media driver's version is 1.49.0 or higher, then the session id is returned by the media driver. Otherwise, a random session id is generated.
- Parameters:
streamId- for which a new session id is requested. Media driver only checks for session clashes at the stream level.- Returns:
- next available session id that is unique for the media driver and given
streamId. - Since:
- 1.49.0
-
countersReader
Get theCountersReaderfor the Aeron media driver counters.- Returns:
- new
CountersReaderfor 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 aCounterfor it.The counter should be freed by calling
Counter.close().The typeId should be 1000 or greater. Values lower than that are reserved for use by Aeron.
- 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 aCounterfor it.The counter should be freed by calling
Counter.close().The typeId should be 1000 or greater. Values lower than that are reserved for use by Aeron.
- 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 specifiedtypeIdandregistrationIdpair. Such a counter cannot be deleted and its lifecycle is decoupled from thisAeroninstance, 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.The typeId should be 1000 or greater. Values lower than that are reserved for use by Aeron.
- 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 specifiedtypeIdandregistrationIdpair. Such a counter cannot be deleted and its lifecycle is decoupled from thisAeroninstance, 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.The typeId should be 1000 or greater. Values lower than that are reserved for use by Aeron.
- 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:
-
asyncAddCounter
Asynchronously allocate a counter on the media driver.The typeId should be 1000 or greater. Values lower than that are reserved for use by Aeron.
- Parameters:
typeId- for the counter.label- for the counter. It should be US-ASCII.- Returns:
- the registration id of the counter which can be used to get it by calling
getCounter(long)method. - Since:
- 1.49.0
- See Also:
-
asyncAddCounter
public long asyncAddCounter(int typeId, DirectBuffer keyBuffer, int keyOffset, int keyLength, DirectBuffer labelBuffer, int labelOffset, int labelLength) Asynchronously allocate a counter on the media driver.The typeId should be 1000 or greater. Values lower than that are reserved for use by Aeron.
- 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 registration id of the counter which can be used to get it by calling
getCounter(long)method. - Since:
- 1.49.0
- See Also:
-
asyncAddStaticCounter
Asynchronously allocates or returns an existing static counter instance using specifiedtypeIdandregistrationIdpair. Such a counter cannot be deleted and its lifecycle is decoupled from thisAeroninstance, 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.The typeId should be 1000 or greater. Values lower than that are reserved for use by Aeron.
- 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 correlation id of the command which can be used to get the counter by calling
getCounter(long)method. - Since:
- 1.49.0
- See Also:
-
asyncAddStaticCounter
public long asyncAddStaticCounter(int typeId, DirectBuffer keyBuffer, int keyOffset, int keyLength, DirectBuffer labelBuffer, int labelOffset, int labelLength, long registrationId) Asynchronously allocates or returns an existing static counter instance using specifiedtypeIdandregistrationIdpair. Such a counter cannot be deleted and its lifecycle is decoupled from thisAeroninstance, 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.The typeId should be 1000 or greater. Values lower than that are reserved for use by Aeron.
- 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 correlation id of the command which can be used to get the counter by calling
getCounter(long)method. - Since:
- 1.49.0
- See Also:
-
getCounter
Get aCounterthat was created asynchronously.- Parameters:
correlationId- returned from one of the async methods:asyncAddCounter(int, String),asyncAddCounter(int, DirectBuffer, int, int, DirectBuffer, int, int),asyncAddStaticCounter(int, String, long)orasyncAddStaticCounter(int, DirectBuffer, int, int, DirectBuffer, int, int, long).- Returns:
- a new
Counterwhen available, otherwisenull. - Since:
- 1.49.0
- See Also:
-
asyncRemoveCounter
public void asyncRemoveCounter(long registrationId) Asynchronously remove aCounter.- Parameters:
registrationId- of the counter to be removed.- Since:
- 1.49.0
- See Also:
-
addAvailableCounterHandler
Add a handler to the list be called whenCounters become available.- Parameters:
handler- to be called whenCounters 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 whenCounters 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 whenCounters 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).