use of io.aeron.Subscription in project Aeron by real-logic.
the class MultipleSubscribersWithFragmentAssembly method eventUnavailableImage.
/**
* This handler is called when image is unavailable.
*
* @param image that has gone inactive.
*/
public static void eventUnavailableImage(final Image image) {
final Subscription subscription = image.subscription();
System.out.format("inactive image on %s streamId %d sessionId %x%n", subscription.channel(), subscription.streamId(), image.sessionId());
}
use of io.aeron.Subscription in project Aeron by real-logic.
the class Pong method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
final Aeron.Context ctx = new Aeron.Context();
if (EMBEDDED_MEDIA_DRIVER) {
ctx.aeronDirectoryName(driver.aeronDirectoryName());
}
if (INFO_FLAG) {
ctx.availableImageHandler(SamplesUtil::printAvailableImage);
ctx.unavailableImageHandler(SamplesUtil::printUnavailableImage);
}
final IdleStrategy idleStrategy = new BusySpinIdleStrategy();
System.out.println("Subscribing Ping at " + PING_CHANNEL + " on stream id " + PING_STREAM_ID);
System.out.println("Publishing Pong at " + PONG_CHANNEL + " on stream id " + PONG_STREAM_ID);
System.out.println("Using exclusive publications " + EXCLUSIVE_PUBLICATIONS);
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> running.set(false));
try (Aeron aeron = Aeron.connect(ctx);
Subscription subscription = aeron.addSubscription(PING_CHANNEL, PING_STREAM_ID);
Publication publication = EXCLUSIVE_PUBLICATIONS ? aeron.addExclusivePublication(PONG_CHANNEL, PONG_STREAM_ID) : aeron.addPublication(PONG_CHANNEL, PONG_STREAM_ID)) {
final BufferClaim bufferClaim = new BufferClaim();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> pingHandler(bufferClaim, publication, buffer, offset, length, header);
while (running.get()) {
idleStrategy.idle(subscription.poll(fragmentHandler, FRAME_COUNT_LIMIT));
}
System.out.println("Shutting down...");
}
CloseHelper.close(driver);
}
use of io.aeron.Subscription in project Aeron by real-logic.
the class BasicSubscriber method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
System.out.println("Subscribing to " + CHANNEL + " on stream id " + STREAM_ID);
final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
final Aeron.Context ctx = new Aeron.Context().availableImageHandler(SamplesUtil::printAvailableImage).unavailableImageHandler(SamplesUtil::printUnavailableImage);
if (EMBEDDED_MEDIA_DRIVER) {
ctx.aeronDirectoryName(driver.aeronDirectoryName());
}
final FragmentHandler fragmentHandler = SamplesUtil.printAsciiMessage(STREAM_ID);
final AtomicBoolean running = new AtomicBoolean(true);
// Register a SIGINT handler for graceful shutdown.
SigInt.register(() -> running.set(false));
// clean up resources when this try block is finished
try (Aeron aeron = Aeron.connect(ctx);
Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
SamplesUtil.subscriberLoop(fragmentHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription);
System.out.println("Shutting down...");
}
CloseHelper.close(driver);
}
use of io.aeron.Subscription in project Aeron by real-logic.
the class EmbeddedPingPong method runPing.
private static void runPing(final Aeron aeron) throws InterruptedException {
System.out.println("Publishing Ping at " + PING_CHANNEL + " on stream id " + PING_STREAM_ID);
System.out.println("Subscribing Pong at " + PONG_CHANNEL + " on stream id " + PONG_STREAM_ID);
System.out.println("Message payload length of " + MESSAGE_LENGTH + " bytes");
System.out.println("Using exclusive publications: " + EXCLUSIVE_PUBLICATIONS);
final FragmentAssembler dataHandler = new FragmentAssembler(EmbeddedPingPong::pongHandler);
try (Subscription pongSubscription = aeron.addSubscription(PONG_CHANNEL, PONG_STREAM_ID, EmbeddedPingPong::availablePongImageHandler, null);
Publication pingPublication = EXCLUSIVE_PUBLICATIONS ? aeron.addExclusivePublication(PING_CHANNEL, PING_STREAM_ID) : aeron.addPublication(PING_CHANNEL, PING_STREAM_ID)) {
System.out.println("Waiting for new image from Pong...");
PONG_IMAGE_LATCH.await();
System.out.format("Warming up... %d iterations of %,d messages%n", WARMUP_NUMBER_OF_ITERATIONS, WARMUP_NUMBER_OF_MESSAGES);
for (int i = 0; i < WARMUP_NUMBER_OF_ITERATIONS; i++) {
roundTripMessages(dataHandler, pingPublication, pongSubscription, WARMUP_NUMBER_OF_MESSAGES);
Thread.yield();
}
Thread.sleep(100);
final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
do {
HISTOGRAM.reset();
System.out.format("Pinging %,d messages%n", NUMBER_OF_MESSAGES);
roundTripMessages(dataHandler, pingPublication, pongSubscription, NUMBER_OF_MESSAGES);
System.out.println("Histogram of RTT latencies in microseconds.");
HISTOGRAM.outputPercentileDistribution(System.out, 1000.0);
} while (barrier.await());
}
}
use of io.aeron.Subscription in project Aeron by real-logic.
the class ReplayedBasicSubscriber method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
System.out.println("Subscribing to " + CHANNEL + " on stream id " + STREAM_ID);
final FragmentHandler fragmentHandler = SamplesUtil.printAsciiMessage(STREAM_ID);
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> running.set(false));
// Create a unique response stream id so not to clash with other archive clients.
final AeronArchive.Context archiveCtx = new AeronArchive.Context().controlResponseStreamId(AeronArchive.Configuration.controlResponseStreamId() + 2);
try (AeronArchive archive = AeronArchive.connect(archiveCtx)) {
final long recordingId = findLatestRecording(archive);
final long position = 0L;
final long length = Long.MAX_VALUE;
final long sessionId = archive.startReplay(recordingId, position, length, CHANNEL, REPLAY_STREAM_ID);
final String channel = ChannelUri.addSessionId(CHANNEL, (int) sessionId);
try (Subscription subscription = archive.context().aeron().addSubscription(channel, REPLAY_STREAM_ID)) {
SamplesUtil.subscriberLoop(fragmentHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription);
System.out.println("Shutting down...");
}
}
}
Aggregations