Class Subscription
- All Implemented Interfaces:
AutoCloseable
Image for a stream of messages from publishers on
a given channel and streamId pair, i.e. a Publication. Images are aggregated under a
Subscription.
Subscriptions are created via an Aeron object, and received messages are delivered
to the FragmentHandler.
By default, fragmented messages are not reassembled before delivery. If an application must
receive whole messages, even if they were fragmented, then the Subscriber
should be created with a FragmentAssembler or a custom implementation.
It is an application's responsibility to poll(io.aeron.logbuffer.FragmentHandler, int) the Subscription for new messages.
Note:Subscriptions are not threadsafe and should not be shared between subscribers.
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddDestination(String endpointChannel) Add a destination manually to a multi-destination Subscription.longasyncAddDestination(String endpointChannel) Asynchronously add a destination manually to a multi-destination Subscription.longasyncRemoveDestination(String endpointChannel) Asynchronously remove a previously added destination from a multi-destination Subscription.Callback used to indicate when anImagebecomes available under thisSubscription.longblockPoll(BlockHandler blockHandler, int blockLengthLimit) Poll theImages under the subscription for available message fragments in blocks.channel()Media address for delivery to the channel.longGet the status of the media channel for this Subscription.intGet the counter used to represent the channel status for this Subscription.voidclose()Close the Subscription so that associatedImages can be released.intcontrolledPoll(ControlledFragmentHandler fragmentHandler, int fragmentLimit) Poll in a controlled manner theImages under the subscription for available message fragments.voidforEachImage(Consumer<Image> consumer) Iterate over theImages for this subscription.booleanHas this subscription currently noImages?imageAtIndex(int index) Get theImageat the given index from the images array.imageBySessionId(int sessionId) Return theImageassociated with the given sessionId.intCount ofImages associated to this subscription.images()booleanisClosed()Has this object been closed and should no longer be used?booleanIs this subscription connected by having at least one open publicationImage.Fetches the local socket addresses for this subscription.intpoll(FragmentHandler fragmentHandler, int fragmentLimit) Poll theImages under the subscription for available message fragments.longrawPoll(RawBlockHandler rawBlockHandler, int blockLengthLimit) Poll theImages under the subscription for available message fragments in blocks.longReturn the registration id used to register this Subscription with the media driver.voidremoveDestination(String endpointChannel) Remove a previously added destination from a multi-destination Subscription.Find the resolved endpoint for the channel.intstreamId()Stream identity for scoping within the channel media address.toString()Resolve channel endpoint and replace it with the port from the ephemeral range when 0 was provided.Callback used to indicate when anImagegoes unavailable under thisSubscription.
-
Method Details
-
channel
Media address for delivery to the channel.- Returns:
- Media address for delivery to the channel.
-
streamId
public int streamId()Stream identity for scoping within the channel media address.- Returns:
- Stream identity for scoping within the channel media address.
-
registrationId
public long registrationId()Return the registration id used to register this Subscription with the media driver.- Returns:
- registration id
-
availableImageHandler
Callback used to indicate when anImagebecomes available under thisSubscription.- Returns:
- callback used to indicate when an
Imagebecomes available under thisSubscription.
-
poll
Poll theImages under the subscription for available message fragments.Each fragment read will be a whole message if it is under MTU length. If larger than MTU then it will come as a series of fragments ordered within a session.
To assemble messages that span multiple fragments then use
FragmentAssembler.- Parameters:
fragmentHandler- callback for handling each message fragment as it is read.fragmentLimit- number of message fragments to limit when polling across multipleImages.- Returns:
- the number of fragments received.
-
controlledPoll
Poll in a controlled manner theImages under the subscription for available message fragments. Control is applied to message fragments in the stream. If more fragments can be read on another stream they will even if BREAK or ABORT is returned from the fragment handler.Each fragment read will be a whole message if it is under MTU length. If larger than MTU then it will come as a series of fragments ordered within a session.
To assemble messages that span multiple fragments then use
ControlledFragmentAssembler.- Parameters:
fragmentHandler- callback for handling each message fragment as it is read.fragmentLimit- number of message fragments to limit when polling across multipleImages.- Returns:
- the number of fragments received.
- See Also:
-
blockPoll
Poll theImages under the subscription for available message fragments in blocks.This method is useful for operations like bulk archiving and messaging indexing.
-
rawPoll
Poll theImages under the subscription for available message fragments in blocks.This method is useful for operations like bulk archiving a stream to file.
-
isConnected
public boolean isConnected()Is this subscription connected by having at least one open publicationImage.- Returns:
- true if this subscription connected by having at least one open publication
Image.
-
hasNoImages
public boolean hasNoImages()Has this subscription currently noImages?- Returns:
- has subscription currently no
Images?
-
imageCount
public int imageCount()Count ofImages associated to this subscription.- Returns:
- count of
Images associated to this subscription.
-
imageBySessionId
Return theImageassociated with the given sessionId.- Parameters:
sessionId- associated with theImage.- Returns:
- Image associated with the given sessionId or null if no Image exist.
-
imageAtIndex
Get theImageat the given index from the images array.- Parameters:
index- in the array.- Returns:
- image at given index.
- Throws:
ArrayIndexOutOfBoundsException- if the index is not valid.
-
images
-
forEachImage
Iterate over theImages for this subscription.- Parameters:
consumer- to handle eachImage.
-
close
public void close()Close the Subscription so that associatedImages can be released.This method is idempotent.
- Specified by:
closein interfaceAutoCloseable
-
isClosed
public boolean isClosed()Has this object been closed and should no longer be used?- Returns:
- true if it has been closed otherwise false.
-
channelStatus
public long channelStatus()Get the status of the media channel for this Subscription.The status will be
ChannelEndpointStatus.ERROREDif a socket exception occurs on setup andChannelEndpointStatus.ACTIVEif all is well.- Returns:
- status for the channel as one of the constants from
ChannelEndpointStatuswith it beingChannelEndpointStatus.NO_ID_ALLOCATEDif the subscription is closed. - See Also:
-
channelStatusId
public int channelStatusId()Get the counter used to represent the channel status for this Subscription.- Returns:
- the counter used to represent the channel status for this Subscription.
-
localSocketAddresses
Fetches the local socket addresses for this subscription. If the channel is notChannelEndpointStatus.ACTIVE, then this will return an empty list.The format is as follows:
IPv4:ip address:port
IPv6:[ip6 address]:port
This is to match the formatting used in the Aeron URI.- Returns:
Listof local socket addresses for this subscription.- See Also:
-
addDestination
Add a destination manually to a multi-destination Subscription.- Parameters:
endpointChannel- for the destination to add.
-
removeDestination
Remove a previously added destination from a multi-destination Subscription.- Parameters:
endpointChannel- for the destination to remove.
-
asyncAddDestination
Asynchronously add a destination manually to a multi-destination Subscription.Errors will be delivered asynchronously to the
Aeron.Context.errorHandler(). Completion can be tracked by passing the returned correlation id toAeron.isCommandActive(long).- Parameters:
endpointChannel- for the destination to add.- Returns:
- the correlationId for the command.
-
asyncRemoveDestination
Asynchronously remove a previously added destination from a multi-destination Subscription.Errors will be delivered asynchronously to the
Aeron.Context.errorHandler(). Completion can be tracked by passing the returned correlation id toAeron.isCommandActive(long).- Parameters:
endpointChannel- for the destination to remove.- Returns:
- the correlationId for the command.
-
tryResolveChannelEndpointPort
Resolve channel endpoint and replace it with the port from the ephemeral range when 0 was provided.- Returns:
- original channel URI string if it does not have an endpoint (IPC, response channel, MDS) or if endpoint
is not using an ephemeral port, channel URI where ephemeral port is resolved to the actual bind port or
nullif the channel is notChannelEndpointStatus.ACTIVE. - See Also:
-
resolvedEndpoint
Find the resolved endpoint for the channel. This may benullif MDS is used and no destination is yet added. The result will similar to taking the first element returned fromlocalSocketAddresses(). If more than one destination is added then the first found is returned.- Returns:
- resolved endpoint or
nullif not found. - See Also:
-
toString
-