use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class MultiSubscriberTest method shouldReceiveMessageOnSeparateSubscriptions.
@Test(timeout = 10_000)
public void shouldReceiveMessageOnSeparateSubscriptions() {
final FragmentHandler mockFragmentHandlerOne = mock(FragmentHandler.class);
final FragmentHandler mockFragmentHandlerTwo = mock(FragmentHandler.class);
final FragmentAssembler adapterOne = new FragmentAssembler(mockFragmentHandlerOne);
final FragmentAssembler adapterTwo = new FragmentAssembler(mockFragmentHandlerTwo);
try (Publication publication = aeron.addPublication(CHANNEL_1, STREAM_ID);
Subscription subscriptionOne = aeron.addSubscription(CHANNEL_1, STREAM_ID);
Subscription subscriptionTwo = aeron.addSubscription(CHANNEL_2, STREAM_ID)) {
final byte[] expectedBytes = "Hello, World! here is a small message".getBytes();
final UnsafeBuffer srcBuffer = new UnsafeBuffer(expectedBytes);
assertThat(subscriptionOne.poll(adapterOne, FRAGMENT_COUNT_LIMIT), is(0));
assertThat(subscriptionTwo.poll(adapterTwo, FRAGMENT_COUNT_LIMIT), is(0));
while (!subscriptionOne.isConnected() || !subscriptionTwo.isConnected()) {
SystemTest.checkInterruptedStatus();
Thread.yield();
}
while (publication.offer(srcBuffer) < 0L) {
SystemTest.checkInterruptedStatus();
Thread.yield();
}
while (subscriptionOne.poll(adapterOne, FRAGMENT_COUNT_LIMIT) == 0) {
SystemTest.checkInterruptedStatus();
Thread.yield();
}
while (subscriptionTwo.poll(adapterTwo, FRAGMENT_COUNT_LIMIT) == 0) {
SystemTest.checkInterruptedStatus();
Thread.yield();
}
verifyData(srcBuffer, mockFragmentHandlerOne);
verifyData(srcBuffer, mockFragmentHandlerTwo);
}
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class EmbeddedThroughput method main.
public static void main(final String[] args) throws Exception {
loadPropertiesFiles(args);
final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedThroughput::printRate);
final FragmentHandler rateReporterHandler = rateReporterHandler(reporter);
final ExecutorService executor = Executors.newFixedThreadPool(2);
final AtomicBoolean running = new AtomicBoolean(true);
try (MediaDriver ignore = MediaDriver.launch();
Aeron aeron = Aeron.connect();
Publication publication = aeron.addPublication(CHANNEL, STREAM_ID);
Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
executor.execute(reporter);
executor.execute(() -> SamplesUtil.subscriberLoop(rateReporterHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription));
final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
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++) {
ATOMIC_BUFFER.putLong(0, i);
OFFER_IDLE_STRATEGY.reset();
while (publication.offer(ATOMIC_BUFFER, 0, ATOMIC_BUFFER.capacity()) < 0) {
OFFER_IDLE_STRATEGY.idle();
backPressureCount++;
}
}
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.logbuffer.FragmentHandler in project aeron by real-logic.
the class RateSubscriber method main.
public static void main(final String[] args) throws Exception {
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 FragmentHandler rateReporterHandler = new FragmentAssembler(rateReporterHandler(reporter));
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, 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.quietClose(driver);
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class EmbeddedExclusiveSpiedThroughput method main.
public static void main(final String[] args) throws Exception {
loadPropertiesFiles(args);
final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedExclusiveSpiedThroughput::printRate);
final FragmentHandler rateReporterHandler = rateReporterHandler(reporter);
final ExecutorService executor = Executors.newFixedThreadPool(2);
final AtomicBoolean running = new AtomicBoolean(true);
final MediaDriver.Context ctx = new MediaDriver.Context().spiesSimulateConnection(true);
try (MediaDriver ignore = MediaDriver.launch(ctx);
Aeron aeron = Aeron.connect();
ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID);
Subscription subscription = aeron.addSubscription(CommonContext.SPY_PREFIX + CHANNEL, STREAM_ID)) {
executor.execute(reporter);
executor.execute(() -> SamplesUtil.subscriberLoop(rateReporterHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription));
final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
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++) {
ATOMIC_BUFFER.putLong(0, i);
OFFER_IDLE_STRATEGY.reset();
while (publication.offer(ATOMIC_BUFFER, 0, ATOMIC_BUFFER.capacity()) < 0) {
OFFER_IDLE_STRATEGY.idle();
backPressureCount++;
}
}
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.logbuffer.FragmentHandler in project aeron by real-logic.
the class SubscriptionTest method shouldReadData.
@Test
public void shouldReadData() {
subscription.addImage(imageOneMock);
when(imageOneMock.poll(any(FragmentHandler.class), anyInt())).then((invocation) -> {
final FragmentHandler handler = (FragmentHandler) invocation.getArguments()[0];
handler.onFragment(atomicReadBuffer, HEADER_LENGTH, READ_BUFFER_CAPACITY - HEADER_LENGTH, header);
return 1;
});
assertThat(subscription.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT), is(1));
verify(fragmentHandler).onFragment(eq(atomicReadBuffer), eq(HEADER_LENGTH), eq(READ_BUFFER_CAPACITY - HEADER_LENGTH), any(Header.class));
}
Aggregations