use of io.aeron.Subscription in project aeron by real-logic.
the class RateSubscriber method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
* @throws InterruptedException if the task is interrupted
* @throws ExecutionException if the {@link Future} has an error.
*/
public static void main(final String[] args) throws InterruptedException, ExecutionException {
System.out.println("Subscribing to " + CHANNEL + " on stream id " + STREAM_ID);
final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
final ExecutorService executor = Executors.newFixedThreadPool(1);
final Aeron.Context ctx = new Aeron.Context().availableImageHandler(SamplesUtil::printAvailableImage).unavailableImageHandler(SamplesUtil::printUnavailableImage);
if (EMBEDDED_MEDIA_DRIVER) {
ctx.aeronDirectoryName(driver.aeronDirectoryName());
}
final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), SamplesUtil::printRate);
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> {
reporter.halt();
running.set(false);
});
try (Aeron aeron = Aeron.connect(ctx);
Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
final Future<?> future = executor.submit(() -> SamplesUtil.subscriberLoop(rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));
reporter.run();
System.out.println("Shutting down...");
future.get();
}
executor.shutdown();
if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
System.out.println("Warning: not all tasks completed promptly");
}
CloseHelper.close(driver);
}
use of io.aeron.Subscription in project aeron by real-logic.
the class EmbeddedThroughput method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
* @throws InterruptedException if the thread is interrupted during linger.
*/
public static void main(final String[] args) throws InterruptedException {
loadPropertiesFiles(args);
final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedThroughput::printRate);
final ExecutorService executor = Executors.newFixedThreadPool(2);
final AtomicBoolean running = new AtomicBoolean(true);
try (MediaDriver mediaDriver = MediaDriver.launch();
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID);
Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
executor.execute(reporter);
executor.execute(() -> SamplesUtil.subscriberLoop(rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));
final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
do {
System.out.format("%nStreaming %,d messages of payload length %d bytes to %s on stream id %d%n", NUMBER_OF_MESSAGES, MESSAGE_LENGTH, CHANNEL, STREAM_ID);
printingActive = true;
long backPressureCount = 0;
for (long i = 0; i < NUMBER_OF_MESSAGES; i++) {
OFFER_BUFFER.putLong(0, i);
idleStrategy.reset();
while (publication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) < 0) {
backPressureCount++;
idleStrategy.idle();
}
}
System.out.println("Done streaming. backPressureRatio=" + ((double) backPressureCount / NUMBER_OF_MESSAGES));
if (LINGER_TIMEOUT_MS > 0) {
System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
Thread.sleep(LINGER_TIMEOUT_MS);
}
printingActive = false;
} while (barrier.await());
running.set(false);
reporter.halt();
executor.shutdown();
}
}
use of io.aeron.Subscription in project Aeron by real-logic.
the class RateSubscriber method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
* @throws InterruptedException if the task is interrupted
* @throws ExecutionException if the {@link Future} has an error.
*/
public static void main(final String[] args) throws InterruptedException, ExecutionException {
System.out.println("Subscribing to " + CHANNEL + " on stream id " + STREAM_ID);
final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
final ExecutorService executor = Executors.newFixedThreadPool(1);
final Aeron.Context ctx = new Aeron.Context().availableImageHandler(SamplesUtil::printAvailableImage).unavailableImageHandler(SamplesUtil::printUnavailableImage);
if (EMBEDDED_MEDIA_DRIVER) {
ctx.aeronDirectoryName(driver.aeronDirectoryName());
}
final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), SamplesUtil::printRate);
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> {
reporter.halt();
running.set(false);
});
try (Aeron aeron = Aeron.connect(ctx);
Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
final Future<?> future = executor.submit(() -> SamplesUtil.subscriberLoop(rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));
reporter.run();
System.out.println("Shutting down...");
future.get();
}
executor.shutdown();
if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
System.out.println("Warning: not all tasks completed promptly");
}
CloseHelper.close(driver);
}
use of io.aeron.Subscription in project Aeron by real-logic.
the class MultipleSubscribersWithFragmentAssembly method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
System.out.format("Subscribing to %s on stream ID %d and stream ID %d%n", CHANNEL, STREAM_ID_1, STREAM_ID_2);
final Aeron.Context ctx = new Aeron.Context().availableImageHandler(MultipleSubscribersWithFragmentAssembly::eventAvailableImage).unavailableImageHandler(MultipleSubscribersWithFragmentAssembly::eventUnavailableImage);
final FragmentAssembler assembler1 = new FragmentAssembler(reassembledMessage1(STREAM_ID_1));
final FragmentAssembler assembler2 = new FragmentAssembler(reassembledMessage2(STREAM_ID_2));
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> running.set(false));
try (Aeron aeron = Aeron.connect(ctx);
Subscription subscription1 = aeron.addSubscription(CHANNEL, STREAM_ID_1);
Subscription subscription2 = aeron.addSubscription(CHANNEL, STREAM_ID_2)) {
final IdleStrategy idleStrategy = new BackoffIdleStrategy(100, 10, TimeUnit.MICROSECONDS.toNanos(1), TimeUnit.MICROSECONDS.toNanos(100));
int idleCount = 0;
while (running.get()) {
final int fragmentsRead1 = subscription1.poll(assembler1, FRAGMENT_COUNT_LIMIT);
final int fragmentsRead2 = subscription2.poll(assembler2, FRAGMENT_COUNT_LIMIT);
if ((fragmentsRead1 + fragmentsRead2) == 0) {
idleStrategy.idle(idleCount++);
} else {
idleCount = 0;
}
}
System.out.println("Shutting down...");
}
}
use of io.aeron.Subscription in project Aeron by real-logic.
the class MultipleSubscribersWithFragmentAssembly method eventAvailableImage.
/**
* Print the information for an available image to stdout.
*
* @param image that has been created.
*/
public static void eventAvailableImage(final Image image) {
final Subscription subscription = image.subscription();
System.out.format("new image on %s streamId %x sessionId %x from %s%n", subscription.channel(), subscription.streamId(), image.sessionId(), image.sourceIdentity());
}
Aggregations