public final class Subscription extends Object implements AutoCloseable
Image
for a stream of messages from publishers on
a given channel and streamId pair, i.e. a Publication
. Image
s are aggregated under a
Subscription
.
Subscription
s 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, whether or not 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.
Modifier and Type | Method and Description |
---|---|
void |
addDestination(String endpointChannel)
Add a destination manually to a multi-destination Subscription.
|
long |
asyncAddDestination(String endpointChannel)
Asynchronously add a destination manually to a multi-destination Subscription.
|
long |
asyncRemoveDestination(String endpointChannel)
Asynchronously remove a previously added destination from a multi-destination Subscription.
|
AvailableImageHandler |
availableImageHandler()
Callback used to indicate when an
Image becomes available under this Subscription . |
long |
blockPoll(BlockHandler blockHandler,
int blockLengthLimit)
Poll the
Image s under the subscription for available message fragments in blocks. |
String |
channel()
Media address for delivery to the channel.
|
long |
channelStatus()
Get the status of the media channel for this Subscription.
|
int |
channelStatusId()
Get the counter used to represent the channel status for this Subscription.
|
void |
close()
Close the Subscription so that associated
Image s can be released. |
int |
controlledPoll(ControlledFragmentHandler fragmentHandler,
int fragmentLimit)
Poll in a controlled manner the
Image s under the subscription for available message fragments. |
void |
forEachImage(Consumer<Image> consumer)
Iterate over the
Image s for this subscription. |
boolean |
hasNoImages()
Has this subscription currently no
Image s? |
Image |
imageAtIndex(int index)
Get the
Image at the given index from the images array. |
Image |
imageBySessionId(int sessionId)
Return the
Image associated with the given sessionId. |
int |
imageCount()
Count of
Image s associated to this subscription. |
List<Image> |
images()
|
boolean |
isClosed()
Has this object been closed and should no longer be used?
|
boolean |
isConnected()
Is this subscription connected by having at least one open publication
Image . |
List<String> |
localSocketAddresses()
Fetches the local socket addresses for this subscription.
|
int |
poll(FragmentHandler fragmentHandler,
int fragmentLimit)
Poll the
Image s under the subscription for available message fragments. |
long |
rawPoll(RawBlockHandler rawBlockHandler,
int blockLengthLimit)
Poll the
Image s under the subscription for available message fragments in blocks. |
long |
registrationId()
Return the registration id used to register this Subscription with the media driver.
|
void |
removeDestination(String endpointChannel)
Remove a previously added destination from a multi-destination Subscription.
|
String |
resolvedEndpoint()
Find the resolved endpoint for the channel.
|
int |
streamId()
Stream identity for scoping within the channel media address.
|
String |
toString() |
String |
tryResolveChannelEndpointPort()
Resolve channel endpoint and replace it with the port from the ephemeral range when 0 was provided.
|
UnavailableImageHandler |
unavailableImageHandler()
Callback used to indicate when an
Image goes unavailable under this Subscription . |
public String channel()
public int streamId()
public long registrationId()
public AvailableImageHandler availableImageHandler()
Image
becomes available under this Subscription
.Image
becomes available under this Subscription
.public UnavailableImageHandler unavailableImageHandler()
Image
goes unavailable under this Subscription
.Image
goes unavailable under this Subscription
.public int poll(FragmentHandler fragmentHandler, int fragmentLimit)
Image
s 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
.
fragmentHandler
- callback for handling each message fragment as it is read.fragmentLimit
- number of message fragments to limit when polling across multiple Image
s.public int controlledPoll(ControlledFragmentHandler fragmentHandler, int fragmentLimit)
Image
s under the subscription for available message fragments.
Control is applied to 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
.
fragmentHandler
- callback for handling each message fragment as it is read.fragmentLimit
- number of message fragments to limit when polling across multiple Image
s.ControlledFragmentHandler
public long blockPoll(BlockHandler blockHandler, int blockLengthLimit)
Image
s under the subscription for available message fragments in blocks.
This method is useful for operations like bulk archiving and messaging indexing.
public long rawPoll(RawBlockHandler rawBlockHandler, int blockLengthLimit)
Image
s under the subscription for available message fragments in blocks.
This method is useful for operations like bulk archiving a stream to file.
public boolean isConnected()
Image
.Image
.public boolean hasNoImages()
Image
s?Image
s?public int imageCount()
Image
s associated to this subscription.Image
s associated to this subscription.public Image imageBySessionId(int sessionId)
Image
associated with the given sessionId.sessionId
- associated with the Image
.public Image imageAtIndex(int index)
Image
at the given index from the images array.index
- in the arrayArrayIndexOutOfBoundsException
- if the index is not valid.public void forEachImage(Consumer<Image> consumer)
Image
s for this subscription.consumer
- to handle each Image
.public void close()
Image
s can be released.
This method is idempotent.
close
in interface AutoCloseable
public boolean isClosed()
public long channelStatus()
The status will be ChannelEndpointStatus.ERRORED
if a socket exception occurs on setup
and ChannelEndpointStatus.ACTIVE
if all is well.
ChannelEndpointStatus
with it being
ChannelEndpointStatus.NO_ID_ALLOCATED
if the subscription is closed.ChannelEndpointStatus
public int channelStatusId()
public List<String> localSocketAddresses()
ChannelEndpointStatus.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.
List
of local socket addresses for this subscription.channelStatus()
public void addDestination(String endpointChannel)
endpointChannel
- for the destination to add.public void removeDestination(String endpointChannel)
endpointChannel
- for the destination to remove.public long asyncAddDestination(String endpointChannel)
Errors will be delivered asynchronously to the Aeron.Context.errorHandler()
. Completion can be
tracked by passing the returned correlation id to Aeron.isCommandActive(long)
.
endpointChannel
- for the destination to add.public long asyncRemoveDestination(String endpointChannel)
Errors will be delivered asynchronously to the Aeron.Context.errorHandler()
. Completion can be
tracked by passing the returned correlation id to Aeron.isCommandActive(long)
.
endpointChannel
- for the destination to remove.public String tryResolveChannelEndpointPort()
localSocketAddresses()
then the original
channel()
is returned.
If the channel is not ChannelEndpointStatus.ACTIVE
, then null
will be returned.
channelStatus()
,
localSocketAddresses()
public String resolvedEndpoint()
localSocketAddresses()
. If more than
one destination is added then the first found is returned.
If the channel is not ChannelEndpointStatus.ACTIVE
, then null
will be returned.
channelStatus()
,
localSocketAddresses()
Copyright © 2014-2021 Real Logic Limited. All Rights Reserved.