Skip to content

How to block while awaiting publication connection

Problem

You want to open an Aeron publication, but first want to ensure the publication is connected before continuing with another task.

Solution

Poll Publication.isConnected() after creating the publication. Idle between polls to allow time for the Aeron Publication to be connected.

Sample

1
2
3
4
5
6
publication = aeron.addPublication(channel, stream);

while (!publication.isConnected())
{
    idleStrategy().idle();
}

Discussion

Creating publications in Aeron is a non-blocking operation, and as a result, the returned publication might not be ready before you would like to offer.

To add safety to the above sample, use an EpochClock or similar to add timeout functionality by setting a maximum connected time before starting the while loop, and breaking out if the connect does not happen before timeout.

See Also